三栏布局

页面布局

实现一个高度固定的三栏布局
在这里插入图片描述
有五种方式:绝对定位实现、float实现、flex实现、table和table-cell实现、grid实现。

<section class="content">
    <div class="left">left</div>
    <div class="middle">middle</div>
    <div class="right">class</div>
</section>
  1. 绝对定位实现:
   <style>
        .content{
            height: 500px;
            position: relative;
        }
        .content div{
            height: 100%;
        }
        .left,.right{
            width: 300px;
            background-color: chocolate;
        }
        .left{
            position: absolute;
            top: 0;
            left: 0;
        }
        .right{
            position: absolute;
            top: 0;
            right: 0;
        }
        .middle{
            position: absolute;
            top: 0;
            /*关键在左右宽度*/
            left: 300px;
            right: 300px;
            background-color: cornflowerblue;
        }
    </style>
  1. float实现:
    <style>
        .content{
            height: 500px;
        }
        .content div{
            height: 100%;
        }
        .left,.right{
            width: 300px;
            background-color: chocolate;
        }
        .left{
            float: left;
        }
        .right{
            float: right;
        }
        .middle{
            background-color: cornflowerblue;
        }
    </style>
    <section class="content">
	    <div class="left">left</div>
	    <div class="right">class</div>
	    <div class="middle">middle放最后</div>
	</section>
  1. flex实现:
    <style>
        .content{
            height: 500px;
            display: flex;
        }
        .content div{
            height: 100%;
        }
        .left,.right{
            width: 300px;
            background-color: chocolate;
        }
        .middle{
            flex: 1;	/*flex:1;自适应*/
            background-color: cornflowerblue;
        }
    </style>
  1. table-cell实现:
   <style>
        .content{
            width: 100%;
            /*父容器宽度100% 没设置宽度的table-cell自动填充*/
            height: 500px;
            display: table;
        }
        .content div{
            height: 100%;
            display: table-cell;
        }
        .left,.right{
            width: 300px;
            background-color: chocolate;
        }
        .middle{
            background-color: cornflowerblue;
        }
    </style>
  1. grid实现:
    <style>
        .content{
            width: 100%;
            height: 500px;
            display: grid;
            grid-template-columns: 300px auto 300px;
        }
        .content div{
            /*height: 100%;*/
        }
        .left,.right{
            background-color: chocolate;
        }
        .middle{
            background-color: cornflowerblue;
        }
    </style>

优缺点总结:
在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值