What pseudo-classes are there?

👨‍💻 Frontend Developer 🟠 May come up 🎚️ Medium
#CSS #HTML

Brief Answer

Pseudo-classes are selectors that target elements in a specific state or position:

  1. :hover — when cursor hovers over element 🖱️
  2. :focus — when element has focus 🎯
  3. :active — when element is being clicked 👆
  4. :visited — for visited links 🔗
  5. :first-child — first child element 1️⃣
  6. :last-child — last child element 🔚
  7. :nth-child() — element by number 🔢
  8. :not() — excludes elements ❌
/* Main pseudo-classes */
a:hover { color: red; }
input:focus { border: 2px solid blue; }
li:first-child { font-weight: bold; }
p:nth-child(2n) { background: #f0f0f0; }

Full Answer

Pseudo-classes are like smart filters that select elements not by their type or class, but by state, position, or other characteristics! 🎯

Pseudo-class syntax

/* Single colon for pseudo-classes */
element:pseudo-class {
  property: value;
}
 
/* Can be combined */
element.class:pseudo-class {
  property: value;
}

1. State pseudo-classes

React to user actions:

/* Hover */
a:hover {
  color: #007bff;
  text-decoration: underline;
}
 
/* Focus */
input:focus {
  outline: none;
  border: 2px solid #007bff;
  box-shadow: 0 0 5px rgba(0, 123, 255, 0.3);
}
 
/* Active state */
button:active {
  transform: scale(0.98);
  background: #0056b3;
}
 
/* Visited links */
a:visited {
  color: purple;
}

2. Structural pseudo-classes

Select elements by position:

/* First and last */
li:first-child {
  margin-top: 0;
  font-weight: bold;
}
 
li:last-child {
  margin-bottom: 0;
  border-bottom: none;
}
 
/* By number */
tr:nth-child(odd) {
  background: #f9f9f9;
}
 
tr:nth-child(even) {
  background: white;
}
 
/* Every third */
.item:nth-child(3n) {
  margin-right: 0;
}

3. Type pseudo-classes

Work with element type:

/* First element of type */
h2:first-of-type {
  margin-top: 0;
  color: #333;
}
 
/* Last element of type */
p:last-of-type {
  margin-bottom: 0;
}
 
/* Only element of type */
img:only-of-type {
  display: block;
  margin: 0 auto;
}

4. Negation pseudo-class

Excludes elements from selection:

/* All buttons except disabled */
button:not(:disabled) {
  cursor: pointer;
  opacity: 1;
}
 
/* All links except external */
a:not([href^="http"]) {
  color: #007bff;
}
 
/* All elements except first */
.menu-item:not(:first-child) {
  margin-left: 20px;
}

5. Form pseudo-classes

For form elements:

/* Required fields */
input:required {
  border-left: 3px solid red;
}
 
/* Valid fields */
input:valid {
  border-color: green;
}
 
/* Invalid fields */
input:invalid {
  border-color: red;
  background: #ffe6e6;
}
 
/* Disabled elements */
input:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}
 
/* Checked checkboxes */
input:checked + label {
  font-weight: bold;
  color: green;
}

6. Language pseudo-classes

/* Elements in specific language */
p:lang(en) {
  font-family: "Times New Roman", serif;
}
 
p:lang(ru) {
  font-family: "Arial", sans-serif;
}
/* Target element (by anchor) */
:target {
  background: yellow;
  padding: 10px;
}
 
/* Links to current page */
a[href="#"]:hover {
  cursor: default;
  text-decoration: none;
}

Practical examples

.nav-item {
  padding: 10px 15px;
  transition: all 0.3s ease;
}
 
.nav-item:hover {
  background: #f0f0f0;
  transform: translateY(-2px);
}
 
.nav-item:first-child {
  border-radius: 5px 0 0 5px;
}
 
.nav-item:last-child {
  border-radius: 0 5px 5px 0;
}

Table with alternating rows

table tr:nth-child(even) {
  background: #f8f9fa;
}
 
table tr:hover {
  background: #e9ecef;
  cursor: pointer;
}
 
table th:first-child,
table td:first-child {
  padding-left: 20px;
}

Form with validation

.form-group input:focus {
  border-color: #007bff;
  box-shadow: 0 0 0 0.2rem rgba(0, 123, 255, 0.25);
}
 
.form-group input:valid {
  border-color: #28a745;
}
 
.form-group input:invalid:not(:focus) {
  border-color: #dc3545;
}

Combining pseudo-classes

/* Multiple pseudo-classes */
a:hover:not(:visited) {
  color: #ff6b6b;
}
 
/* With other selectors */
.button:hover:not(:disabled) {
  background: #0056b3;
  transform: translateY(-1px);
}
 
/* Nested states */
.card:hover .card-title:first-child {
  color: #007bff;
}

Differences from pseudo-elements

Pseudo-classesPseudo-elements
Single colon :Double colon ::
Element statePart of element
:hover, :focus::before, ::after

Browser support

Most pseudo-classes are supported by all modern browsers:

  • :hover, :focus, :active — full support
  • :nth-child(), :first-child — IE9+
  • :not() — IE9+ (limited)
  • ⚠️ :focus-visible — modern browsers

Best practices

  1. Use for interactivity:hover, :focus 🖱️
  2. Structure content:first-child, :nth-child() 📋
  3. Improve form UX:valid, :invalid
  4. Don’t overuse — complex selectors are slower 🐌

Conclusion

Pseudo-classes are powerful tools for creating interactive and adaptive interfaces:

  • States:hover, :focus, :active
  • Structure:first-child, :nth-child(), :last-child
  • Forms:valid, :invalid, :required
  • Logic:not(), :target

Use them to create lively and responsive interfaces! 🎨