我在 <H4>
中有一个标题<header>
内的文字在 wordpress 中标记。我希望如果标题太长(不适合 <header>
宽度),文本将被截断并在其位置添加“...”。
例如标题(取自 ipsum 生成器):
Audiam tritani prompta mel ex
应该看起来像:
Audiam tritani promp...
尝试使用 overflow 属性但没有成功..
最佳答案
通过使用 text-overflow: ellipsis
h4 {
width: /** your width **/
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
使用这些将根据您的宽度截断您的长文本,并在其上添加一个 ...
。
这是来自 W3Schools 的实时代码:
<!DOCTYPE html>
<html>
<head>
<style>
#div1 {
white-space: nowrap;
width: 12em;
overflow: hidden;
text-overflow: clip;
border: 1px solid #000000;
}
#div2 {
white-space: nowrap;
width: 12em;
overflow: hidden;
text-overflow: ellipsis;
border: 1px solid #000000;
}
</style>
</head>
<body>
<p>The following two divs contains a long text that will not fit in the box. As you can see, the text is clipped.</p>
<p>This div uses "text-overflow:clip":</p>
<div id="div1">This is some long text that will not fit in the box</div>
<p>This div uses "text-overflow:ellipsis":</p>
<div id="div2">This is some long text that will not fit in the box</div>
</body>
</html>
在 W3Schools: text-overflow 中查看更多信息.
关于html - 如果太长如何打断H4文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39175861/