实现圣杯布局(双飞翼布局)方式

132 阅读1分钟

实现圣杯布局(双飞翼布局)方式

圣杯布局(双飞翼布局):左右两边盒子的宽度固定不会随着屏幕大小的改变而改变,中间盒子宽度自适应

方法一:利用定位

思路:左右两边盒子绝对定位,中间盒子给长度为两边盒子宽度为的padding
<!DOCTYPE html>
<html lang="en"><head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
​
        .box {
            position: relative;
            width:100%;
            height:400px;
        }
​
        .center {
            height: 400px;
            padding: 0 100px;
            background-color: orange;
        }
​
        .left,
        .right {
            position: absolute;
            top: 0;
            width: 100px;
            height: 400px;
            background-color: pink;
        }
​
        .left {
            left: 0;
        }
​
        .right {
            right: 0;
        }
    </style>
</head><body>
​
    <div class="box">
        <div class="left"></div>
        <div class="center"></div>
        <div class="right"></div>
    </div>
</body></html>

方法二、利用flex

思路:大盒子也就是父盒子设置flex,左右两边盒子给宽度,中间盒子设置flex1
<!DOCTYPE html>
<html><head>
    <meta charset="utf-8">
    <title></title>
    <style type="text/css">
        * {
            margin: 0;
            padding: 0;
        }
​
        .box {
            display: flex;
            width: 100%;
            height: 400px;
        }
​
        .left,
        .right {
            width: 100px;
            height: 400px;
            background-color: pink;
        }
​
        .center {
            flex: 1;
            background-color: oranges;
        }
    </style>
</head>
<body>
    <div class="box">
        <div class="left"></div>
        <div class="center"></div>
        <div class="right"></div>
    </div>
</body></html>

\