CSS-布局4-双飞翼布局

1、双飞翼布局概述

双飞翼布局同样来源淘宝,可以说是借鉴了圣杯布局,同时也解决了圣杯布局使用相对定位的缺陷。

2、双飞翼布局实现思路

(1)与圣杯布局一样,利用负边距技术实现初步效果

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>双飞翼布局</title>
    <style type="text/css">
        *{
            margin:0px;
            padding:0px;
        }
        .header{
            background : yellow;
        }
        .left{
            width:198px;
            height:200px;
            float:left;
            border: 1px solid red;
            margin-left :-100%;
        }
        .right{
            width:198px;
            height:200px;
            border: 1px solid blue;
            float: left;
            margin-left :-200px;
        }
        .center{
            width : 100%;
            height:200px;
            float: left;
            background :gray;
        }
        .footer{
            background : blue;
        }
    </style>
</head>
<body>
    <div class="header">heder</div>
    <div class="container">
        <div class="center">center</div>
        <div class="left">left</div>
        <div class="right">right</div>
    </div>
    <div class="footer">footer</div>
</body>
</html>

运行效果:

image.png

(2)在cetner元素中间添加一个元素,设置margin-left和margin-right
center部分html源代码修改:

<div class="center">
    <div class="center-inner">
    center
    </div>
</div>

center-inner 样式添加

.center-inner{
    margin-left:200px;
    margin-right:200px;
    background:red;
}

运行效果:


image.png

3、等高双飞翼布局实现

(1)使用包裹元素contianer的overflow:hidden 触发BFC。
(2)使用 padding-bottom: 9999px; margin-bottom: -9999px;将元素展开,然后再把元素收回。
源代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>双飞翼布局</title>
    <style type="text/css">
        *{
            margin:0px;
            padding:0px;
        }
        .header{
            background : yellow;
        }
        .container{
            overflow: hidden;
        }
        .left{
            width:198px;
            float:left;
            border: 1px solid red;
            margin-left :-100%;
            padding-bottom: 9999px;
            margin-bottom: -9999px;
        }
        .right{
            width:198px;
            border: 1px solid blue;
            float: left;
            margin-left :-200px;
            padding-bottom: 9999px;
            margin-bottom: -9999px;
        }
        .center{
            width : 100%;
            float: left;
            background :gray;
            padding-bottom: 9999px;
            margin-bottom: -9999px;
        }
        .center-inner{
            margin-left:200px;
            margin-right:200px;
            background:red;
        }
        .footer{
            background : blue;
        }
    </style>
</head>
<body>
    <div class="header">heder</div>
    <div class="container">
        <div class="center">
            <div class="center-inner">
            center<br/>
            center<br/>
            center<br/>
            </div>
        </div>
        <div class="left">left</div>
        <div class="right">right</div>
    </div>
    <div class="footer">footer</div>
</body>
</html>

运行效果:

image.png

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 问答题47 /72 常见浏览器兼容性问题与解决方案? 参考答案 (1)浏览器兼容问题一:不同浏览器的标签默认的外补...
    _Yfling阅读 13,796评论 1 92
  • 双飞翼布局和BFC 之前看到了一些面试题中,面试官会问到如果实现双飞翼布局或者是圣杯布局,这两个布局的理念基本是类...
    LucasDog阅读 1,070评论 0 4
  • 三栏式布局 涉及浮动和清除浮动,主要讲解“圣杯”和“双飞翼”两种解决方法。这两种方法实现的都是三栏布局,两边的盒子...
    紫电倚青霜阅读 2,381评论 0 6
  • 收听音频,戳链接,旧号itclan已暂停使用,欢迎关注微信itclanCoder公众号可收听更多音频 前言 关于网...
    itclanCoder阅读 8,210评论 3 30
  • display: none; 与 visibility: hidden; 的区别 联系:它们都能让元素不可见 区别...
    纹小艾阅读 1,661评论 0 1