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.
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 … <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.
Copyright of the German edition © 2007 by O’Reilly Verlag GmbH & Co. KG
You may print the online version for your private use.
Otherwise, this chapter from “CSS Kochbuch” is subject to the same conditions as the bound edition: the work, including all its parts, is protected by copyright. All rights reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
O’Reilly Verlag GmbH & Co. KG, Balthasarstr. 81, 50670 Köln