css--两栏布局易懂的5种方法

1.左侧固定,右侧自适应

<style>
    .left {
        width: 200px;
        height: 600px;
        background-color: rgb(247, 10, 10);
        float: left;
    }
    .right {
        height: 600px;
        margin-left: 200px;
       background-color: blue;
    }

</style>
 
<body>
   <div class="box">
       <div class="left"></div>
       <div class="right"></div>
   </div>

</body>

    2.浮动+BFC

<style>
    .left {
        width: 200px;
        height: 600px;
        background-color: rgb(247, 10, 10);
        float: left;
    }
    .right {
        height: 600px;
       /* 触发BFC */
       overflow: hidden;
       background-color: blue;
    }
</style>
 <body>
   <div class="box">
       <div class="left"></div>
       <div class="right"></div>
   </div>
</body>

   3.  定位  子绝父相

<style>
    .box {
        position: relative;
    }

    .left {
        position: absolute;
        left: 0;
        width: 200px;
        height: 600px;
        background-color: rgb(247, 10, 10);
        float: left;
    }

    .right {
        height: 600px;
        margin-left: 200px;
        background-color: blue;
    }
</style>

<body>
    <div class="box">
        <div class="left"></div>
        <div class="right"></div>
    </div>
</body>

   4.定位 -子绝父相 不使用 margin

  左侧右侧都使用定位

<style>
    .box {
        position: relative;
    }

    .left {
        position: absolute;
        left: 0;
        width: 200px;
        height: 600px;
        background-color: rgb(247, 10, 10);
        float: left;
    }

    .right {
        position: absolute;
        height: 600px;
        left: 200px;
        right: 0;
        background-color: blue;
    }
</style>

<body>
    <div class="box">
        <div class="left"></div>
        <div class="right"></div>
    </div>
</body>

   5.flex布局

<style>
    .box {
        /* 将所以元素排成一排 */
        display: flex;
    }

    .left {
        width: 200px;
        height: 600px;
        background-color: rgb(247, 10, 10);
        float: left;
    }

    .right {
        /* 将flex设置为1,右盒子会将剩余空间填满 */
        flex: 1;
        height: 600px;
        background-color: blue;
    }
</style>

<body>
    <div class="box">
        <div class="left"></div>
        <div class="right"></div>
    </div>
</body>


样式

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值