【问题标题】:Nested flex containers with overflow and ellipsis not working带有溢出和省略号的嵌套弹性容器不起作用
【发布时间】:2017-08-06 14:00:25
【问题描述】:

我无法让 text-overflow: ellipsis 和 overflow: hidden 按我需要的方式工作。

基本上,我需要使用类item1 和文本“请截断我”来缩小左侧的div,以便随着容器宽度的减小而缩小,以便item1 和item2 在同一行上。

无论我尝试什么,最终都会导致行溢出并且它永远不会缩小。

从这里尝试了各种解决方案,但没有按照我需要的方式进行任何工作。

.mainwrapper {
  display: flex;
  justify-content: center;
  width: 100%;
  max-width: 100%;
  height: 100%;
  max-height: 100%;
}

.content {
  display: flex;
  align-items: center;
  justify-content: center;
  margin-top: 20px;
  margin-bottom: 20px;
  max-width: 800px;
  width: 100%;
  height: 400px;
  background-color: red;
}

.top-container {
  display: flex;
  flex-wrap: wrap;
  width: 80%;
  height: 80%;
  background-color: cyan;
}

.title {
  background-color: white;
}

.table-container {
  display: table;
}

.skills-container {
  width: 100%;
  display: block;
  background-color: green;
}

.skill-row {
  width: 100%;
  display: flex;
  justify-content: start;
  background-color: blue;
}

.item1 {
  display: flex;
  flex-wrap: nowrap;
}

.item2 {
  flex-shrink: 0;
  display: flex;
  flex-wrap: nowrap;
}

.item-content {
  display: flex;
}

.item-details {
  display: flex;
}

.text1 {
  display: inline-block;
  white-space: nowrap;
  background-color: yellow;
}

.small-button {
  display: flex;
  height: 50px;
  width: 50px;
  background-color: green;
}

.overflow-toverflow {
  overflow: hidden;
  text-overflow: ellipsis;
}

.flex-w {
  flex-wrap: wrap;
}

.flex-nw {
  flex-wrap: nowrap;
}

.flex-min {
  flex: 1 1 auto;
  min-width: 0;
}

.flex-sh-0 {
  flex-shrink: 0;
}

.min0 {
  min-width: 0;
}
<div class="mainwrapper">
  <div class="content flex-min">
    <div class="top-container flex-min">
      <div class="title">Your skills</div>
      <div class="table-container">
        <div class="skills-container">
          <div class="skill-row flex-nw flex-min">
            <div class="item1 flex-min">
              <div class="item-content">
                <div class="small-button"></div>
                <div class="text1 overflow-toverflow">Please truncate me! Please truncate me!Please truncate me!Please truncate me!Please truncate me!Please truncate me</div>
              </div>
            </div>
            <div class="item2 flex-sh-0">
              <div class="small-button"></div>
              <div class="text1">Relevance: None Whatsoever None</div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

代码笔: https://codepen.io/Tiartyos/pen/Ljxyqr

【问题讨论】:

    标签: html css flexbox ellipsis


    【解决方案1】:

    弹性项目的初始设置是min-width: auto。这意味着,默认情况下,项目不能缩小到其内容的大小以下。这可以防止呈现省略号,因为项目只是扩展以容纳所有内容。

    您的大多数弹性项目都应用了必要的min-width: 0 覆盖。但不是全部。

    另外,flex 和 table 属性不能很好地配合使用。混合它们可能会破坏 flex 布局,这在您的情况下似乎正在发生。

    通过以下调整,您的布局似乎可以正常工作。

    .table-container {
      /* display: table; */
      min-width: 0; /* NEW */
    }
    
    .item-content {
      display: flex;
      min-width: 0; /* NEW */
    }
    

    revised codepen

    更多信息:

    (如果不是因为display: table 问题,这篇文章将与此链接重复。)

    【讨论】:

      猜你喜欢
      • 2017-02-11
      • 1970-01-01
      • 2019-06-19
      • 2017-05-08
      • 2014-06-17
      • 2016-07-15
      • 1970-01-01
      • 2021-02-23
      相关资源
      最近更新 更多