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



我很难获得text-overflow: ellipsisoverflow: hidden按照我需要的方式工作。

基本上,我需要让带有类item1和文本"请截断我"的左侧div 随着容器宽度的减小而缩小,以便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覆盖。但不是全部。

此外,弹性和表属性不能很好地协同工作。混合它们可能会破坏弹性布局,这似乎发生在您的案例中。

通过以下调整,您的布局似乎有效。

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

修订后的码笔

更多信息:

  • 为什么弹性项目不会缩小超过内容大小?

(如果不是因为display: table问题,这篇文章将是此链接的副本。

最新更新