5.左侧不动,右侧自适应

一:运用BFC的overflow:hidden

    <style>
        .left {
            width: 100px;
            height: 100px;
            background-color: aqua;
            float: left;
        }

        .main {
            background-color: blue;
            height: 200px;
            overflow: hidden;
            /* 要加 否则自适应元素包括浮动元素 */
        }
    </style>
<body>
    <div class="left">左侧不动</div>
    <div class="main">右侧自适应</div>
</body>

二:给右侧自适应盒子加margin-left:不动盒子的宽度

    <style>
        .left {
            width: 100px;
            height: 100px;
            background-color: aqua;
            float: left;
        }

        .main {
            margin-left: 100px;
            background-color: blue;
            height: 200px;
        }
    </style>
<body>
    <div class="left">左侧不动</div>
    <div class="main">右侧自适应</div>
</body>

三:有一个共同的父元素,给他设置高度500px,设置相对定位;padding-left:不动元素的宽度,,  左侧元素设置宽高并相对定位 top:0;left:0; 右侧元素只设置高度

    <style>
        .father {
            height: 500px;
            position: relative;
            padding-left: 100px;
            background-color: pink;
        }
        .left {
            width: 100px;
            height: 100px;
            background-color: aqua;
            position: absolute;
            top: 0;
            left: 0;
        }
        .main {
            background-color: blue;
            height: 200px;

        }
    </style>
    <div class="father">
        <div class="left">左侧不动</div>
        <div class="main">右侧自适应</div>
    </div>

四:利用display:flex给父元素设置为弹性盒子,左侧宽度写死,右侧占所有剩余空间

容器的高度是根据最大的那个来确定的

    <style>
        .father {
            height: 500px;
            background-color: pink;
            display: flex;
        }

        .left {
            width: 100px;
            height: 100px;
            background-color: aqua;
        }

        .main {
            background-color: blue;
            height: 200px;
            flex: 1;
        }
    </style>
    <div class="father">
        <div class="left">左侧不动</div>
        <div class="main">右侧自适应</div>
    </div>

五:父元素display: table;  子元素 display: table-cell;

    <style>
        .father {
            display: table;
        }
        .left {
            display: table-cell;
            background-color: aqua;
        }
        .main {
            width: 100%;
            display: table-cell;
            background-color: blue;
        }
    </style>
    <div class="father">
        <div class="left">左侧不动</div>
        <div class="main">右侧自适应</div>
    </div>

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值