【CSS布局】左边宽度固定右边宽度自适应

本文介绍了三种常见的网页布局技术:1) 使用float属性实现左右布局;2) 利用Flexbox布局,通过设置display: flex和flex属性调整元素位置;3) 结合Flex和min-width属性,确保元素宽度的响应性。这些技术对于创建响应式和灵活的网页设计至关重要。
摘要由CSDN通过智能技术生成

布局

<div id="root">
    <div class="left"></div>
    <div class="right"></div>
</div>

1. float浮动

<!-- 样式 -->
<style>
    #root {
        width: 800px;
        height: 500px;
        background-color: #ccc;
    }
    .left {
        float: left;
        width: 200px;
        height: 500px;
        background-color: pink;
    }
    .right {
        height: 500px;
        margin-left: 200px;
        background-color: #3cd;
    }
</style>

2. flex布局

父元素设置display flex
左元素设置flex-basis
右元素设置flex-grow或flex为1
<style>
    #root {
        display: flex;
        width: 800px;
        height: 500px;
        background-color: #ccc;
    }

    .left {
        flex-basis: 200px;
        height: 500px;
        background-color: pink;
    }

    .right {
        flex-grow: 1;
        height: 500px;
        background-color: #3cd;
    }
</style>

3. flex和min-width

<style>
    #root {
        display: flex;
        width: 800px;
        height: 500px;
        background-color: #ccc;
    }

    .left {
        min-width: 200px;
        height: 500px;
        background-color: pink;
    }

    .right {
        width: 100%;
        height: 500px;
        background-color: #3cd;
    }
</style>

只有设置min-width才可以使用width:100%

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值