圣杯布局详解三步

圣杯布局详解三步

总结:圣杯布局用到了文档流,浮动流,定位流。还有用到了负margin的。圣杯布局解决了先加载主要内容的问题
把大象装进冰箱需要几步?

1.布局成这个样子,中间一个容器包裹中间,左边,右边

在这里插入图片描述

Document
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0,maximum-scale=1.0,user-scalable=no,shrink-to-fit=no,viewport=cover">
    <title>Document</title>
    <style>
        body {
            width: 100%;
        }
        .header {
            width: 100%;
            height: 50px;
            background-color: #878e7d;
        }
        .center {
            width: 100%;
            background-color: #6737c6;
        }
        .left {
            width: 200px;
            background-color: #369b42;
        }
        .right {
            width: 150px;
            background-color: #d68419;
        }
        .footer {
            width: 100%;
            height: 50px;
            background-color: #9dc4c3;
        }
        .box {
            /* 居中文字 */
            text-align: center;
        }
    </style>
</head>
<body>
    <header class="header box">header</header>
    <section class="section clearfix">
        <div class="center column box">中间部分</div>
        <div class="left column box">左边部分</div>
        <div class="right column box">右边部分</div>
    </section>
    <footer class="footer box">footer</footer>
</body>
</html>

2.开启左浮动,并清除浮动影响,并设置父元素padding

在这里插入图片描述

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0,maximum-scale=1.0,user-scalable=no,shrink-to-fit=no,viewport=cover">
    <title>Document</title>
    <style>
        body {
            width: 100%;
        }
        .header {
            width: 100%;
            height: 50px;
            background-color: #878e7d;
        }
        .box {
            /* 居中文字 */
            text-align: center;
        }
        .section {
            padding-left: 200px;
            padding-right: 150px;
        }
        .column {
            /* 2.开启左浮动,并清除浮动并给父元素设置padding */
            float: left;
        }
        /* 清除浮动 */
        .clearfix:after {
            content: '';
            display: block;
            height: 0;
            visibility: hidden;
            clear: both;
        }
        .clearfix {
            /* IE的清除浮动,触发hasLayout */
            zoom: 1;
        }
        .center {
            width: 100%;
            background-color: #6737c6;
        }
        .left {
            width: 200px;
            background-color: #369b42;
        }
        .right {
            width: 150px;
            background-color: #d68419;
        }
        .footer {
            width: 100%;
            height: 50px;
            background-color: #9dc4c3;
        }
        
    </style>
</head>
<body>
    <header class="header box">header</header>
    <section class="section clearfix">
        <div class="center column box">中间部分</div>
        <div class="left column box">左边部分</div>
        <div class="right column box">右边部分</div>
    </section>
    <footer class="footer box">footer</footer>
</body>
</html>

3.把左右两边使用负margin和positon realtive 放到对应的位置

在这里插入图片描述

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0,maximum-scale=1.0,user-scalable=no,shrink-to-fit=no,viewport=cover">
    <title>Document</title>
    <style>
        body {
            width: 100%;
        }
        .header {
            width: 100%;
            height: 50px;
            background-color: #878e7d;
        }
        .box {
            /* 居中文字 */
            text-align: center;
        }
        .section {
            padding-left: 200px;
            padding-right: 150px;
        }
        .column {
            /* 2.开启左浮动,并清除浮动并给父元素设置padding */
            float: left;
            position: relative;
        }
        /* 清除浮动 */
        .clearfix:after {
            content: '';
            display: block;
            height: 0;
            visibility: hidden;
            clear: both;
        }
        .clearfix {
            /* IE的清除浮动,触发hasLayout */
            zoom: 1;
        }
        .center {
            width: 100%;
            background-color: #6737c6;
        }
        .left {
            width: 200px;
            background-color: #369b42;
            /* 3.使用负margin 和 realtive  把左右两边放到位置 */
            margin-left: -100%;
            left: -200px;
        }
        .right {
            width: 150px;
            background-color: #d68419;
            margin-right: -150px;
            /* 除了 margin-left -150 也可以使用margin-left */
            /* margin-left: -150px;
            right: -150px; */
        }
        .footer {
            width: 100%;
            height: 50px;
            background-color: #9dc4c3;
        }
    </style>
</head>
<body>
    <header class="header box">header</header>
    <section class="section clearfix">
        <div class="center column box">中间部分</div>
        <div class="left column box">左边部分</div>
        <div class="right column box">右边部分</div>
    </section>
    <footer class="footer box">footer</footer>
</body>
</html>

注意: 在第3步可能难以理解,那是因为margin-left= 负数元素宽度,因为3个元素都是浮动的,左边就跟中间部分重合了。右边部分没有右marigin之后,浏览器就看不见它了,就安排到父容器的右边padding位置了

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值