我有一个网格布局,就像下面 header
中的那个。 div 中有一个文本,其宽度为 1fr
。我希望该 div 中的文本在太长时被截断。添加 text-overflow
作为 ellpsis
不起作用。知道怎么做吗?
它必须是网格,我无法改变它。
html,
body {
height: 100%;
width: 100%;
overflow: hidden;
}
body {
display: flex;
}
.container {
display: flex;
flex-direction: column;
flex: 1;
}
content {
flex: 1;
background-color: red;
}
header {
background-color: limegreen;
display: grid;
grid-template-columns: 0fr 1fr 0fr;
}
header div {
border: 1px solid orange;
}
.long {
text-overflow: ellipsis;
}
<div class="container">
<header>
<div>Left</div>
<div class="long">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</div>
<div>Right</div>
</header>
<content>
</content>
</div>
原文由 nullDev 发布,翻译遵循 CC BY-SA 4.0 许可协议
将 .long css 更改为以下
您需要添加空格和溢出的原因如下:
white-space: nowrap
告诉浏览器在文本长度超过包含块宽度时不换行,white-space
属性默认值是正常的,表示它可以换行,所以不会存在这种情况文本溢出的地方;overflow
默认值可见,当文本超出包含块时,它只是显示,所以不需要溢出文本显示,只有当你设置overflow
到hidden
.然后,当文本不能完全显示时,它将使用text-overflow
属性。