圣杯布局和双飞翼布局的三栏式

二者都是三栏左右定宽中间自适应的典型布局

圣杯布局

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>圣杯布局</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        .header,.footer{
            height: 32px;
            background: #444;
            width:100%;
            color: #eee;
            text-align: center;/*水平居中文字*/
            line-height: 32px;
            /* lin-height是为了垂直居中文字 */
        }
        .container{ 
            width: 100%;
            box-sizing: border-box;
            padding-left: 100px;
            padding-right: 200px;
            overflow: hidden;/*形成bfc,因为里面的div块都已经float了*/
        }
        .main{
            width: 100%;
            float: left;
            background-color: dimgray;
            min-height: 300px;
        }
        .left{
            float: left;
            width: 100px;
            background-color: aquamarine;
            margin-left: -100%;/*文档流向左移动一整行*/
            position: relative;
            left: -100px;
            min-height: 200px;
        }
        .right{
            float: left;
            width: 200px;
            background-color: coral;
            margin-left: -200px;/*文档流向左移动一个自己*/
            position: relative;
            left: 200px;
            min-height: 400px;
        }
    </style>
</head>
<body>
    <div class="header">头部</div>
    <div class="container">
        <div class="main">中间中间中间中间中间中间中间中间中间中间中间中间中间中间中间中间中间中间中间中间中间中间中间中间中间中间中间中间中间中间中间中间中间中间中间中间</div>
        <div class="left">左边左边左边左边左边左边左边左边左边左边左边左边左边左边左边左边左边</div>
        <div class="right">右边右边右边右边右边右边右边右边右边右边右边右边右边右边右边右边右边右边右边右边右边右边右边右边右边右边右边右边右边右边右边右边右边右边右边右边右边右边右边右边右边右边右边右边右边右边右边右边右边</div>
    </div>
    <div class="footer">尾部</div>
</body>
</html>

image-20200802104750657

过程就是:

  1. 首先头部、正文、尾部上中下通过width:100%分开

  2. 正文设置怪异盒模型后给左右padding分别等于left和right的宽度

  3. 然后左边中间和正文都是左浮动,且中间放到最前面,中间宽度为100%

  4. 这时候第一行只有中间,左边和右边在第二行,正文的左右padding起到作用所以都有一段为空白

  5. 然后设置left的margin-left为-100%,让其文档流退一行退到中间内部的最左边,然后使用relative定位让他左移自己的宽度

  6. right部分只需要margin-left为其本身宽度,退到中间内部的最右侧,然后使用relative定位右移自己的宽度

双飞翼布局

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>双飞翼布局</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        .header,
        .footer {
            height: 32px;
            background: #444;
            width: 100%;
            color: #eee;
            text-align: center;
            /*水平居中文字*/
            line-height: 32px;
            /* lin-height是为了垂直居中文字 */
        }
        .container {
            width: 100%;
            overflow: hidden;
            /*形成bfc,因为里面的div块都已经float了*/
        }
        .left,.middle,.right{
            float: left;
        }
        .left{
            background-color:aquamarine;
            width: 100px;
            min-height: 200px;
            margin-left: -100%;
        }
        .right{
            background-color:coral;
            width: 200px;
            min-height: 400px;
            margin-left: -200px;
        }
        .middle{
            margin: 0 200px 0 100px;
        }
        .main{
            width: 100%;
            background-color: dimgray;
        }
    </style>
</head>

<body>
    <div class="header">头部</div>
    <div class="container">
        <div class="middle">
            <div class="main">中间中间中间中间中间中间中间中间中间中间中间中间中间中间中间中间中间中间中间中间中间中间中间中间中间中间中间中间中间中间中间中间中间中间中间中间</div>
        </div>
        <div class="left">左边左边左边左边左边左边左边左边左边左边左边左边左边左边左边左边左边</div>
        <div class="right">
            右边右边右边右边右边右边右边右边右边右边右边右边右边右边右边右边右边右边右边右边右边右边右边右边右边右边右边右边右边右边右边右边右边右边右边右边右边右边右边右边右边右边右边右边右边右边右边右边右边</div>
    </div>
    <div class="footer">尾部</div>
</body>

</html>

image-20200802110916424
过程:

  1. 和圣杯相比,对于中间块要在外面套一个middle
  2. middle、left、right都进行左浮动
  3. 不需要container的padding,代替为middle的左右margin
  4. left直接margin-left为-100%
  5. right直接为margin-left为负的自身宽度
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值