[CSS面试题] 如何使得左右两边没有确定宽度的元素保持其中文本的自适应

image.png

需要实际的代码, 而不是解题思路 :

<div class="bg">
    <div>
        <span>文案文案文案文案文案文案文案文案文案文案文案文案文案文案</span>
    </div>
    
    <div>
        <button>按钮按钮</button>
    </div>
</div>
阅读 3.1k
5 个回答

我也来试试 :

<style type="text/css">
    .bg {
        display: flex;
        justify-content: space-between;
        width: 440px;
        background-color: beige;
        align-items: center;
    }


    img {
        width: 44px;
        height: 44px;
    }

    .left {
        flex: 1;
    }

    .right {
        flex: none;
    }
</style>

其中, html为 :

<div class="bg">
    <img src="https://sponsor-static.segmentfault.com/a0178ced5d32d23e1e29fe810c91de02.jpg" alt="">
    <div class="left">
        <span>文案文案文案文案文案文案文案文案</span>
    </div>

    <div class="right">
        <button>按钮按钮</button>
    </div>
</div>
    <style type="text/css">
        .bg {
            display: flex;
            justify-content: space-between;
        }
        .right {
            flex: 1;
        }
    </style>
<div class="bg">
    <div class="left">
        <span>文案文案文案文案文案文案文案文案文案文案文案文案文案文案</span>
    </div>
    
    <div class="right">
        <button>按钮按钮</button>
    </div>
</div>

html:

<div class="outer">
    <img  class="img" width="32" height="32" src="https://avatar-static.segmentfault.com/151/837/1518375183-54f135ef0d731_big64" alt="">
    <div class="text">
    一文搞懂华为ML Ki成一文搞懂华水电费水电费是为ML Ki成一文搞懂华为ML Ki成fgdfgdfgdfgdfgdfgdfgdgdgdfgdfgdfge你每人每年花胡搜按键
    </div>
</div>

css

.outer{
  width: 260px;  
}

.img{
    width: 100px;
    height: 100px;
    float: right;
}

.text{
    display:inline;
}

image

.bg {
  display: flex;
}
.bg > div:nth-of-type(1) {
  flex: 1;
}
新手上路,请多包涵

用两个盒子包裹着左右给个浮动就可以达到,你想要的效果

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题