Limit text length to n lines using CSS

后端 未结 13 978
滥情空心
滥情空心 2020-11-22 02:53

Is it possible to limit a text length to \"n\" lines using CSS (or cut it when overflows vertically).

text-overflow: ellipsis; only works for 1 line tex

相关标签:
13条回答
  • 2020-11-22 02:55

    What you can do is the following:

    .max-lines {
      display: block;/* or inline-block */
      text-overflow: ellipsis;
      word-wrap: break-word;
      overflow: hidden;
      max-height: 3.6em;
      line-height: 1.8em;
    }
    <p class="max-lines">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc vitae leo dapibus, accumsan lorem eleifend, pharetra quam. Quisque vestibulum commodo justo, eleifend mollis enim blandit eu. Aenean hendrerit nisl et elit maximus finibus. Suspendisse scelerisque consectetur nisl mollis scelerisque.</p>

    where max-height: = line-height: × <number-of-lines> in em.

    0 讨论(0)
  • 2020-11-22 03:00

    As far as I can see, this would be possible only using height: (some em value); overflow: hidden and even then it wouldn't have the fancy ... at the end.

    If that is not an option, I think it's impossible without some server side pre-processing (difficult because text flow is impossible to predict reliably) or jQuery (possible but probably complicated).

    0 讨论(0)
  • 2020-11-22 03:02

    There's a way to do it using unofficial line-clamp syntax, and starting with Firefox 68 it works in all major browsers.

    body {
       margin: 20px;
    }
    
    .text {
       overflow: hidden;
       text-overflow: ellipsis;
       display: -webkit-box;
       -webkit-line-clamp: 2; /* number of lines to show */
       -webkit-box-orient: vertical;
    }
    <div class="text">
    Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam consectetur venenatis blandit. Praesent vehicula, libero non pretium vulputate, lacus arcu facilisis lectus, sed feugiat tellus nulla eu dolor. Nulla porta bibendum lectus quis euismod. Aliquam volutpat ultricies porttitor. Cras risus nisi, accumsan vel cursus ut, sollicitudin vitae dolor. Fusce scelerisque eleifend lectus in bibendum. Suspendisse lacinia egestas felis a volutpat.
    </div>

    Unless you care about IE users, there is no need to do line-height and max-height fallbacks.

    0 讨论(0)
  • 2020-11-22 03:03

    following CSS class helped me in getting two line ellipsis.

      .two-line-ellipsis {
            padding-left:2vw;
            text-overflow: ellipsis;
            overflow: hidden;
            width: 325px;
            line-height: 25px;
            display: -webkit-box;
            -webkit-line-clamp: 2;
            -webkit-box-orient: vertical;
            padding-top: 15px;
        }
    
    0 讨论(0)
  • 2020-11-22 03:03

    Currently you can't, but in future you will be able to use text-overflow:ellipis-lastline. Currently it's available with vendor prefix in Opera 10.60+: example

    0 讨论(0)
  • 2020-11-22 03:05

    The solution from this thread is to use the jquery plugin dotdotdot. Not a CSS solution, but it gives you a lot of options for "read more" links, dynamic resizing etc.

    0 讨论(0)
提交回复
热议问题