html - 如何使用 CSS 在多行 div 中用省略号截断文本?

标签 html css

如何在多行 div 字段中用省略号截断文本?当 div 是单行时它起作用。我如何让它适用于多行,以便将省略号放在 div 右下角的最后?

<!DOCTYPE html>
<html>
<head>
<style> 
#div1 {
    white-space: nowrap; 
    width: 12em; 
    overflow: hidden;
    text-overflow: ellipsis; 
    border: 1px solid #000000;
}

#div2 {
    /*white-space: nowrap; */
    width: 12em; 
    overflow: hidden;
    text-overflow: ellipsis; 
    border: 1px solid #000000;
    height: 3em;
}

</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 "white-space: nowrap;":</p>
<div id="div1">This is some long text that will not fit in the box</div>

<p>This div uses "height: 3em;":</p>
<div id="div2">This is some long text that will not fit in the box This is some long text that will not fit in the box This is some long text that will not fit in the box</div>

</body>
</html>

最佳答案

这并没有变得太复杂。它似乎不支持跨浏览器。

.description {
  height: 4.5em;
  display: block; /* Fallback for non-webkit */
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
  text-overflow: ellipsis;
}

#div1 {
    white-space: nowrap; 
    width: 12em; 
    overflow: hidden;
    text-overflow: ellipsis; 
    border: 1px solid #000000;
}

#div2 {
    /*white-space: nowrap; */
    width: 12em; 
    overflow: hidden;
    text-overflow: ellipsis; 
    border: 1px solid #000000;
    height: 3em;
}

#div3 {
    width: 12em; 
    border: 1px solid #000000;
    height: 3em;
    line-height: 1em;
  display: block; /* Fallback for non-webkit */
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
  text-overflow: ellipsis;
}
<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 "white-space: nowrap;":</p>
<div id="div1">This is some long text that will not fit in the box</div>

<p>This div uses "height: 3em;":</p>
<div id="div2">This is some long text that will not fit in the box This is some long text that will not fit in the box This is some long text that will not fit in the box</div>

<p>This is the solution.</p>
<div id="div3">This is some long text that will not fit in the box This is some long text that will not fit in the box This is some long text that will not fit in the box</div>

关于html - 如何使用 CSS 在多行 div 中用省略号截断文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47580648/

相关文章:

html - 如何使用没有白色边框的CSS居中图像

html - 侧边栏背景向左展开

javascript - 检测有多少 div 超出窗口高度

javascript - 位置粘性不占用空间

html - 使图像上的倾斜元素透明

jquery - 伪类 :empty and :not(:empty) - make it hide() and show() regarding content (jquery, mvc 5, asp.net, css)

html - IE中的按钮样式

html - 响应式网页设计 - 消失的风格?

javascript - 删除具有特定内容的表 tr

javascript - 使用 javascript 和 canvas 没有在页面上绘制任何内容