并列的两个盒子,一个有固定宽度,让另一个占满剩余宽度(grid、calc、外边距、flex)

HTML:

<div class="d">
        <div class="box1"></div>
        <div class="box2"></div>
</div>

CSS:

grid:通过 grid-template-columns:(固定盒子的宽度)px auto 和 grid-column-gap:0 来实现

        * {
            margin: 0;
            padding: 0;
        }

        .d {
            width: 100%;
            height: 200px;

            display: grid;
            grid-template-columns: 200px auto;
            grid-column-gap: 0;
        }

        .box1 {
            height: 100%;
            background-color: rgb(88, 121, 80);
        }

        .box2 {
            height: 100%;
            background-color: rgb(173, 119, 119)
        }

 css计算函数calc():

        1.第一个盒子设置flot:left,第二个盒子设置flot:right,同时第二个盒子的宽度使用计算函数计算

        2.父级设置flex的情况下,第二个盒子的宽度直接使用计算函数即可

    /* 第一个盒子设置flot:left,第二个盒子设置flot:right,同时第二个盒子的宽度使用计算函数计算 */
        * {
            margin: 0;
            padding: 0;
        }

        .d {
            width: 100%;
            height: 200px;
        }

        .box1 {
            float: left;
            width: 200px;
            height: 100%;
            background-color: rgb(88, 121, 80);
        }

        .box2 {
            float: right;
            width: calc(100% - 200px);
            height: 100%;
            background-color: rgb(173, 119, 119)
        }
/* 父级设置flex的情况下,第二个盒子的宽度直接使用计算函数即可 */
        * {
            margin: 0;
            padding: 0;
        }

        .d {
            display: flex;
            width: 100%;
            height: 200px;
        }

        .box1 {
            width: 200px;
            height: 100%;
            background-color: rgb(88, 121, 80);
        }

        .box2 {
            width: calc(100% - 200px);
            height: 100%;
            background-color: rgb(173, 119, 119)
        }

flex/flex-grow:

        * {
            margin: 0;
            padding: 0;
        }

        .d {
            display: flex;
            width: 100%;
            height: 200px;
        }

        .box1 {
            width: 200px;
            height: 100%;
            background-color: rgb(88, 121, 80);
        }

        .box2 {
            /* flex: 1; */
            flex-grow: 1;
            height: 100%;
            background-color: rgb(173, 119, 119)
        }

外边距:给第一个的盒子设置flot:left,给第二个盒子设置一个左外边距,值为第一个盒子的宽度,第二个盒子宽度为auto

        * {
            margin: 0;
            padding: 0;
        }

        .d {
            width: 100%;
            height: 200px;
        }

        .box1 {
            float: left;
            width: 200px;
            height: 100%;
            background-color: rgb(88, 121, 80);
        }

        .box2 {
            margin-left: 200px;
            /* width: auto; */
            height: 100%;
            background-color: rgb(173, 119, 119)
        }

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值