【问题标题】:Display flex, 1 item fixed width, other item overflow and gets cut off [duplicate]显示 flex,1 个项目固定宽度,其他项目溢出并被切断 [重复]
【发布时间】:2019-04-13 11:43:49
【问题描述】:

我有 2 个项目,1 个是 100px 的固定宽度,另一个填充空间。另一个项目在一行上有一个带有长文本的子项,该子项带有省略号。不幸的是,它溢出了它的父级。我怎样才能让它不超过父母??

问题: https://jsfiddle.net/jv54f6g1/2/

我找到了一种解决方案,但我觉得它并不理想: https://jsfiddle.net/jv54f6g1/3/

代码:

<div class="a">
  <div class="b">
    <p>im a icon</p>
  </div>
  <div class="c">
    <p>im a teapot that is short and stout, here is my handle Lorem ipsum dolor sit amet, consectetur adipisicing elit. Accusamus, mollitia, obcaecati. Amet, modi. Consequuntur sit asperiores veniam, cupiditate dolores iusto illo alias? Cupiditate, optio delectus voluptatum, ducimus nam inventore doloribus.</p>
  </div>
</div>

css

* {
  box-sizing: border-box;
}

.a {
  width: 100%;
  display: flex;
  max-width: 500px;
  border: 1px solid red;
}

.b {
  min-width: 100px;
  display: block;
}

.c {
  flex: 1;
  background: yellow;
  max-width: 100%;

  p {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
}

【问题讨论】:

  • 你需要添加min-width:0,你可以摆脱最大宽度
  • 感谢这个完美的工作

标签: css flexbox display


【解决方案1】:

overflow: hidden; 添加到第二个元素 (.c)

https://jsfiddle.net/e2am9hj0/

【讨论】:

  • 这解决了它,但隐藏了盒子阴影等东西,对我来说合适的解决方案是@temani afif 所说的,使用'min-width:0'
猜你喜欢
  • 2017-12-06
  • 2018-07-30
  • 2019-01-14
  • 2018-06-27
  • 2019-02-28
  • 2013-09-24
  • 2019-01-09
  • 1970-01-01
  • 2015-12-10
相关资源
最近更新 更多