Home  >  Article  >  Web Front-end  >  [CSS] 双飞翼布局和圣杯布局_html/css_WEB-ITnose

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

WBOY
WBOYOriginal
2016-06-21 08:47:161384browse

前言

在使用 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>header</h2>    </div>    <divclass="container">        <divclass="middle">            <divclass="middle-wrap">                <h2>middle</h2>            </div>        </div>        <divclass="left">            <h2>left</h2>        </div>        <divclass="right">            <h2>right</h2>        </div>    </div>    <divclass="footer">        <h2>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>header</h2>    </div>    <divclass="container">        <divclass="middle">            <h2>middle</h2>        </div>        <divclass="left">            <h2>left</h2>        </div>        <divclass="right">            <h2>right</h2>        </div>    </div>    <divclass="footer">        <h2>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