This is the 9th section of the CSS Getting Started tutorial; it describes some ways in which you can use CSS to add content when a document is displayed. You modify your stylesheet to add text content and an image.

Information: Content

One of the important advantages of CSS is that it helps you to separate a document's style from its content. Yet there are situations where it makes sense to specify certain content as part of the stylesheet, not as part of the document.

Content specified in a stylesheet can consist of text or images. You specify content in your stylesheet when the content is closely linked to the document's structure.

More details

Specifying content in a stylesheet can cause complications. For example, you might have different language versions of your document that share a stylesheet. If part of the stylesheet has to be translated, it means that you must put those parts of the stylesheet in separate files and arrange for them to be linked with the appropriate language versions of your document.

These complications do not arise if the content you specify consists of symbols or images that apply in all languages and cultures.

Content specified in a stylesheet does not become part of the DOM.

Text content

CSS can insert text content before or after an element. To specify this, make a rule and add ::before or ::after to the selector. In the declaration, specify the content property with the text content as its value.



A text where I need to <span class="ref">something</span>


.ref::before {
  font-weight: bold;
  color: navy;
  content: "Reference: ";


More details

The character set of a stylesheet is UTF-8 by default, but it can be specified in the link, or in the stylesheet itself, or in other ways. For details, see 4.4 CSS style sheet representation in the CSS Specification.

Individual characters can also be specified by an escape mechanism that uses backslash as the escape character. For example, \265B is the chess symbol for a black queen ♛. For details, see Referring to characters not represented in a character encoding and also Characters and case in the CSS Specification.

Image content

To add an image before or after an element, you can specify the URL of an image file in the value of the content property.


This rule adds a space and an icon after every link that has the class glossary:

a.glossary::after {
  content: " " url("../images/glossary-icon.gif");

What next?

A common way that stylesheets add content is to mark items in lists. The next section describes how to specify style for list elements.