Skip to main content

Pseudo-elements for Content

Adding content before or after an element, styling list markers, and more all become possible with pseudo-elements for content.


The CSS ::before Pseudo-element

To add something in front of the content of an element, use ::before, and state what to add in the content property.

Example

Each <h3> element gets an image in front of its content.

Prompt
Demonstrate the ::before pseudo-element. Insert an image (smiley.gif) before the content of each h3 element using the content property. Include a couple of h3 headings with text.
Output: complete HTML page

The CSS ::after Pseudo-element

Its counterpart, ::after, adds what the content property names at the end of the content of an element.

Example

Each <h3> element gets an image following its content.

Prompt
Demonstrate the ::after pseudo-element. Insert an image (smiley.gif) after the content of each h3 element using the content property. Include a couple of h3 headings with text.
Output: complete HTML page

The CSS ::marker Pseudo-element

List item markers (bullets or numbers) can be styled with ::marker.

Example

Here the list item markers get their own style.

Prompt
Demonstrate the ::marker pseudo-element. Style the list item markers with color: red and font-size: 23px. Include both an unordered and ordered list.
Output: complete HTML page

The CSS ::selection Pseudo-element

Whichever part of a text the user selects can be styled with ::selection.

Example

Text selected by the user turns red on a yellow background.

Prompt
Demonstrate the ::selection pseudo-element. Style the user-selected text with color: red and background: yellow. Include a paragraph of text that the user can select to see the effect.
Output: complete HTML page

The CSS ::backdrop Pseudo-element

The area behind a dialog box or popover element can be styled with ::backdrop.

Example

Here the area behind a dialog box gets a style of its own.

Prompt
Demonstrate the ::backdrop pseudo-element. Create a dialog element with a button to open it. Style the backdrop behind the dialog with background-color: lightgreen using dialog::backdrop.
Output: complete HTML page