XML technologies / CSS / CSS Cookbook / Typography for the web / Styling the first line of a paragraph

(Excerpt from “CSS Kochbuch” by Christopher Schmitt)

Problem

You want a paragraph’s first line to appear in bold, as shown in the following figure.

The first line of a paragraph appears in bold

Figure: The first line of a paragraph appears in bold.

Solution

Use the pseudo-element :first-line to apply the style only to the first line:

p:first-line {
 font-weight: bold;
}

Discussion

Like a class selector, a pseudo-element selects particular parts of a web document. Unlike a class selector, however, resizing the browser window or changing the font size can alter the parts it matches. In this example, the length of the first line can change when the browser window is resized (see the following figure).

Note that Internet Explorer 5 does not recognise the pseudo-element :first-line.

Resizing the browser window also changes the affected text

Figure: Resizing the browser window also changes the affected text.

See also

The CSS 2.1 specification for the :first-line pseudo-element.

  

 

 

 

back to top