页面布局的几种方法

高度固定,左右300px,中间自适应

第一种: float

浮动会脱离文档流,要清除浮动,兼容性比较好

第二种: position

定位会脱离文档流,所以子元素也会脱离文档流,写法比较便捷

第三种: flex

不兼容IE8,最常用,兼容性也好

第四种: table

操作繁琐,兼容性好,兼容IE8

第五种: grid

代码量少,新出的属性

代码如下:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        html *{
            padding: 0;
            margin: 0;
        }
        .layout{
            margin-top: 20px;
        }
        .layout div{
            min-height: 100px;
        }
    </style>
    <style>
        /*float*/
        .layout .float .left{
            float: left;
            width: 300px;
            background: red;
        }
        .layout .float .center{
            background: green;
        }
        .layout .float .right{
            float: right;
            width: 300px;
            background: blue;
        }
    </style>
    <style>
        /*position*/
        .layout .position div{
            position: absolute;
        }
        .layout .position .left{
            left: 0;
            width: 300px;
            background: red;
        }
        .layout .position .center{
            left: 300px;
            right: 300px;
            background: green;
        }
        .layout .position .right{
            right: 0;
            width: 300px;
            background: blue;
        }
    </style>
    <style>
        /*flex*/
        .layout .flex{
            display: flex;
            margin-top: 140px;
        }
        .layout .flex .left{
            width: 300px;
            background: red;
        }
        .layout .flex .center{
            flex: 1 auto;
            background: green;
        }
        .layout .flex .right{
            width: 300px;
            background: blue;
        }
    </style>
    <style>
        /*table*/
        .layout .table{
            display: table;
            width: 100%;
            height: 100px;
        }
        .layout .table .left{
            display: table-cell;
            width: 300px;
            background: red;
        }
        .layout .table .center{
            display: table-cell;
            background: green;
        }
        .layout .table .right{
            display: table-cell;
            width: 300px;
            background: blue;
        }
    </style>
    <style>
        /*grid*/
        .layout .grid{
            display: grid;
            width: 100%;
            grid-template-columns: 300px auto 300px;
            grid-template-rows: 100px;
        }
        .layout .grid .left{
            background: red;
        }
        .layout .grid .center{
            background: green;
        }
        .layout .grid .right{
            background: blue;
        }
    </style>
</head>
<body>
<!--float-->
    <section class="layout">
        <arcticle class="float">
            <div class="left"></div>
            <div class="right"></div>
            <div class="center">
                这是中间float
            </div>
        </arcticle>
    </section>
<!--position-->
    <section class="layout">
        <arcticle class="position">
            <div class="left"></div>
            <div class="center">
                这是中间position
            </div>
            <div class="right"></div>
        </arcticle>
    </section>
<!--flex-->
    <section class="layout">
        <arcticle class="flex">
            <div class="left"></div>
            <div class="center">
                这是中间flex
            </div>
            <div class="right"></div>
        </arcticle>
    </section>
<!--table-->
    <section class="layout">
        <arcticle class="table">
            <div class="left"></div>
            <div class="center">
                这是中间table
            </div>
            <div class="right"></div>
        </arcticle>
    </section>
<!--grid-->
<section class="layout">
    <arcticle class="grid">
        <div class="left"></div>
        <div class="center">
            这是中间grid
        </div>
        <div class="right"></div>
    </arcticle>
</section>
</body>
</html>

结果如图:

 

问题思考:

1、上高度固定,下高度自适应(可以用position,table,flex,grid)

2、下高度固定,上高度自适应(可以用position,table,flex,grid)

3、左宽度固定,右自适应(可以用float,position,table,flex,grid)

4、右宽度固定,左自适应(可以用float,position,table,flex,grid)

5、上下高度固定,中间自适应(可以用table,flex,position,grid)

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值