【发布时间】:2019-03-26 16:59:47
【问题描述】:
我目前正在深入研究一些 iPhone (Safari/WebKit) 网络应用程序开发,并希望拥有具有标题文本和正文文本的设定高度的项目,以便始终显示 3 行。如果标题文本很短,则应显示 2 行正文。如果标题文本很长,最多占 2 行,正文留 1 行。每当文本被截断时,它应该显示一个省略号作为最后一个字符。
我想出了以下内容,可以满足我的所有需求,只是它不显示省略号。有没有办法让这个解决方案满足最后一个要求?
(来源:segdeha.com)
<!DOCTYPE HTML>
<html>
<head>
<style type="text/css">
#container {
width: 100px;
}
p {
/* white-space: nowrap; */
font-family: Helvetica;
font-size: 12px;
line-height: 16px;
text-overflow: ellipsis;
max-height: 48px;
overflow: hidden;
border: solid 1px red;
}
strong {
/* white-space: nowrap; */
font-size: 16px;
display: block;
text-overflow: ellipsis;
max-height: 32px;
overflow: hidden;
}
</style>
</head>
<body>
<div id="container">
<p>
<strong>Short title</strong>
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.
</p>
<p>
<strong>Long title that will span more
than 2 lines when we're done.</strong>
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.
</p>
</div>
</body>
</html>
【问题讨论】:
-
我在 Safari 中查看了 iphone.facebook.com(欺骗 iPhone 用户代理),看起来他们在服务器端执行此操作。我怀疑我注定要遭受同样的命运。
-
过去我也想做类似的事情。我最终尝试拆分行并添加省略号服务器端。这最终要求我知道文本的宽度和高度(以像素为单位)。最终我沮丧地放弃了。
-
JavaScript 是不可能的吗?
-
太糟糕了 媒体查询不支持这个。如果你能做这样的事情会很棒... @media id="myIdHere" and (max-height:32px) {strong{content:'...';}}
-
您需要指定
white-space:nowrap才能使text-overflow正常工作。这意味着您被限制在一行文本中。所以看起来你想要的东西不能单独使用 CSS 来实现。 :((来源:quirksmode.org/css/textoverflow.html)
标签: css