XML technologies / CSS / CSS Cookbook / General topics / Different selectors for assigning styles / Pseudo-elements

(Excerpt from “CSS Kochbuch” by Christopher Schmitt)

In most cases, web developers assign styles using selectors that refer to elements and their position in the document. However, certain parts of a document can also be addressed without special markup. This is done using the so-called pseudo-elements :first-letter, :first-line, :before and :after.

Here is an example using the pseudo-element :first-letter. The following figure shows the browser rendering:

p:first-letter {
 font-size: 200%;
 font-weight: bold;
}

The first letter has its own styles

Figure: The first letter has its own styles.

Alternatively, use :first-line (see the following figure) to style the entire first line. Even if the first line does not contain a complete sentence or includes the start of a second sentence, the rules defined with :first-line always apply to the entire first line.

p:first-line {
 font-size: 200%;
 font-weight: bold;
}

The entire first line has its own styles

Figure: The entire first line has its own styles.

See also

Further information is available in the CSS 2.1 specification, in the section on selectors. Selectutorial is a tutorial on using CSS selectors. Westciv: browser support - Selectors shows which browsers support which selectors. CSS 2.1: selectors, pseudo-classes and pseudo-elements contains a list of CSS selectors.

  

 

 

 

back to top