Multi-Line Text overflow display ellipsis (...) All strategy, ellipsis

Source: Internet
Author: User

Multi-Line Text overflow display ellipsis (...) All strategy, ellipsis

Everyone should know how to use it.text-overflow:ellipsisAttribute to display the ellipsis (…) for overflow of a single line of text (...). Of course, some browsers still need to add widthwidthAttribute.

Css code:
  1. Overflow: hidden;
  2. Text-overflow: ellipsis;
  3. White-space: nowrap;

However, this attribute does not support multi-line text overflow to display ellipsis. Here we will introduce several methods based on the Application Scenario to achieve this effect.

WebKit browser or mobile page

The WebKit browser or mobile terminal (mostly WebKit kernel browsers) page implementation is relatively simple, you can directly use the WebKit CSS extension attributes (WebKit is a private attribute)-webkit-line-clampNote: This is an nonstandard property (unsupported WebKit property), which does not appear in the CSS specification draft.

-webkit-line-clampLimit the number of lines of text displayed on a block element. To achieve this effect, it needs to combine other WebKit attributes.
Common attributes:

Css code:
  1. Overflow: hidden;
  2. Text-overflow: ellipsis;
  3. Display:-webkit-box;
  4. -Webkit-line-clamp: 2;
  5. -Webkit-box-orient: vertical;

This attribute is suitable for WebKit browsers or mobile browsers (mostly WebKit kernels.

See the http://www.css88.com/webkit/-webkit-line-clamp/ for specific examples

Cross-browser compatibility

A more reliable and simple method is to set the relative positioning height of the container and use the ellipsis (...) Element simulation;

For example:

Css code:
  1. P {
  2. Position: relative;
  3. Line-height: 1.4em;
  4. /* 3 times the line-height to show 3 lines */
  5. Height: 4.2em;
  6. Overflow: hidden;
  7. }
  8. P: after {
  9. Content :"...";
  10. Font-weight: bold;
  11. Position: absolute;
  12. Bottom: 0;
  13. Right: 0;
  14. Padding: 0 20px 1px 45px;
  15. Background: url (http://css88.b0.upaiyun.com/css88/2014/09/ellipsis_bg.png) repeat-y;
  16. }

Note the following points:

JavaScript Scheme

Js can also be simulated based on the above ideas. The implementation is also very simple. We recommend several mature gadgets for similar work:

1. Clamp. js

Download and documentation address: https://github.com/josephschmitt/Clamp.js
Ease of use:

Js Code:
  1. Var module = document. getElementById ("clamp-this-module ");
  2. $ Clamp (module, {clamp: 3 });

 

2. jQuery plug-in-jQuery. dotdotdot

This is also very convenient to use:

Js Code:
  1. $ (Document). ready (function (){
  2. $ ("# Wrapper"). dotdotdot ({
  3. // Configuration goes here
  4. });
  5. });

Download and detailed documentation URL: Workshop/

Refer:
Http://www.cssmojo.com/line-clamp_for_non_webkit-based_browsers/#what-can-we-do-across-browsers
Http://css-tricks.com/line-clampin/

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.