On this page10 sections
  1. Learning Objectives
  2. 1. Interaction States
  3. 2. Link States
  4. 3. Choosing by Position
  5. 4. ::before and ::after
  6. 5. More Pseudo-elements
  7. Common Beginner Mistakes
  8. Practice Questions
  9. Practice Answers
  10. Key Takeaways

Lecture 020: Pseudo-classes & Pseudo-elements

Category5: Pseudo-classes & Components
Lecture020 of 057
Estimated video length14 min
TargetCSS3 · any modern browser
LevelIntermediate

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, :focus and :active
  • Style links with :link and :visited
  • Pick elements by position with :first-child, :last-child and :nth-child()
  • Add content with ::before and ::after, and style ::first-letter and ::placeholder

1. Interaction States

Example: hover over the button, then click it and press Tab:

html
<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-classWhen
:hoverThe mouse is over it
:focusIt is selected for typing or keyboard use
:focus-visibleFocused by keyboard, so you can show an outline only for keyboard users
:activeIt is being clicked

Write them in this order, or later rules hide earlier ones: LoVe HAte.

css
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

html
<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

html
<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

html
<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

  1. Which pseudo-class styles a button while the mouse is over it?
  2. Write a rule that gives every even table row a light grey background.
  3. Which property must ::before and ::after always have?
  4. Add a red asterisk after every element with the class required.
  5. Why style :focus-visible instead of removing outlines?
  6. How do you colour the placeholder text in inputs?

Practice Answers

  1. :hover
  2. tr:nth-child(even) { background: #f1f3f5; }
  3. content
  4. .required::after { content: " *"; color: red; }
  5. Keyboard users need to see which element is focused; :focus-visible shows it without affecting mouse users.
  6. 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 / ::after need content.
  • Never remove focus outlines without giving keyboard users something better.