XML technologies / CSS / CSS Cookbook / Typography for the web / Adding an image to the first line of a paragraph

(Excerpt from “CSS Kochbuch” by Christopher Schmitt)

Problem

You want to add an image to the first line of a paragraph, as shown in the following figure.

Background image for the first paragraph line

Figure: Background image in the first line of the paragraph.

Solution

Within the pseudo-element :first-line use the property background-image:

p:first-line {
 font-size: 2em;
 background-image: url(background.gif);
}

Discussion

The pseudo-element :first-line affects only the text on an element’s first line, rather than the element itself. In addition to background-image the pseudo-element :first-line also supports the following properties that affect the design:

  • font

  • color

  • background

  • word-spacing

  • letter-spacing

  • text-decoration

  • vertical-align

  • text-transform

  • text-shadow

  • line-height

  • clear

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

See also

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

  

 

 

 

back to top