html - 如果太长如何打断H4文本

标签 html css wordpress

我在 <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/

相关文章:

javascript - 将网站的一部分嵌入到另一个网站

html - 基于列的表格可以在 html 和/或 css 中使用吗

javascript - console.log 和 console.dir 之间有什么区别?

html - 第三个div自动 float

css - slider 响应

php - Wordpress 对两个表的自定义查询以获取分层数据

php - 如何显示自定义帖子类型的其他字段并显示特色缩略图选项?

html - 第一个单元格比其余单元格宽的表格

jquery - 在鼠标悬停时显示 PNG 的不同部分

javascript - 单击选项卡时不加载隐藏图像或加载内容