Multi-Line Text overflow display ellipsis (...) All strategy, ellipsis
Everyone should know how to use it.text-overflow:ellipsis
Attribute to display the ellipsis (…) for overflow of a single line of text (...). Of course, some browsers still need to add widthwidth
Attribute.
Css code:
- Overflow: hidden;
- Text-overflow: ellipsis;
- 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-clamp
Note: This is an nonstandard property (unsupported WebKit property), which does not appear in the CSS specification draft.
-webkit-line-clamp
Limit 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:
- Overflow: hidden;
- Text-overflow: ellipsis;
- Display:-webkit-box;
- -Webkit-line-clamp: 2;
- -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:
- P {
- Position: relative;
- Line-height: 1.4em;
- /* 3 times the line-height to show 3 lines */
- Height: 4.2em;
- Overflow: hidden;
- }
- P: after {
- Content :"...";
- Font-weight: bold;
- Position: absolute;
- Bottom: 0;
- Right: 0;
- Padding: 0 20px 1px 45px;
- Background: url (http://css88.b0.upaiyun.com/css88/2014/09/ellipsis_bg.png) repeat-y;
- }
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:
- Var module = document. getElementById ("clamp-this-module ");
- $ Clamp (module, {clamp: 3 });
2. jQuery plug-in-jQuery. dotdotdot
This is also very convenient to use:
Js Code:
- $ (Document). ready (function (){
- $ ("# Wrapper"). dotdotdot ({
- // Configuration goes here
- });
- });
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/