【问题标题】:Cause first div to overflow based on width of second div导致第一个 div 基于第二个 div 的宽度溢出
【发布时间】:2017-05-03 13:25:18
【问题描述】:

我有两个并排放置的 div 元素。我希望左 div 占用右 div 宽度的剩余空间。

.text {
  display: inline;
  text-overflow: ellipsis;
  white-space: nowrap;
  overflow: hidden;
  background-color: red;
}

.icon {
  display: inline-block;
  width: 92px;
  background-color: green;
}

.information {
  width: 200px;
}
<div class="information">
  <div class="text">Text Here</div>
  <div class="icon">Image Here</div>
</div>

我尝试过的一切都会导致图标 div 根据文本剩余的空间调整大小/溢出。

但如果图标 div 的宽度更大,我希望我的文本 div 溢出。有没有办法做到这一点?

我无法设置宽度(使用 less 或 calc(totalWidth - iconWidth)) 对于文本 div,因为图标 div 的宽度可以变化,我不知道它的值可能是什么(图标宽度的值是在 mixin 中计算的)。

小提琴链接:https://jsfiddle.net/fdur0wd1/

【问题讨论】:

    标签: html css less


    【解决方案1】:

    在父级上使用flex,并将.text 设置为flex-grow: 1(或简称flex: 1 0 0

    .text {
        text-overflow: ellipsis;
        white-space: nowrap;
        overflow: hidden;
        background-color: red;
        flex: 1 0 0;
    }
    
    .icon {
        width: 92px;
        background-color: green;
    }
    
    .information {
        width: 200px;
        display: flex;
    }
    <div class="information">
        <div class="text">Text Here</div>
        <div class="icon">Image Here</div>
    </div>

    【讨论】:

    • 完全按照我的预期工作。但我什至不必做 flex-grow。只是让父 flex 的 display 属性解决了我的问题。谢谢你的回答。我会阅读更多关于 flex 的内容。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-25
    • 1970-01-01
    • 1970-01-01
    • 2011-08-18
    • 2012-11-25
    • 2020-04-02
    • 2015-04-29
    相关资源
    最近更新 更多