ÍøÂçѧԺ w3popÉçÇø ÍøÂç×ÊÔ´ ITÐÂÎÅ

w3pop.com :: ÍøÂçѧԺ :: CSS :: CSS Î±ÔªËØ

»áÔ±µÇ½

ÕʺÅ

ÃÜÂë

»Ø´ð

¼ÇסÃÜÂë

Íü¼ÇÃÜÂë? ×¢²á

CSS
CSS2 Aural
CSS µ¥Î»
CSS ÑÕÉ«
CSS ÑÕɫֵ
CSS ÑÕÉ«Ãû³Æ
CSS ±àÂëÓë×éÖ¯¼¼..
CSS Ä£¿é»¯Éè¼ÆË¼..
CSSÍêÃÀ¼æÈÝIE6/I..
CSS @ Ten: ÏÂÒ»¸..

CSS Î±ÔªËØ


×÷Õß:w3pop.com ·­Òë/ÕûÀí:w3pop.com ·¢²¼:2007-04-28 ä¯ÀÀ:5459 :: ::

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
:first-lineÎ±ÔªËØ

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:first-line {color: #0000FF; font-variant: small-caps}
<p>Some text that ends up on two or more lines</p>

The output could be something like this:
Êä³öµÄÑù×Ó¾ÍÊÇÕâÑù£º

Some text that ends
up on two or more lines

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"Î±ÔªËØ£º

  • font ÊôÐÔ
  • color ÊôÐÔ
  • background ÊôÐÔ
  • word-spacing
  • letter-spacing
  • text-decoration
  • vertical-align
  • text-transform
  • line-height
  • clear

The :first-letter Pseudo-element
:first-letterÎ±ÔªËØ

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:first-letter {font-size: 200%; float: left}
<p>The first words of an article.</p>

The output could be something like this:
Êä³ö¾Í³ÉÕâ¸öÑù×Ó£º

___
  |  he first 
  |  words of an
article.

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"Î±ÔªËØ£º

  • font ÊôÐÔ
  • color ÊôÐÔ
  • background ÊôÐÔ
  • margin ÊôÐÔ
  • padding ÊôÐÔ
  • border ÊôÐÔ
  • text-decoration
  • vertical-align (only if 'float' is 'none')
  • text-transform
  • line-height
  • float
  • clear

Pseudo-elements and CSS Classes
Î±ÔªËØºÍCSSÀà

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"µÄ¶ÎÂäÊ××ÖĸΪºìÉ«¡£


Multiple Pseudo-elements
¶àÖØÎ±ÔªËØ

Several pseudo-elements can be combined:
¼¸¸öÎ±ÔªËØ¿ÉÒÔ»ìºÏʹÓÃ:

p {font-size: 12pt}
p:first-letter {color: #FF0000; font-size: 200%}
p:first-line {color: #0000FF}
<p>The first words of an article</p>

The output could be something like this:
Êä³ö¾Í³ÉÕâÑù£º

___
  |  he first 
  |  words of an
article.

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¿·Ö»á±äΪÀ¼É«£¬ÔÙÓàϵľÍÊÇĬÈϵÄÑÕÉ«ÁË¡£


CSS2 - The :before Pseudo-element
CSS2ÖеÄ:beforeÎ±ÔªËØ

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
{
content: url(beep.wav)
}



CSS2 - The :after Pseudo-element
CSS2ÖеÄ:after Î±ÔªËØ

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
{
content: url(beep.wav)
}



Pseudo-elements
Î±ÔªËØ

Ö§³ÖµÄä¯ÀÀÆ÷: 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

ÆÀÂÛ (1) 1 All