左右定宽,中间自适应布局三种方式

圣杯布局(结构简单,理解绕弯)

原理:外盒子的padding+ (left right)的相对定位

    <style>
        body{
            color:white;
            min-width: 900px;
        }
        .center{
            width: 100%;
            height: 300px;
            background-color:teal;      
        }
        .left{
            width: 200px;
            height: 300px;
            background-color: red;
            margin-left: -100%;
             position: relative;
            left: -200px; 
        }
        .right{
            width: 200px;
            height: 300px;
            background-color: blue;
            margin-left: -200px;
             position: relative;
            right: -200px; 
        }
        .center,.left,.right{
            float: left;
        }
        .clear::before{
            clear: both;
            display: block;
            content: "";
        }
        .clear{
            padding: 0 200px;
        }
    </style>


<body>
    <div class="clear">
        <div class="center">
            中间
        </div>
        <div class="left">
            左边
        </div>
        <div class="right">
            右边
        </div>
    </div>

</body>

步骤:
1.在主体内部外面嵌套了一个div
2.center在结构上靠前
3.center,left,right都浮动
4.清除浮动
(给嵌套的主题外部加一个伪元素::after~在**之后,然后清除浮动)
5.center宽度为100%,left,right宽度固定
6.left通过margin-left为负值(-100%),移动到center最左边
7.right通过margin-left为负值(自身的宽度),移动到center的最右边。
8.center的内容左边和右边会被left,right遮盖
9.最外层div添加一个padding,padding的值为left,right的宽度。
10.left,right相对定位,移动到相应的位置。位移量为自身的宽度。

双飞翼布局(结构较绕)

原理:用了中间盒子的里面盒子的margin值,然后左右两边用margin-left顶开

    <style>
        .center {
            width: 100%;
            height: 300px;
            background-color: teal;
            float: left;
        }
        .left {
            width: 200px;
            height: 300px;
            background-color: red;
            margin-left: -100%;
        }
        .right {
            width: 200px;
            height: 300px;
            background-color: blue;
            margin-left: -200px;
        }
        .center,
        .left,
        .right {
            float: left;
        }
        .home {
            margin: 0 200px;
        }   
    </style>


<body>
    <div class="center">
        <div class="home">
            中间
        </div>
    </div>
    <div class="left">
        左边
    </div>
    <div class="right">
        右边
    </div>
</body>

步骤:
1.让left right center浮动
2.设置left的margin-left为-100%
3.设置right的margin-left为负的自身宽度
4.在center中添加一个div,将内容写在这个div中
5.为这个div添加一个margin,左右margin的值为left right的单个宽度。

弹性盒子实现三栏(结构简单,理解复杂)

原理:用了中间盒子的flex-grow属性,然后用了order属性来改变显示位置

    <style>
        .home{
            display: flex;
        }
        .left,.right{
            width: 200px;
            height: 300px;
        }
        .left{
             order: 1; 
            background-color:blue;
        }
        .right{
            order: 3;
            background-color: red;
        }
        .center{    
            order: 2;
             flex-grow: 1; 
            background-color:green;
        }

    </style>

<body>
    <div class="home">
        <div class="center">
            中间
        </div>
        <div class="left">
            左边
        </div>
        <div class="right">
            右边
        </div>
    </div>

</body>

步骤:
1.添加一个最外层的div 暂起名为 home
2.将home设置为弹性盒子(display: flex)
3.left right 固定宽度 center不设置宽度
4.将center设置为 flex-grow: 1
(用于将弹性盒子的可用空间按照比例分配给弹性元素);
5.调整显示顺序,left: order: 1; center: order: 2 right;: order:3;

上述三种方法都能实现左右定宽,中间自适应的效果
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值