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

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

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

1.使用定位的方式

让左右两边的盒子绝对定位,左边盒子left:0,右边盒子:right:0,绝对定位盒子不占位置,中间的盒子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;
        }

        .center {
            height: 300px;
            padding: 0 200px;
            background-color: #0a0;
        }

        .left,
        .right {
            position: absolute;
            top: 0;
            width: 200px;
            height: 300px;
            background-color: #6cf;
        }

        .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>

2.使用浮动加calc计算属性

让所有盒子左浮动,左右两边的盒子宽度固定,中间盒子的宽度通过calc计算属性动态计算得出即中间盒子的宽度为100%减去左右两边的盒子总宽度

<!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;
        }

        .left,
        .right {
            float: left;
            width: 200px;
            height: 300px;
            background-color: #6cf;
        }

        .center {
            float: left;
            width: calc(100% - 400px);
            height: 300px;
            background-color: #0a0;
        }
    </style>
</head>

<body>
    <div class="box">
        <div class="left"></div>
        <div class="center">
           内容部分
        </div>
        <div class="right"></div>
    </div>
</body>

</html>

3.使用flex布局(推荐)

通过父元素设置flex布局,让盒子在一行显示,左右两边的盒子固定宽度,而父元素的剩余宽度则为中间盒子的宽度flex:1

<!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 {
            display: flex;

        }

        .left,
        .right {
            width: 200px;
            height: 300px;
            background-color: #6cf;
        }

        .center {
            flex: 1;
            background-color: #0a0;
        }
    </style>
</head>

<body>
    <div class="box">
        <div class="left"></div>
        <div class="center">
           内容部分
        </div>
        <div class="right"></div>
    </div>
</body>

</html>
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值