The :before and :after pseudo-elements

The :before and :after pseudo-elements

Authors specify the style and location of generated content with the :before and :after pseudo-elements. As their names indicate, the :before and :after pseudo-elements specify the location of content before and after an element's document tree content. The 'content' property, in conjunction with these pseudo-elements, specifies what is inserted.

Example(s):

For example, the following rule inserts the string "Note: " before the content of every P element whose "class" attribute has the value "note":

p.note:before { content: "Note: " }

The formatting objects (e.g., boxes) generated by an element include generated content. So, for example, changing the above style sheet to:

p.note:before { content: "Note: " }
p.note{ border: solid green }

would cause a solid green border to be rendered around the entire paragraph, including the initial string.

The :before and :after pseudo-elements inherit any inheritable properties from the element in the document tree to which they are attached.

Example(s):

For example, the following rules insert an open quote mark before every Q element. The color of the quote mark will be red, but the font will be the same as the font of the rest of the Q element:

q:before {
content: open-quote;
color: red
}

In a :before or :after pseudo-element declaration, non-inherited properties take their initial values.

Example(s):

So, for example, because the initial value of the 'display' property is 'inline', the quote in the previous example is inserted as an inline box (i.e., on the same line as the element's initial text content). The next example explicitly sets the 'display' property to 'block', so that the inserted text becomes a block:

body:after {
content: "The End";
display: block;
margin-top: 2em;
text-align: center;
}

The :before and :after pseudo-elements elements interact with other boxes, such as run-in boxes, as if they were real elements inserted just inside their associated element.

Example(s):

For example, the following document fragment and style sheet:

<h2> Header </h2> h2 { display: run-in; }
<p> Text </p> p:before { display: block; content: 'Some'; }

...would render in exactly the same way as the following document fragment and style sheet:

<h2> Header </h2>h2 { display: run-in; }
<p><span>Some</span> Text </p>span { display: block }

Similarly, the following document fragment and style sheet:

<h2> Header </h2> h2 { display: run-in; }
h2:after { display: block; content: 'Thing'; }
<p> Text </p>

...would render in exactly the same way as the following document fragment and style sheet:

<h2> Header <span>Thing</span></h2> h2 { display: block; }
span { display: block; }
<p> Text </p>

Note. This specification does not fully define the interaction of :before and :after with replaced elements (such as IMG in HTML). This will be defined in more detail in a future specification.

12.2 The 'content' property

'content'
Value: normal | none | [ <string> | <uri> | <counter> | attr(<identifier>) | open-quote | close-quote | no-open-quote | no-close-quote ]+ | inherit
Initial: normal
Applies to: :before and :after pseudo-elements
Inherited: no
Percentages: N/A
Media: all
Computedvalue: On elements, always computes to 'normal'. On :before and :after, if 'normal' is specified, computes to 'none'. Otherwise, for URI values, the absolute URI; for attr() values, the resulting string; for other keywords, as specified.

This property is used with the :before and :after pseudo-elements to generate content in a document. Values have the following meanings:

none
The pseudo-element is not generated.
normal
Computes to 'none' for the :before and :after pseudo-elements.
<string>
Text content (see the section on strings).
<uri>
The value is a URI that designates an external resource (such as an image). If the user agent cannot display the resource it must either leave it out as if it were not specified or display some indication that the resource cannot be displayed.
<counter>
Counters may be specified with two different functions: 'counter()' or 'counters()'. The former has two forms: 'counter( name)' or 'counter( name, style)'. The generated text is the value of the innermost counter of the given name in scope at this pseudo-element; it is formatted in the indicated style ('decimal' by default). The latter function also has two forms: 'counters( name, string)' or 'counters( name, string, style)'. The generated text is the value of all counters with the given name in scope at this pseudo-element, from outermost to innermost separated by the specified string. The counters are rendered in the indicated style ('decimal' by default). See the section on automatic counters and numbering for more information. The name must not be 'none', 'inherit' or 'initial'. Such a name causes the declaration to be ignored.
open-quote and close-quote
These values are replaced by the appropriate string from the 'quotes' property.
no-open-quote and no-close-quote
Introduces no content, but increments (decrements) the level of nesting for quotes.
attr(X)
This function returns as a string the value of attribute X for the subject of the selector. The string is not parsed by the CSS processor. If the subject of the selector does not have an attribute X, an empty string is returned. The case-sensitivity of attribute names depends on the document language.
Note. In CSS 2.1, it is not possible to refer to attribute values for other elements than the subject of the selector.

The 'display' property controls whether the content is placed in a block or inline box.

Example(s):

The following rule causes the string "Chapter: " to be generated before each H1 element:

H1:before { 
content: "Chapter: ";
display: inline;
}

Authors may include newlines in the generated content by writing the "/A" escape sequence in one of the strings after the 'content' property. This inserted line break is still subject to the 'white-space' property. See "Strings" and "Characters and case" for more information on the "/A" escape sequence.

Example(s):

h1:before {
display: block;
text-align: center;
white-space: pre;
content: "chapter/A hoofdstuk/A chapitre"
}

Generated content does not alter the document tree. In particular, it is not fed back to the document language processor (e.g., for reparsing).

你可能感兴趣的:(element)