圣杯布局的三种写法

圣杯布局通俗点说就是:中间盒子宽度自适应,两边盒子固定宽度,并且中间盒子放在最前面,便于优先加载。

通过网上的资料查询,大致总结了三种写法

第一种:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <header>圣杯布局</header>
    <div class="main">
        <div class="center">一二三四五六七八九十一二三四五六七八九十一二三四五六七八九十一二三四五六七八九十一二三四五六七八九十一二三四五六七八九十一二三四五六七八九十一二三四五六七八九十一二三四五六七八九十一二三四五六七八九十中</div>
        <div class="left">左</div>
        <div class="right">右</div>
    </div>
    <footer>脚部</footer>
</body>
<style >
    .main{
      padding: 0 100px;
      background-color: red;

    }
    .main::after{
        content: "";
        display: block;
        clear: both;
    }
    .center {
        width: 100%;
        height: 100px;
        background: lightblue;
        float: left;
    }
    .left{
        width: 100px;
        height: 100px;
        background: lightgreen;
        float: left;
        margin-left: -100%;
        position: relative;
        left: -100px;

    }
    .right{
        width: 100px;
        height: 100px;
        background: yellow;
        float: left;
        margin-left: -100px;
        position: relative;
        right: -100px;
    }
    footer{
        background-color: bisque;
    }
</style>
</html>

 第二种:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <header>圣杯布局</header>
    <div class="main">
        <div class="wapper">
            <div class="center">一二三四五六七八九十一二三四五六七八九十一二三四五六七八九十一二三四五六七八九十一二三四五六七八九十一二三四五六七八九十一二三四五六七八九十一二三四五六七八九十一二三四五六七八九十一二三四五六七八九十中</div>
        </div>
        <div class="left">左</div>
        <div class="right">右</div>
    </div>
    <footer>脚部</footer>
</body>
<style >
    .wapper{
        width: 100%;
        height: 100px;
        background: lightblue;
        float: left;
       
    }
    .center {
        margin: 0 100px;
      
    }
    .left{
        width: 100px;
        height: 100px;
        background: lightgreen;
        float: left;
        margin-left: -100%;

    }
    .right{
        width: 100px;
        height: 100px;
        background: yellow;
        float: left;
        margin-left: -100px;
    }
    footer{
        background-color: bisque;
    }
</style>
</html>

第三种:利用的弹性布局,个人觉得这种代码最少,且项目中用到flex的最多,是最合适的一种。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <header>圣杯布局</header>
    <div class="main">
        <div class="center">中</div>
        <div class="left">左</div>
        <div class="right">右</div>
    </div>
    <footer>脚部</footer>
</body>
<style >
    .main{
        display: flex;
    }
    .center {
        width: 150px;
        height: 100px;
        background: lightblue;
        flex-grow:1;
        order: 2;
    }
    .left{
        width: 150px;
        height: 100px;
        background: lightgreen;
        order: 1;
    }
    .right{
        width: 100px;
        height: 100px;
        background: yellow;
        order: 3;
    }
    footer{
        background-color: bisque;
    }
</style>
</html>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值