XML technologies / CSS / CSS Cookbook / Typography for the web / Highlighting portions of text

(Excerpt from “CSS Kochbuch” by Christopher Schmitt)

Problem

You want to highlight parts of a paragraph, as shown in the following figure.

Highlighted text

Figure: Highlighted text.

Solution

Mark the passages to highlight with the element strong:

<p>The distribution of messages from the selling of propaganda to the giving away of disinformation takes place at a blindingly fast pace thanks to the state of technology &hellip; <strong>This change in how fast information flows revolutionizes the culture.</strong> ...</p>

Now define the CSS rules that highlight the text:

strong {
 font-weight: normal;
 background-color: yellow;
}

Discussion

Instead of the strong element used here, you could also use the element em to mark the text to highlight. According to the HTML 4.01 specification, em marks emphasised text, whereas strong indicates “more strongly emphasised text”.

Once the text is marked, use the property background-color to choose the highlight colour. Since some browsers display text marked with strong in bold, also set the property font-weight (font weight) to normal. When using em, instead set the property font-style to normal to prevent some browsers from displaying the marked passages in italics, as shown in the following code example:

em {
 font-style: normal;
 background-color: #ff00ff;
}

See also

The HTML 4 specification for the elements strong and em.

  

 

 

 

back to top