Test browsers: IE6/6/7/8/9, opera12.02, firefox15.0.1, chrome
The code is as follows: |
Copy code |
<P style = "width: 200px; overflow: hidden; white-space: nowrap; text-overflow: ellipsis;"> omit me and ignore me! Ignore me! Omit me! </P> |
If we want to define text-overflow: ellipsis for the p tag, we can write it like this:
The code is as follows: |
Copy code |
P { White-space: nowrap; Width: 100%;/* the width of IE6 needs to be defined */ Overflow: hidden; -O-text-overflow: ellipsis;/* Opera */ Text-overflow: ellipsis;/* IE, Safari (WebKit )*/ } |
Now let's explain why we should do this:
1. text-overflow: ellipsis;
The key style here is text-overflow: ellipsis;
However, text-ellipsis is a special style, which is interpreted as follows: the text-overflow attribute is only an annotation, and whether the omitted flag is displayed when the text overflows. Does not have other style attribute definitions. To achieve the effect of skipping numbers during overflow, you must also define: to force the text to be displayed (white-space: nowrap) in a row and to hide the overflow content (overflow: hidden ), only in this way can the overflow text display effect be achieved. To put it simply, I want to limit the text to one line (white-space: nowrap;). Surely this line has a limit (width ), in addition, the overflow part should be hidden (overflow: hidden;), and then the ellipsis (text-overflow: ellipsis) will appear ).
2. white-space
By the way, let's explain how to use white-space.
The white-space attribute declares how to process the blank characters in the elements during the establishment of the layout. (Nonsense, here the blank space should refer to the space or press enter we press on the keyboard, because there will be a space or press enter no matter what white-space is set .)
Full instance
The code is as follows: |
Copy code |
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <Html xmlns = "http://www.w3.org/1999/xhtml"> <Head> <Meta http-equiv = "Content-Type" content = "text/html; charset = utf-8"/> <Title> ellipsis test </title> <Style type = "text/css"> *{ Margin: 0; Padding: 0; } Body { Padding: 10px; Font-family: Arial; } # Test { Position: relative; Width: 150px; Height: 20px; Line-height: 20px; Text-overflow: ellipsis; White-space: normal; * White-space: nowrap; Overflow: hidden; Border: 1px solid #999; } # Test span { Position: absolute; Top: 0; Right: 0; Display: block; Float: left; } # Test span: after {content :"...";} </Style> </Head> <Body> <Div id = "test"> let's get up and cheer up. <span> </div> </Body> </Html> |