在display: flex
上没有.container
的情况下,减小容器大小时文本会正确地截断。
但是绿色的块和文本不再相邻了,因为容器没有display: flex
。当我将其添加到容器中时,绿色块和文本已正确对齐,但文本不再被截断(省略了省略号)。
我如何使用flex来确保块和文本对齐,同时在动态宽度容器中将文本用省略号截断?
body {
padding: 20px;
}
.container {
display: flex;
}
.block {
width: 25px;
height: 25px;
padding: 10px;
background-color: yellowgreen;
display: flex;
align-items: center;
justify-content: center;
margin-right: 20px
}
.text {
display: flex;
}
.truncate {
flex: 1;
min-width: 0;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
font-size: 20pt;
}
<div class="container">
<div class="block">B</div>
<div class="text">
<div class="truncate">
3. This is a long string that is OK to truncate please and thank you
</div>
</div>
</div>
答案 0 :(得分:1)
您的带省略号的弹性项目必须能够收缩到内容的宽度以下。有关完整的详细信息,请参见我的回答:Why don't flex items shrink past content size?
body {
padding: 20px;
}
.container {
display: flex;
border: 1px dashed red; /* demo */
}
.block {
flex: 0 0 25px; /* flex-grow, flex-shrink, flex-basis */
/* set flex-shrink to 0 to prevent item from shrinking below 25px */
padding: 10px;
background-color: yellowgreen;
display: flex;
align-items: center;
justify-content: center;
margin-right: 20px;
}
.text {
display: flex;
min-width: 0; /* allow item to shrink below content size */
}
.truncate {
flex: 1;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
font-size: 20pt;
background-color: yellow; /* demo */
}
<div class="container">
<div class="block">B</div>
<div class="text">
<div class="truncate">
3. This is a long string that is OK to truncate please and thank you
</div>
</div>
</div>