Pseudo-elements and generated decoration
Pseudo-elements let CSS style a part of an element or add decorative generated content without adding a separate markup node.
✓ What you will learn in this topic
- Explain the key idea: Pseudo-elements are useful for decoration; meaningful content belongs in the document.
- Apply the concept: A decorative blue stripe appears beside the demo, and the link receives a visual external-style arrow.
- Avoid this common mistake:
Concept explanation
::before and ::after create generated boxes when their content is set. ::first-letter and ::selection style parts of text. Generated content is appropriate for decoration such as a small visual marker, not for labels, instructions, or information a visitor must understand.
When a pseudo-element is positioned, establish the containing block intentionally. A decorative pseudo-element should not obscure text or intercept pointer events.
Syntax
A CSS rule has a selector followed by declarations. Each declaration is a property and a value; the braces group the declarations that apply to matching elements.
.demo {
position: relative;
padding-inline-start: 1.25rem;
}
.demo::before {
content: "";
position: absolute;
inset: 0.25rem auto 0.25rem 0;
width: 0.25rem;
background: #2563eb;
}Example 1: a small page element
Use this HTML as the target and place the CSS in a stylesheet linked to the page, or inside a <style> element while experimenting.
<main class="demo card-grid course-grid dashboard-panel course-card-list dashboard-grid card-layout"><h2>Learning card</h2><p>Clear styles make this information easier to scan.</p><nav class="site-nav"><a href="/learn">Courses</a></nav><label class="form-field"><span>Email</span><input type="email" aria-invalid="true"></label><article class="card course-card dashboard-card theme-dark"><span class="badge status">New</span><div class="course-card__image"></div><h2 class="course-card__title card__title">Course update</h2><p class="status">New lessons are available this week.</p><a href="/learn" aria-current="page">Read the details</a><div class="actions course-card__actions dashboard-panel__actions"><button type="button">View lessons</button><button type="button" disabled>Unavailable</button></div></article></main>.demo {
position: relative;
padding-inline-start: 1.25rem;
}
.demo::before {
content: "";
position: absolute;
inset: 0.25rem auto 0.25rem 0;
width: 0.25rem;
background: #2563eb;
}Expected result: A decorative blue stripe appears beside the demo, and the link receives a visual external-style arrow.
Example 2: a practical interface card
The same idea can style a reusable card on a course page. The class selector keeps the rule attached to this component rather than changing every element of that type.
<section class="demo card-grid course-grid dashboard-panel course-card-list dashboard-grid card-layout"><h2>Learning card</h2><p>Clear styles make this information easier to scan.</p><nav class="site-nav"><a href="/learn">Courses</a></nav><label class="form-field"><span>Email</span><input type="email" aria-invalid="true"></label><article class="card course-card dashboard-card theme-dark"><span class="badge status">New</span><div class="course-card__image"></div><h2 class="course-card__title card__title">Course update</h2><p class="status">New lessons are available this week.</p><a href="/learn" aria-current="page">Read the details</a><div class="actions course-card__actions dashboard-panel__actions"><button type="button">View lessons</button><button type="button" disabled>Unavailable</button></div></article></section>.card a::after {
content: " â";
font-size: 0.85em;
}
.card::before {
content: "";
pointer-events: none;
}Expected result: The course update reads as a distinct interface card, and its content follows the spacing, color, and layout choices in the rule.
Common mistake
Using hover as the only way to reveal an action excludes touch users and many keyboard users.
Best practice
Provide visible focus styles, pair hover with focus-visible, and keep essential content in HTML rather than generated content.
Practice exercise
Style a link and button for hover, keyboard focus, and disabled states. Add a decorative marker with a pseudo-element. Apply it to the example above and inspect the result at a narrow and a wide viewport.
Section summary
- Pseudo-elements are useful for decoration; meaningful content belongs in the document.
- Provide visible focus styles, pair hover with focus-visible, and keep essential content in HTML rather than generated content.
Knowledge check
What kind of content belongs in ::before or ::after?
Reveal the explanation
Generated content may be unavailable or confusing to some users, so keep essential information in HTML.
Finish the module lessons, then take the server-scored module quiz to check your understanding.
Practical example
css.demo {
position: relative;
padding-inline-start: 1.25rem;
}
.demo::before {
content: "";
position: absolute;
inset: 0.25rem auto 0.25rem 0;
width: 0.25rem;
background: #2563eb;
}
Expected result:
A decorative blue stripe appears beside the demo, and the link receives a visual external-style arrow.
Hands-on practice task
Style a link and button for hover, keyboard focus, and disabled states. Add a decorative marker with a pseudo-element.
In this section
Other lessons in Pseudo-Classes and Pseudo-Elements
Broaden your knowledge
Related learning topics
HTML
Learn to structure accessible, meaningful web pages with a complete HTML course from first document to professional application patterns.
JavaScript
Build strong JavaScript foundations with browser examples, focused practice, beginner projects, and module quizzes.