CSS Pseudo-elements
CSS Pseudo-elements
Pseudo-elements style a particular part of an element or create decorative generated content.
What you’ll learn
- Create a decorative ::before marker.
- Style a first letter independently.
- Keep meaningful text in semantic HTML.
Understand the concept
::before and ::after generate styleable boxes associated with an element when content is supplied. ::first-letter styles the first letter of a block’s first formatted line, and ::selection controls some highlighting of selected text.
Generated content is primarily decorative. Important, meaningful information should remain in the HTML so it stays robust for assistive technologies and other user agents.
What the browser evaluates
The double colon distinguishes pseudo-elements from pseudo-classes, for example <code>.label::before</code>.
Add a decorative prefix
A small circle is generated before a label.
HTML
<p class="label">New lesson available</p>CSS
.label { font-weight: 700; color: #1e40af; }
.label::before { content: ""; display: inline-block; width: 9px; height: 9px; background: #14b8a6; border-radius: 50%; margin-right: 9px; }Rendered output
New lesson available
Observe: The green dot comes from CSS, while the message remains in HTML.
Style the first letter
A decorative initial makes the paragraph distinctive.
HTML
<p class="lead">Design starts with readable content and careful spacing.</p>CSS
.lead { line-height: 1.65; color: #334155; }
.lead::first-letter { color: #7c3aed; font-size: 30px; font-weight: 700; }Rendered output
Design starts with readable content and careful spacing.
Observe: The paragraph stays accessible text; only its first letter is presented differently.
Explain the result
The double colon distinguishes pseudo-elements from pseudo-classes, for example <code>.label::before</code>.
Compare the two examples: identify the selector, each changed property, and which visible part of the output proves that the CSS took effect.
Common mistakes to avoid
contentis normally required for visible::beforeand::aftergenerated boxes.- Avoid putting crucial words solely in generated content.
Try it yourself
Change the decorative dot into a small square and add a dotted underline to the label.
Before changing the code, predict what will change. Then verify by checking the rendered result.
Check your understanding
What you should remember
Use pseudo-elements for decoration or parts of existing elements, not critical semantic content.
Revisit the rendered examples if the rule still feels abstract. Change one declaration at a time so you can connect cause and effect.
Reaching the recap and completing the quizzes are separate milestones.