【问题标题】:CSS: Text overflow ellipsis if large than two lines, is it possible?CSS:如果大于两行,则文本溢出省略号,这可能吗?
【发布时间】:2014-05-09 14:58:32
【问题描述】:

我有一个宽度为 300 像素、高度为 20 像素的 div,其中的文本随机变化。我只想在溢出 2 行时显示省略号,否则仅使用 CSS。

http://jsfiddle.net/7BXtr/272/

HTML:

<div id="container">
            <div class="box">

        <div class="text-body">
            <div class="text-inner">This is the text description of the item.
                It can flow onto more than 2 lines, too,
                if there is room for it, but otherwise
                should only show 1 line.
            </div>
        </div>
    </div>
</div>

CSS:

#container {
    width: 104px;
}
.box {

    max-height: 40px;
    overflow: hidden;

    border: solid 1px red;
    position: relative;
}
.box:after {
    content: '...';
    position: absolute;
    left: 84px;
    bottom: 0;
}
.text-body {
    position: relative;
}
.text-body:before {
    content: '';
    position: absolute;
    left: 80px;
    top: 14px;
    width: 12px;
    height: 2px;
    background: white;
}
.text-inner {
    width: 90px;
    padding-right: 10px;
}

【问题讨论】:

  • 不——不幸的是,这在 CSS 中是不可能的,你需要一个 JS 解决方案

标签: css


【解决方案1】:

Webkit 可以设置多于一行的文本溢出省略号,其他厂商暂不支持

下面是一个示例,其中包含三行带有椭圆 example 的文本:

.three-line-block {
    line-height: 15px; //for not webkit browser
    max-height: 45px; //for not webkit browser
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-line-clamp: 3;
    -webkit-box-orient: vertical;
}

【讨论】:

  • 代码中的cmets是什么意思? webkit 和其他供应商都支持这两条线...
猜你喜欢
  • 2013-04-01
  • 2015-10-20
  • 1970-01-01
  • 1970-01-01
  • 2012-07-30
  • 2018-07-22
  • 1970-01-01
  • 2012-04-13
  • 2013-07-20
相关资源
最近更新 更多