On this page10 sections
Lecture 020: Pseudo-classes & Pseudo-elements
| Category | 5: Pseudo-classes & Components |
| Lecture | 020 of 057 |
| Estimated video length | 14 min |
| Target | CSS3 · any modern browser |
| Level | Intermediate |
A pseudo-class styles an element in a particular state: hovered, focused, the first in a list. A pseudo-element styles a part of an element, or adds decoration before or after it, with no extra HTML.
Learning Objectives
- Style interaction states with
:hover,:focusand:active - Style links with
:linkand:visited - Pick elements by position with
:first-child,:last-childand:nth-child() - Add content with
::beforeand::after, and style::first-letterand::placeholder
1. Interaction States
Example: hover over the button, then click it and press Tab:
<style>
.btn { background: #1971c2; color: white; border: 0; padding: 10px 18px; border-radius: 6px; cursor: pointer; }
.btn:hover { background: #1864ab; }
.btn:active { transform: scale(0.96); }
.btn:focus-visible { outline: 3px solid #ffd43b; outline-offset: 2px; }
input:focus { border-color: #1971c2; outline: none; box-shadow: 0 0 0 3px rgb(25 113 194 / 0.25); }
input { border: 2px solid #ced4da; padding: 8px; border-radius: 6px; }
</style>
<button class="btn">Hover and click me</button>
<p><input placeholder="Click in here"></p>| Pseudo-class | When |
|---|---|
:hover | The mouse is over it |
:focus | It is selected for typing or keyboard use |
:focus-visible | Focused by keyboard, so you can show an outline only for keyboard users |
:active | It is being clicked |
2. Link States
Write them in this order, or later rules hide earlier ones: LoVe HAte.
a:link { color: #1971c2; } /* not visited yet */
a:visited { color: #7048e8; } /* already visited */
a:hover { color: #e8590c; }
a:active { color: #c92a2a; }3. Choosing by Position
Example
<style>
li { padding: 6px; }
li:first-child { font-weight: bold; }
li:last-child { color: #868e96; }
li:nth-child(even) { background: #f1f3f5; }
</style>
<ul>
<li>First item (bold)</li>
<li>Second (striped)</li>
<li>Third</li>
<li>Fourth (striped)</li>
<li>Last item (grey)</li>
</ul>:nth-child(even) and :nth-child(odd) make striped tables easy. :nth-child(3) picks the third item.
:not(.x) selects everything except elements with the class x.
4. ::before and ::after
They insert decoration before or after the content. The content property is required,
even if it's empty (content: "").
Example
<style>
.required::after { content: " *"; color: #e03131; }
.tip::before { content: "💡 "; }
.quote::before { content: "“"; font-size: 2em; color: #adb5bd; vertical-align: -0.4em; }
</style>
<label class="required">Email</label>
<p class="tip">Save your work often.</p>
<p class="quote">Small steps every day.</p>Use them for decoration only. Screen readers may not read inserted text, so keep real content in the HTML.
5. More Pseudo-elements
Example
<style>
.story::first-letter { font-size: 2.4em; float: left; line-height: 1; margin-right: 6px; color: #e8590c; }
input::placeholder { color: #adb5bd; font-style: italic; }
::selection { background: #ffd43b; }
</style>
<p class="story">Once upon a time, in a small village by the river, there lived a baker who woke up before the sun every single day.</p>
<input placeholder="Placeholder in italics">
<p>Select some text on this page to see the yellow highlight.</p>Common Beginner Mistakes
1. Forgetting content. ::before with no content shows nothing.
2. Removing focus outlines. outline: none with no replacement makes keyboard use
impossible. Style :focus-visible instead.
3. One colon vs two. Pseudo-classes use one colon (:hover); pseudo-elements use two (::before).
4. Wrong link order. Put :hover after :link and :visited, or it never shows.
Practice Questions
- Which pseudo-class styles a button while the mouse is over it?
- Write a rule that gives every even table row a light grey background.
- Which property must
::beforeand::afteralways have? - Add a red asterisk after every element with the class
required. - Why style
:focus-visibleinstead of removing outlines? - How do you colour the placeholder text in inputs?
Practice Answers
:hovertr:nth-child(even) { background: #f1f3f5; }content.required::after { content: " *"; color: red; }- Keyboard users need to see which element is focused;
:focus-visibleshows it without affecting mouse users. input::placeholder { color: #999; }
Key Takeaways
- Pseudo-classes (one colon) style states and positions:
:hover,:focus,:nth-child(). - Pseudo-elements (two colons) style parts or add decoration:
::before,::after,::first-letter. ::before/::afterneedcontent.- Never remove focus outlines without giving keyboard users something better.