Style parts of an element and create decorative content.
A pseudo-element is a keyword added to a selector so CSS can style a particular part of an element. Some pseudo-elements also create rendered content without adding another HTML element.
This lesson focuses on four pseudo-elements: ::before, ::after, ::first-line, and ::selection.
Use this general form:
selector::pseudo-element {
property: value;
}The pseudo-element comes after the rest of the selector. The element named before it is the originating element. In .note::before, the originating element is the HTML element matched by .note.
Pseudo-elements are part of the rendered result; they are not independent HTML elements. CSS-generated content is not included in the DOM, so use it for decoration rather than essential information.
Compare these selectors:
p:hover: Selects a paragraph while it is hovered. This is a pseudo-class: a condition on the originating element.p::first-line: Selects the first rendered line of a paragraph. This is a pseudo-element: a part of the originating element's rendered result.The single colon introduces the state; the double colon introduces the rendered part.
::beforeThe HTML supplies the words. Check the CSS tab for the decoration before them.
.note::before {
content: "• ";
color: green;
}.note identifies the originating paragraph.::before places the generated part before the paragraph's existing text.content supplies the generated value. Without content, or with normal or none, this pseudo-element is not rendered.color styles the generated bullet.The result is a green decorative bullet before “Read the latest update.” The bullet is not added to the HTML text, so the sentence remains meaningful if the decoration is removed.
Use text such as "• ", or an empty string ("") when you need a generated box for decoration without visible text. ::before is inline by default, so it sits in the text flow unless you give it another display value.
The HTML already contains the meaningful label. Complete only the generated decoration:
.note::before {
content: ______;
color: purple;
}The missing value is "• ". The reasoning is the same as in the worked example: choose the originating element, attach ::before, then provide the generated content.
::after::after uses the same pattern but renders after the element's existing content:
.note::after {
content: " •";
color: green;
}With both rules present, the note has a decorative bullet before and after its text. Neither rule changes the text stored in the HTML. The generated content is rendered after the text; it is not a new element that you should use for an instruction or label.
::before and ::after are intended for normal elements such as paragraphs and headings. They cannot be applied to replaced elements such as <img>, whose contents come from an external resource.
.intro::first-line {
font-weight: bold;
}The first displayed line becomes bold. It is a line created by layout, not the first line in the HTML source. The words on that line can change when the element width, document width, or font size changes.
Only a limited group of properties applies to ::first-line, including font properties, background properties, color, spacing, text decoration, text transformation, line height, and text shadow. Use it for text styling, not for arbitrary layout changes.
::selection targets the portion of text a user selects:
.intro::selection {
background-color: yellow;
color: black;
}The colors appear while text in the introduction is selected; they do not permanently highlight the paragraph. ::selection also accepts only a limited set of properties, including color, background-color, text-decoration properties, and text shadow. Safari on iOS uses the system selection colors. Keep essential information in the text so it is available with either appearance. MDN compatibility data for ::selection.
Choose the source of the content before you write the selector:
.note::before and content.::selection; it does not change the document's stored text.CSS-generated content is not part of the DOM and may not be announced reliably by assistive technology. Keep essential information in the HTML so it remains available when CSS is disabled and to users who do not perceive the decoration.
Try it yourself
HTML + CSS
::before and ::after use content to create content before or after an element's existing content.::first-line follows the browser's text wrapping.::selection styles the text currently selected by a user.