Table of Contents
前言
准备阶段
解决方案
双飞翼布局
header
middle
left
right
footer
圣杯布局
结论
参考文章
Home Web Front-end HTML Tutorial [CSS] 双飞翼布局和圣杯布局_html/css_WEB-ITnose

[CSS] 双飞翼布局和圣杯布局_html/css_WEB-ITnose

Jun 21, 2016 am 08:47 AM

前言

在使用 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> 
Copy after login

圣杯布局

  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> 
Copy after login

结论

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

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

参考文章

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

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

圣杯布局

CSS布局终极方案之–圣杯布局
Statement of this Website
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

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Is HTML easy to learn for beginners? Is HTML easy to learn for beginners? Apr 07, 2025 am 12:11 AM

HTML is suitable for beginners because it is simple and easy to learn and can quickly see results. 1) The learning curve of HTML is smooth and easy to get started. 2) Just master the basic tags to start creating web pages. 3) High flexibility and can be used in combination with CSS and JavaScript. 4) Rich learning resources and modern tools support the learning process.

The Roles of HTML, CSS, and JavaScript: Core Responsibilities The Roles of HTML, CSS, and JavaScript: Core Responsibilities Apr 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.

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

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

What is an example of a starting tag in HTML? What is an example of a starting tag in HTML? Apr 06, 2025 am 12:04 AM

AnexampleofastartingtaginHTMLis,whichbeginsaparagraph.StartingtagsareessentialinHTMLastheyinitiateelements,definetheirtypes,andarecrucialforstructuringwebpagesandconstructingtheDOM.

Gitee Pages static website deployment failed: How to troubleshoot and resolve single file 404 errors? Gitee Pages static website deployment failed: How to troubleshoot and resolve single file 404 errors? Apr 04, 2025 pm 11:54 PM

GiteePages static website deployment failed: 404 error troubleshooting and resolution when using Gitee...

How to implement adaptive layout of Y-axis position in web annotation? How to implement adaptive layout of Y-axis position in web annotation? Apr 04, 2025 pm 11:30 PM

The Y-axis position adaptive algorithm for web annotation function This article will explore how to implement annotation functions similar to Word documents, especially how to deal with the interval between annotations...

HTML, CSS, and JavaScript: Essential Tools for Web Developers HTML, CSS, and JavaScript: Essential Tools for Web Developers Apr 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.

How to use CSS3 and JavaScript to achieve the effect of scattering and enlarging the surrounding pictures after clicking? How to use CSS3 and JavaScript to achieve the effect of scattering and enlarging the surrounding pictures after clicking? Apr 05, 2025 am 06:15 AM

To achieve the effect of scattering and enlarging the surrounding images after clicking on the image, many web designs need to achieve an interactive effect: click on a certain image to make the surrounding...

See all articles