w3pop.com :: ÍøÂçѧԺ :: CSS :: CSS Î±ÔªËØ
CSS pseudo-elements are used to add special effects to some selectors.
CSSµÄÎ±ÔªËØ¿ÉÓÃÀ´¸øÒ»Ð©Ñ¡ÔñÆ÷¼ÓÉÏÌØÊâµÄЧ¹û¡£
Make the first letter special
This example demonstrates how to add a special effect to the first letter of a text.
ÈçºÎ¸øÎÄ×ÖµÄÊ׸ö×Ö¼ÓÉÏÌØÊâЧ¹û
Make the first line special
This example demonstrates how to add a special effect to the first line of a text.
ÈçºÎ¸øÎÄ×ֵĵÚÒ»ÐмÓÉÏÌØÊâЧ¹û
The syntax of pseudo-elements:
Î±ÔªËØµÄÓ﷨Ϊ£º
selector:pseudo-element {property: value}
|
CSS classes can also be used with pseudo-elements:
CSSÀàÒ²¿ÉÒÔʹÓÃÎ±ÔªËØ:
selector.class:pseudo-element {property: value}
|
The "first-line" pseudo-element is used to add special styles to the first line of the text in a selector:
Ò»°ãÕâ¸öÎ±ÔªËØÊ¹ÓÃÔÚΪÎÄ×ÖÊ×ÐÐÌí¼ÓÌØÊâÑùʽÉÏ£º
p {font-size: 12pt}
<p>Some text that ends up on two or more lines</p> |
The output could be something like this:
Êä³öµÄÑù×Ó¾ÍÊÇÕâÑù£º
|
Some text that ends |
In the example above the browser displays the first line formatted according to the "first-line" pseudo element. Where the browser breaks the line depends on the size of the browser window.
ÉÏÃæµÄÀý×ÓÖÐä¯ÀÀÆ÷¾Í»áÒÀ¾Ý"first-line"Î±ÔªËØ½«µÚÒ»Ðиñʽ»¯£¬ä¯ÀÀÆ÷µÄ·ÖÐÐÊǸù¾ÝËüµÄ´°¿Ú´óСÀ´¶¨µÄ¡£
Note: The "first-line" pseudo-element can only be used with block-level elements.
×¢Ò⣺"first-line"Î±ÔªËØÖ»¿ÉÒÔͨ¹ýblock-levelÔªËØÀ´Ê¹ÓÃ
Note: The following properties apply to the "first-line" pseudo-element:
ÒÔÏÂÊôÐÔÊÊÓÃ"first-line"Î±ÔªËØ£º
The "first-letter" pseudo-element is used to add special style to the first letter of the text in a selector:
Õâ¸öÎ±ÔªËØÓÃÔÚҪΪÎÄ×ֵĵÚÒ»×ÖĸÌí¼ÓÌØÊâÑùʽµÄʱºò£º
p {font-size: 12pt}
<p>The first words of an article.</p> |
The output could be something like this:
Êä³ö¾Í³ÉÕâ¸öÑù×Ó£º
|
___ |
Note: The "first-letter" pseudo-element can only be used with block-level elements.
×¢Ò⣺ºÍ"first-line"αÊôÐÔÒ»Ñù£¬"first-letter"Ò²Ö»ÓÐͨ¹ýblock-levelÔªËØ²ÅÄÜʹÓÃ
Note: The following properties apply to the "first-letter" pseudo- element:
×¢Ò⣺ÒÔÏÂÊôÐÔÊÊÓÃÓÚ "first-letter"Î±ÔªËØ£º
Pseudo-elements can be combined with CSS classes:
Î±ÔªËØ¿ÉÒÔºÍCSSÀà»ìºÏʹÓãº
p.article:first-letter {color: #FF0000}
<p class="article">A paragraph in an article</p> |
The example above will make the first letter of all paragraphs with class="article" red.
ÒÔÉϵÄÀý×Ó»áÈÃËùÓÐclass="article"µÄ¶ÎÂäÊ××ÖĸΪºìÉ«¡£
Several pseudo-elements can be combined:
¼¸¸öÎ±ÔªËØ¿ÉÒÔ»ìºÏʹÓÃ:
p {font-size: 12pt}
<p>The first words of an article</p> |
The output could be something like this:
Êä³ö¾Í³ÉÕâÑù£º
|
___ |
In the example above the first letter of the paragraph will be red with a font size of 24pt. The rest of the first line would be blue while the rest of the paragraph would be the default color.
ÉÏÃæµÄÀý×Ó¶ÎÂäµÄÊ׸ö×Öĸ»á³ÉΪ´óСΪ24ptµÄºìÉ«×ÖÌ壬ÓàÏÂÊ×ÐеIJ¿·Ö»á±äΪÀ¼É«£¬ÔÙÓàϵľÍÊÇĬÈϵÄÑÕÉ«ÁË¡£
The ":before" pseudo-element can be used to insert some content before an element.
":before"Î±ÔªËØ¿ÉÒÔÓÃÀ´ÔÚÒ»ÔªËØÇ°²åÈëһЩÄÚÈÝ
The style below will play a sound before each occurrence of a header one element.
ÏÂÃæµÄÑùʽ¾Í»áÔÚÿ¸ö±êÌâÔªËØÇ°²¥·ÅÒ»¶ÎÉùÒô
|
h1:before |
The ":after" pseudo-element can be used to insert some content after an element.
":after"Î±ÔªËØÓÃÔÚ¸øÒ»ÔªËغó²åÈëһЩÄÚÈÝ
The style below will play a sound after each occurrence of a header one element.
ÏÂÃæµÄÑùʽ¾Í»áÔÚÿ¸ö±êÌâÔªËØºó²¥·ÅÒ»¶ÎÉùÒô
|
h1:after |
Ö§³ÖµÄä¯ÀÀÆ÷: IE: Internet Explorer, F: Firefox, N: Netscape.
W3C: The number in the "W3C" column indicates in which CSS recommendation the property is defined (CSS1 or CSS2).
W3C£ºÏ±íÖГW3C”×ÝÀ¸ÖеÄÊý×ÖÖ¸Ã÷ÁËÖ§³Ö¸ÃÊôÐÔµÄCSS°æ±¾£¨CSS1»òCSS2£©¡£
| Î±ÔªËØ | Òâͼ | IE | F | N | W3C |
|---|---|---|---|---|---|
| :first-letter | Adds special style to the first letter of a text ÉèÖÃÎÄ×ÖµÚÒ»¸ö×Ö·ûµÄÑùʽ |
5 | 1 | 8 | 1 |
| :first-line | Adds special style to the first line of a text ΪÎÄ×ֵĵÚÒ»ÐмÓÉÏÌØÊâÑùʽ |
5 | 1 | 8 | 1 |
| :before | Inserts some content before an element ÔÚÔªËØÇ°²åÈëһЩÄÚÈÝ |
1.5 | 8 | 2 | |
| :after | Inserts some content after an element ÔÚÔªËØºó²åÈëһЩÄÚÈÝ |
1.5 | 8 | 2 |