实现两边定宽,中间自适应布局(三栏布局)的七种方法

7 篇文章 0 订阅
浮动法
    <style type="text/css">
        html,body{
            height: 100%;
            margin: 0;
        } 
        div{
            height: 100%;
        }
        .left{ 
            background-color: red;
            float: left;
            width: 300px; 
        } 
        .right{ 
            background-color: yellow; 
            float: right; 
            width: 300px; 
        } 
        .center{ 
            background-color: green; 
            margin-left: 300px;
            margin-right: 300px;
        } 
    </style> 

<body> 
    <div class="left">left</div> 
    <div class="right">right</div> 
    <div class="center">center</div> 
</body> 
定位法
   <style type="text/css">
       html,body{
           height: 100%;
           margin: 0;
       } 
       div{
           height: 100%;
       }
       .parent{ 
           position: relative; 
       }
       .left{ 
           background-color: red; 
           width: 300px; 
           position: absolute; 
           left: 0; 
       } 
       .right{ 
           background-color: yellow; 
           width: 300px; 
           position: absolute; 
           right: 0;
       } 
       .center{ 
           background-color: green; 
           margin-left: 300px;
           margin-right: 300px;
       }
   </style> 

<body> 
   <div class="parent"> 
       <div class="left">left</div> 
       <div class="right">right</div> 
       <div class="center">center</div>
   </div> 
</body> 
弹性盒子法
    <style type="text/css">
        html,body{
            height: 100%;
            margin: 0;
        } 
        div{
            height: 100%;
        }
        .parent{ 
            display: flex; 
        } 
        .left{ 
            width: 300px; 
            background-color: red; 
        } 
        .right{ 
            width: 300px; 
            background-color: yellow; 
        } 
        .center{ 
            flex-grow:1; 
            background-color: green; 
        } 
    </style> 

<body> 
    <div class="parent"> 
        <div class="left">left</div> 
        <div class="center">center</div> 
        <div class="right">right</div> 
    </div> 
</body>
网格布局
    <style type="text/css">
        html,body{
            height: 100%;
            margin: 0;
        } 
        div{
            height: 100%;
        }
        .parent{ 
            display: grid;
            grid-template-columns: 300px auto 300px;
        }
        .left{ 
            background-color: red; 
        } 
        .right{ 
            background-color: yellow; 
        } 
        .center{ 
            background-color: green; 
        }
    </style> 

<body> 
    <div class="parent"> 
        <div class="left">left</div> 
        <div class="center">center</div> 
        <div class="right">right</div> 
    </div> 
</body>  
表格布局
    <style>
        body,html{
            height: 100%;
            margin: 0;
        }
        div{
            height: 100%;
        }
        .parent{
            width: 100%;
            display: table;
        }
        .right{
            width: 300px;
            background-color: red;
            display: table-cell;
        }
        .left{
            width: 300px;
            background-color: blue;
            display: table-cell;
        }
        .center{
            background-color: yellow;
            display: table-cell;
        }
    </style>

<body> 
    <div class="parent">
        <div class="left">left</div>
        <div class="center">center</div>
        <div class="right">right</div>
    </div>
</body> 
双飞翼
    <style>
        html,body{
            height: 100%;
            margin: 0;
        }
        div{
            height: 100%;
        }
        .center{
            width: 100%;
            float: left;
        }
        .main{
            background-color: blueviolet;
            margin-left: 300px;
            margin-right: 300px;
        }
        .left{
            background-color: cornflowerblue;
            width: 300px;
            float: left;
            margin-left: -100%;
        }
        .right{
            background-color: #ff0097;
            width: 300px;
            float: left;
            margin-left: -300px;
        }
    </style>

<body>
    <div  class="center">
        <div class="main">center</div>
    </div>
    <div class="left">left</div>
    <div class="right">right</div>
</body>
圣杯
    <style>
        body,html{
            height: 100%;
            margin: 0;
        }
        div{
            height: 100%;
        }
        .parent{
            padding-left: 300px;
            padding-right: 300px;
        }
        .right{
            background-color: red;
            width: 300px;
            float: left;
            margin-left: -300px;
            position: relative;
            right: -300px;
        }
        .left{
            background-color: blue;
            width: 300px;
            float: left;
            margin-left: -100%;
            position: relative;
            left: -300px;
        }
        .center{
            background-color: yellow;
            width: 100%;
            float: left;
        }
    </style>

<body> 
    <div class="parent">
        <div class="center">center</div>
        <div class="left">left</div>
        <div class="right">right</div>
    </div>
</body> 
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值