Lesson 7 of 8

Specificity

Goal

Why your rule doesn't apply

01

Explanation

When two rules give different values to the same property on the same element, the browser first chooses between them by specificity, meaning how precise the selector is. An #id beats any number of classes, a class beats any number of element selectors, and if the two rules tie, the one written later in the file wins.

That's why you write .active and the color doesn't change: #sidebar .menu a has higher specificity thanks to the id. The bold weight does apply, though, because that rule doesn't mention it. A losing rule only loses the properties where it conflicts with another rule.

The quick fix with !important is a trap. It solves the problem this time and then opens a door that never closes, because every change after it needs another !important with a heavier selector. The right fix is to lower the specificity: stick to a few classes and don't use ids for styling.

02

Your task

Make the active link appear in its own color, #b91c1c, and the other links in #334155, without !important and without any selector that contains an id.

Checks

4 checks
  1. The active link is #b91c1c
  2. The other links are #334155
  3. No rule uses !important
  4. No selector contains an id
03

Hints

0/3
Track lessons0 of 8 done

Go deeper into the box model, custom properties, positioning, container queries and more, so you understand what's really going on behind the styles you see.

Intermediate level, 8 lessons, 1.5 hours in total.

index.html
Preview
index.html100%
–/4

Press “Run and check” to grade your code.

4 checks listed under “Your task”.

Track lessons0 of 8 done

Go deeper into the box model, custom properties, positioning, container queries and more, so you understand what's really going on behind the styles you see.

Intermediate level, 8 lessons, 1.5 hours in total.