<div style="display:inline-block;width:100px;">
very long text
</div>
任何使用纯css的方法都可以剪切太长的文本,而不是在下一行显示,并且最多显示100px
Answers:
您可以使用:
overflow:hidden;
隐藏区域外的文本。
请注意,它可能会剪切最后一个字母(因此,最后一个字母的一部分仍会显示)。更好的方法是在末尾显示省略号。您可以使用text-overflow
:
overflow: hidden;
white-space: nowrap; /* Don't forget this one */
text-overflow: ellipsis;
title="full text goes here"
在html中使用。
.crop {
overflow:hidden;
white-space:nowrap;
text-overflow:ellipsis;
width:100px;
}
为什么不使用相对单位?
.cropText {
max-width: 20em;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
下面的代码将隐藏您决定的固定宽度的文本。但不太适合响应式设计。
.CropLongTexts {
width: 170px;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
更新资料
我注意到在(移动)设备中,由于(固定宽度)而使文本(混合)彼此混合...所以我编辑了上面的代码,以如下方式被隐藏:
.CropLongTexts {
max-width: 170px;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
(最大宽度)确保无论(屏幕大小)如何,文本都将有效地隐藏,并且不会彼此混合。