多行文本溢出:CSS或JS中的省略号,带有img标签

发布于 2024-12-24 20:49:56 字数 465 浏览 2 评论 0原文

我尝试使用:

所有这些工具都工作得很好,但如果内容包含图像,则使用 dotdotdot 或 jquery.autoellipsis 计算的截断高度是错误的。

我只是想知道是否有人有一个好主意来处理这个问题(也许是一些服务器端处理?),提前致谢:-)。

I tried using :

All of these tools work quite well but if content has images the calculated height for truncation with dotdotdot or jquery.autoellipsis is wrong.

I was just wondering if someone has a great idea for dealing with this (maybe some server-side processing on ?), Thanks by advance :-).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

深巷少女 2024-12-31 20:49:56

通过为多行 div 设置固定高度,然后绝对定位 img 和省略号来使用您自己的省略号定位偏移以简化脚本。右偏移量特定于每个句子的特定字母的font-size 和字距调整,因此除非使用等宽字体,否则需要反复试验。基本结构是这样的:

<style type="text/css">
.truncate { position: absolute; top: 20px; right: 6px; background-color: #fff; }
.lineup { top: 6px; }
.monalisa { position: absolute; top: 2px; left: -18px; }
.wrapper { position: relative; width: 360px }
.textblob { width: 330px; height: 30px; }
</style>
<!--...-->
<div class="wrapper">
  <img class="monalisa" src="hppt://www.stackoverflow.com/favicon.ico" alt="SO"/>
  <div class="textblob">
    <span class="truncate">…</span>
    <span class="snippet">blah blah blahblah blah blahblah blah blahblah blah blahblah blah blahblah blah blahblah blah blahblah blah blahblah blah blahblah blah blah</span>
  </div>
</div>

Use your own ellipsis positioning offsets by setting a fixed height for the multi-line div, then absolutely positioning the img and the ellipsis to simplify the script. The right offset is specific to the font-size and kerning of the particular letters of each sentence, so trial and error is necessary unless a monospace font is used. The basic structure is something like this:

<style type="text/css">
.truncate { position: absolute; top: 20px; right: 6px; background-color: #fff; }
.lineup { top: 6px; }
.monalisa { position: absolute; top: 2px; left: -18px; }
.wrapper { position: relative; width: 360px }
.textblob { width: 330px; height: 30px; }
</style>
<!--...-->
<div class="wrapper">
  <img class="monalisa" src="hppt://www.stackoverflow.com/favicon.ico" alt="SO"/>
  <div class="textblob">
    <span class="truncate">…</span>
    <span class="snippet">blah blah blahblah blah blahblah blah blahblah blah blahblah blah blahblah blah blahblah blah blahblah blah blahblah blah blahblah blah blah</span>
  </div>
</div>
初心 2024-12-31 20:49:56

如果您需要 X 浏览器支持(不仅适用于 Opera 和 Webkit,如 @c69 所解释的)。

我发现了一个更奇特的方法。
也可进行手动调节。

看一下 jsfiddle 上的工作示例。

HTML

<p>
    Lorem ipsum dolor sit amet, consectetur
</p>

CSS

p {
    height: 3.7em;
    position: relative;
    overflow: hidden;
    width: 235px;
}

p:after{
    /* works since IE10 , ff16, chrome26, safari6.1,opera12, android4.4 ; previouse browser need prefixes */
    background: linear-gradient(to right,  rgba(255,255,255,0) 0%,rgba(255,2055,255,1) 30%); 
    /* for IE9,IE8 */
    filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#00ffffff', endColorstr='#ffffff',GradientType=1 ); 
    bottom: 0;
    content: "...";
    float:right;
    padding-left: 10px;
    position: absolute;
    right: 0;
}

来源:
1 mobify
2 个 css-tricks

If you need X-Browser support(not only for Opera and Webkit, like @c69 explained).

I found a more fancy way.
But also for manually adjustment.

take a look at a working example on jsfiddle.

HTML

<p>
    Lorem ipsum dolor sit amet, consectetur
</p>

css

p {
    height: 3.7em;
    position: relative;
    overflow: hidden;
    width: 235px;
}

p:after{
    /* works since IE10 , ff16, chrome26, safari6.1,opera12, android4.4 ; previouse browser need prefixes */
    background: linear-gradient(to right,  rgba(255,255,255,0) 0%,rgba(255,2055,255,1) 30%); 
    /* for IE9,IE8 */
    filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#00ffffff', endColorstr='#ffffff',GradientType=1 ); 
    bottom: 0;
    content: "...";
    float:right;
    padding-left: 10px;
    position: absolute;
    right: 0;
}

Source:
1 mobify
2 css-tricks

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文