带有溢出和省略号的嵌套弹性容器不起作用

Nested flex containers with overflow and ellipsis not working

我无法让 text-overflow: ellipsisoverflow: hidden 以我需要的方式工作。

基本上,我需要让左边的 div 和 class item1 和文本 "Please truncate me" 随着容器宽度的减小而缩小,这样 item1item2 在同一行。

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

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

.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

弹性项目的初始设置是 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 的问题,这个 post 将是这个 link 的副本。)