search
HomeWeb Front-endHTML Tutorial[CSS] 双飞翼布局和圣杯布局_html/css_WEB-ITnose

前言

在使用 CSS 进行布局的时候,我们通常会用到 div 布局和 float padding margin position 之类的CSS属性,使用不同的组合可以得出很多的布局方案。本篇介绍的是三栏布局方案中的双飞翼布局和圣杯布局,它们解决的问题是一样的:左侧和右侧的部分宽度固定,中间部分宽度随浏览器宽度的变化而自适应变化。它们的实现方案在前半部分大同小异,不同之处是如何使中间部分的内容不被两侧遮挡。

准备阶段

此部分属于双飞翼布局和圣杯布局实现方案的前半部分,实现原理是利用 float 和 negative margin 技术。可以按以下步骤进行:

  1. 按 middle left right 的顺序写出三个 div ,设置 left 和 right 固定宽度分别为 190px , 220px (可以给这三栏都加上 min-height ,让它们的初始高度一致。还可以给作为容器的 container 加上 min-width ,以避免窗口宽度缩小时,中间部分消失的问题)

  2. 给这三栏都加上 float: left

  3. 给 middle 加上 width: 100% ,这时候 left 和 right 会被挤下去

  4. 给 left 加上 margin-left: -100% ,拉到 middle 的左边

  5. 给 right 加上 margin-left: -220 px ,拉到 middle 的右边

在准备阶段完成后,中间部分的内容会被两侧遮挡到,接下来分别介绍两种布局对于该问题的解决方案。

解决方案

双飞翼布局

给 middle 加一层包裹 middle-wrap ,并设置其 margin: 0 220px 0 190px

以下是完整的代码实现:

<!DOCTYPEhtml><htmllang="en"> <head>    <metacharset="UTF-8">    <title>双飞翼布局</title>    <styletype="text/css">    *{        margin: 0;        padding: 0;    }     .middle,    .left,    .right{        float: left;        min-height: 200px;    }     .middle{        width: 100%;        background-color: green;    }     .middle-wrap{        margin: 0 220px 0 190px;    }     .left{        width: 190px;        margin-left: -100%;        background-color: red;    }     .right{        width: 220px;        margin-left: -220px;        background-color: blue;    }     .container{        min-width: 600px;    }     .header,    .footer{        text-align: center;        background-color: yellow;    }     .footer{        clear: both;    }    </style></head> <body>    <divclass="header">        <h2 id="header">header</h2>    </div>    <divclass="container">        <divclass="middle">            <divclass="middle-wrap">                <h2 id="middle">middle</h2>            </div>        </div>        <divclass="left">            <h2 id="left">left</h2>        </div>        <divclass="right">            <h2 id="right">right</h2>        </div>    </div>    <divclass="footer">        <h2 id="footer">footer</h2>    </div></body> </html> 

圣杯布局

  1. 给这三栏的容器 container 加上 padding: 0 220px 0 190px,这会使整个容器左右两边各留出 190px 和 220px 的 padding 。

  2. 对 left 和 right 设置相对定位 position: relative 和偏移量,从而将它们移动到左右两边的 padding 中。

以下是完整的代码实现:

<!DOCTYPEhtml><htmllang="en"> <head>    <metacharset="UTF-8">    <title>圣杯布局</title>    <styletype="text/css">    *{        margin: 0;        padding: 0;    }     .middle,    .left,    .right{        float: left;        min-height: 200px;    }     .left,    .right{        position: relative;    }     .middle{        width: 100%;        background-color: green;    }     .left{        width: 190px;        margin-left: -100%;        left: -190px;        background-color: red;    }     .right{        width: 220px;        margin-left: -220px;        right: -220px;        background-color: blue;    }     .container{        padding: 0 220px 0 190px;        min-width: 600px;    }     .header,    .footer{        text-align: center;        background-color: yellow;    }     .footer{        clear: both;    }    </style></head> <body>    <divclass="header">        <h2 id="header">header</h2>    </div>    <divclass="container">        <divclass="middle">            <h2 id="middle">middle</h2>        </div>        <divclass="left">            <h2 id="left">left</h2>        </div>        <divclass="right">            <h2 id="right">right</h2>        </div>    </div>    <divclass="footer">        <h2 id="footer">footer</h2>    </div></body> </html> 

结论

  1. 两种布局都可以实现中间内容最先加载。

  2. 相比圣杯布局,双飞翼布局多创建了一个 div ,但不用相对定位。双飞翼布局兼容性很好,可以兼容到IE6。此处的圣杯布局经个人测试,可以兼容到IE8。

参考文章

CSS布局 — 圣杯布局 & 双飞翼布局

双飞翼布局介绍-始于淘宝UED

圣杯布局

CSS布局终极方案之–圣杯布局
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
HTML: Is It a Programming Language or Something Else?HTML: Is It a Programming Language or Something Else?Apr 15, 2025 am 12:13 AM

HTMLisnotaprogramminglanguage;itisamarkuplanguage.1)HTMLstructuresandformatswebcontentusingtags.2)ItworkswithCSSforstylingandJavaScriptforinteractivity,enhancingwebdevelopment.

HTML: Building the Structure of Web PagesHTML: Building the Structure of Web PagesApr 14, 2025 am 12:14 AM

HTML is the cornerstone of building web page structure. 1. HTML defines the content structure and semantics, and uses, etc. tags. 2. Provide semantic markers, such as, etc., to improve SEO effect. 3. To realize user interaction through tags, pay attention to form verification. 4. Use advanced elements such as, combined with JavaScript to achieve dynamic effects. 5. Common errors include unclosed labels and unquoted attribute values, and verification tools are required. 6. Optimization strategies include reducing HTTP requests, compressing HTML, using semantic tags, etc.

From Text to Websites: The Power of HTMLFrom Text to Websites: The Power of HTMLApr 13, 2025 am 12:07 AM

HTML is a language used to build web pages, defining web page structure and content through tags and attributes. 1) HTML organizes document structure through tags, such as,. 2) The browser parses HTML to build the DOM and renders the web page. 3) New features of HTML5, such as, enhance multimedia functions. 4) Common errors include unclosed labels and unquoted attribute values. 5) Optimization suggestions include using semantic tags and reducing file size.

Understanding HTML, CSS, and JavaScript: A Beginner's GuideUnderstanding HTML, CSS, and JavaScript: A Beginner's GuideApr 12, 2025 am 12:02 AM

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

The Role of HTML: Structuring Web ContentThe Role of HTML: Structuring Web ContentApr 11, 2025 am 12:12 AM

The role of HTML is to define the structure and content of a web page through tags and attributes. 1. HTML organizes content through tags such as , making it easy to read and understand. 2. Use semantic tags such as, etc. to enhance accessibility and SEO. 3. Optimizing HTML code can improve web page loading speed and user experience.

HTML and Code: A Closer Look at the TerminologyHTML and Code: A Closer Look at the TerminologyApr 10, 2025 am 09:28 AM

HTMLisaspecifictypeofcodefocusedonstructuringwebcontent,while"code"broadlyincludeslanguageslikeJavaScriptandPythonforfunctionality.1)HTMLdefineswebpagestructureusingtags.2)"Code"encompassesawiderrangeoflanguagesforlogicandinteract

HTML, CSS, and JavaScript: Essential Tools for Web DevelopersHTML, CSS, and JavaScript: Essential Tools for Web DevelopersApr 09, 2025 am 12:12 AM

HTML, CSS and JavaScript are the three pillars of web development. 1. HTML defines the web page structure and uses tags such as, etc. 2. CSS controls the web page style, using selectors and attributes such as color, font-size, etc. 3. JavaScript realizes dynamic effects and interaction, through event monitoring and DOM operations.

The Roles of HTML, CSS, and JavaScript: Core ResponsibilitiesThe Roles of HTML, CSS, and JavaScript: Core ResponsibilitiesApr 08, 2025 pm 07:05 PM

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools