Multi-line text-overflow:ellipsis in CSS or JS, with img tags

你。 提交于 2019-12-04 22:33:20

问题


I tried using :

  • text-overflow ellipsis feature in CSS3 (but doesn't support multi-line)
  • several jquery plugins like dotdotdot (http://dotdotdot.frebsite.nl/)
  • jquery autoellipsis (http://pvdspek.github.com/jquery.autoellipsis/).

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 :-).


回答1:


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>



回答2:


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



来源:https://stackoverflow.com/questions/8749590/multi-line-text-overflowellipsis-in-css-or-js-with-img-tags

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!