If you want to apply ellipsis (...) to a single line of text, CSS makes that somewhat easy with the text-overflow
property. It's still a bit tricky (due to all the requirements – see below), but text-overflow
makes it possible and reliable.
If, however, you want to use ellipsis on multiline text – as would be the case here – then don't expect to have any fun. CSS has no standard method for doing this, and the workarounds are hit and miss.
Ellipsis for Single Line Text
With text-overflow
, ellipsis can be applied to a single line of text. The following CSS requirements must be met:
- must have a
width
, max-width
or flex-basis
- must have
white-space: nowrap
- must have
overflow
with value other than visible
- must be
display: block
or inline-block
(or the functional equivalent, such as a flex item).
So this will work:
p {
width: 200px;
white-space: nowrap;
overflow: hidden;
display: inline-block;
text-overflow: ellipsis;
border: 1px solid #ddd;
margin: 0;
}
<p>
This is a test of CSS <i>text-overflow: ellipsis</i>.
This is a test of CSS <i>text-overflow: ellipsis</i>.
This is a test of CSS <i>text-overflow: ellipsis</i>.
This is a test of CSS <i>text-overflow: ellipsis</i>.
This is a test of CSS <i>text-overflow: ellipsis</i>.
This is a test of CSS <i>text-overflow: ellipsis</i>.
</p>
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…