Use CSS to control character length and display length

Source: Internet
Author: User

In the Web page layout design, will encounter the text exceeds the fixed length causes the whole page distortion situation. Programmers often need to intercept fixed lengths to achieve certain fixed-length controls. This paper introduces a method of directly using CSS code control to realize text interception, which is different from the direct character interception of programmers, and its advantage is that it can automatically control the length of text display; The disadvantage is that the compatibility of different browsers is not perfect.

With the three main attributes of the Overflow,text-overflow,white-space, the other code is a cosmetic function.

CSS code:

<style>. Texthidden{width:200px;Overflow:Hidden;Text-overflow:ellipsis;White-space:nowrap;Border:1px solid #ddd;}</style>

HTML code:

<class= "Texthidden">    CSS hides the contents of the overflow part of the container </  div>

Code Analysis:

    1. width:200px; Specify width:
    2. Overflow:hidden; will be out of content hidden
    3. Text-overflow:ellipsis; ie private attribute, text overflow display ellipsis mark (); Other browsers are not supported.
    4. White-space:nowrap; Forces the content to be wrapped without wrapping. Forces all text to be displayed in the same line until the text ends or encounters a BR element

At present, the successful example of using this method is the content display of Gmail, which achieves the best effect under IE. If it is FF that is worse, there will always be the effect of intercepting half of Chinese. The main reason for this is that the Text-overflow property is only valid under IE, there are many CSS properties, especially the two attributes of margin and padding, which often lead to different effects on the page. You need to be aware of the effects of these properties on different browsers.

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.