行列布局,部分固定,部分自适应原理总结

有好多种情况,比如 三行布局,上下固定,中间自适应,好多移动端app是这种原理,各种情况有好几种方法,都是很简单的原理,但是其中的方法不能都想出来,篇幅比较长(代码可直接运行)

一、两列布局,左侧固定,右侧自适应

1、左侧设置浮动(脱离文档流),右侧设置margin-left

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        #left{
            width: 300px;
            background: #ff0000;
            height: 400px;
            display: table-cell;
        }
        #right{
            background: #00ff00;
            height: 400px;
            display: table-cell;
        }
        #main{
            display: table;
            width: 100%;
        }
    </style>
</head>
<body>
    <div id="main">
        <div id="left"></div>
        <div id="right"></div>
    </div>
</body>
</html>

2、左侧绝对定位,脱离文档流,对右侧设置margin-left,
可以使两者水平对齐

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        #left{
            width: 300px;
            background: #ff0000;
            height: 400px;
            /*position: absolute;!* fixed *!*/
        }
        #right{
            background: #00ff00;
            height: 400px;
            margin-left: 310px;
        }
    </style>
</head>
<body>
    <div id="left"></div>
    <div id="right"></div>
</body>
</html>

3.利用弹性盒模型,flex-grow=1,让剩下的元素使用剩余空间

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        #left{
            width: 300px;
            background: #ff0000;
            height: 400px;
        }
        #right{
            background: #00ff00;
            height: 400px;
            flex-grow: 1;
            margin-left: 10px;
        }
        #main{
            display: flex;
        }
    </style>
</head>
<body>
    <div id="main">
        <div id="left"></div>
        <div id="right"></div>
    </div>
</body>
</html>

4、直接设置右侧元素的margin-left和margin-top(为负)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        #left{
            width: 300px;
            background: #ff0000;
            height: 400px;
        }
        #right{
            background: #00ff00;
            height: 400px;
            margin-left: 310px;
            margin-top: -400px;
        }

    </style>
</head>
<body>
    <div id="left"></div>
    <div id="right"></div>
    <h1>hhhh</h1>
</body>
</html>

5、利用表格让两个块级元素在一行
display:table-cell属性指让标签元素以表格单元格的形式呈现,类似于td标签。目前IE8+以及其他现代浏览器都是支持此属性(这个之前没有接触过,第一次用)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        #left{
            width: 300px;
            background: #ff0000;
            height: 400px;
            display: table-cell;
        }
        #right{
            background: #00ff00;
            height: 400px;
            display: table-cell;
        }
        #main{
            display: table;
            width: 100%;
        }
    </style>
</head>
<body>
    <div id="main">
        <div id="left"></div>
        <div id="right"></div>
    </div>
</body>
</html>

二、 三列布局,左右固定,中间自适应

1、左边元素左浮,右边元素右浮,中间元素设置margin:上下 左右;使它居中

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        #left, #right{
            width: 300px;
            height: 400px;
            background: #ff0000;
        }
        #left{
            float: left;
        }
        #right{
            float: right;
        }
        #center{
            height: 400px;
            background: #00ff00;
            margin: 0 310px;
        }
    </style>
</head>
<body>
    <div id="left"></div>
    <div id="right"></div>
    <div id="center"></div>
</body>
</html>

2、利用弹性盒模型,别忘了让父元素允许被“分”

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        #left, #right{
            width: 300px;
            height: 400px;
            background: #ff0000;
        }
        #center{
            height: 400px;
            background: #00ff00;
            flex-grow: 1;
        }
        #main{
            display: flex;
        }
    </style>
</head>
<body>
    <div id="main">
        <div id="left"></div>
        <div id="center"></div>
        <div id="right"></div>
    </div>
</body>
</html>

3、给左右的两个元素都定位

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        #left{
            left: 0;
            top: 0;
        }
        #right{
            right: 0;
            top: 0;
        }
        #left, #right{
            width: 300px;
            height: 400px;
            background: #ff0000;
            position: absolute;
        }
        #center{
            height: 400px;
            background: #00ff00;
            margin: 0 310px;
        }
    </style>
</head>
<body>
    <div id="left"></div>
    <div id="center"></div>
    <div id="right"></div>
</body>
</html>

三、三行布局,上下固定,中间自适应

1、上下两部分固定定位、中间绝对定位

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        #top, #bottom{
            height: 100px;
            background: #ff0000;
            position: fixed;
            width: 100%;
        }
        #top{
            top: 0;
        }
        #bottom{
            bottom: 0;
        }
        #center{
            background: #cccccc;
            position: absolute;
            top: 100px;
            bottom: 100px;
            left: 0;
            right: 0;
            overflow: scroll;   滚动条
        }
    </style>
</head>
<body>
    <div id="top">top</div>
    <div id="center">
        center<br>
        center<br>
        center<br>
        center<br>
        center<br>
        center<br>
        center<br>
        center<br>
        center<br>
        center<br>
        center<br>
        center<br>
        center<br>
        center<br>
        center<br>
        center<br>
        center<br>
        center<br>
        center<br>
        center<br>
        center<br>
        center<br>
        center<br>
        center<br>
        center<br>
        center<br>
        center<br>
        center<br>
        center<br>
        center<br>
        center<br>
        center<br>
    </div>
    <div id="bottom">bottom</div>
</body>
</html>

2、弹性盒模型

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        body, html, #main{
            height: 100%;
        }
        #top, #bottom{
            height: 100px;
            background: #ff0000;
        }

        #center{
            background: #cccccc;
            flex-grow: 1;
            overflow: scroll;
        }
        #main{
            display: flex;
            flex-direction: column;
        }
    </style>
</head>
<body>
    <div id="main">
        <div id="top">top</div>
        <div id="center">
             center
             center<br>
             center<br>
             center<br>
             center<br>
             center<br>
             center<br>
             center<br>
             center<br>
             center<br>
             center<br>
             center<br>
             center<br>
             center<br>
             center<br>
             center<br>
             center<br>
             center<br>
             center<br>
             center<br>
             center<br>
             center<br>
             center<br>
             center<br>
             center<br>
             center<br>
             center<br>
             center<br>
             center<br>
             center<br>
             center<br>
             center<br>
             center<br>
        </div>
        <div id="bottom">bottom</div>
    </div>
</body>
</html>
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值