使用CSS文本溢出来改变设置高度的元素内的文本行数

前端之家收集整理的这篇文章主要介绍了使用CSS文本溢出来改变设置高度的元素内的文本行数前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我现在深入一些iPhone(Safari / WebKit)的Web应用程序开发,并希望具有标题文字和正文文本的高度项目,以便始终显示3行。如果标题文本很短,则应显示2行正文。如果标题文本很长,最多应该占用2行,并为身体文本留下1行。每当文本被截断时,它应该显示一个省略号作为最后一个字符。

我已经提出了以下的一切,除了它不显示省略号,我需要的一切。有没有办法得到这个解决方案来满足最后的要求?

Code below,as rendered by Safari http://segdeha.com/assets/imgs/stack-ellipsis.png

<!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,but otherwise 
                should only show 1 line.
            </p>
        </div>
    </body>
</html>

@R_301_323@

这个问题的一个解决方案是淡出文本而不是添加一个省略号。如果这是您可接受的替代方案,请继续阅读。

由于线高度固定为16px,您可以使用一个简单的png图像,其中包含从透明到相关背景颜色的渐变(或使用css3渐变),并将其放置在段落的右下角。

如果您从顶部放置渐变图像16px,这样它将仅在标题达到两行(感谢溢出隐藏)时才可见),标题也同样适用。

原文链接:https://www.f2er.com/css/219884.html

猜你在找的CSS相关文章