Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära Specificity | Selecting and Targeting Elements
CSS Foundations

bookSpecificity

Understanding how CSS decides which rule to apply is essential, especially when multiple selectors target the same element. This decision is made using specificity, a system that calculates which selector is more "specific" and should therefore win when rules conflict.

Specificity is determined by assigning values based on the types of selectors used:

  • Inline styles: highest specificity (not shown in this example);
  • ID selectors (#id): high specificity;
  • Class selectors (.class), attribute selectors, and pseudo-classes: medium specificity;
  • Type selectors (element) and pseudo-elements: low specificity.

To break it down, specificity is calculated as a four-part value (a, b, c, d):

  • a: inline styles (not shown here);
  • b: number of ID selectors;
  • c: number of class selectors, attributes, and pseudo-classes;
  • d: number of element selectors and pseudo-elements.
index.html

index.html

style.css

style.css

copy

For the rules in the example, here is how specificity applies:

  • p { color: blue; } has specificity (0,0,0,1) — only a type selector;
  • .highlight { color: green; } has specificity (0,0,1,0) — one class selector;
  • #special { color: orange; } has specificity (0,1,0,0) — one ID selector;
  • #main-title.highlight { color: purple; } has specificity (0,1,1,0) — one ID and one class.

When more than one rule matches, the rule with the highest specificity wins. If two selectors have the same specificity, the one that comes last in the stylesheet is applied.

For example, the <h1 id="main-title" class="highlight"> element matches both .highlight and #main-title.highlight. The latter is more specific because it combines an ID and a class, so it will set the color to purple.

Similarly, <p class="highlight"> matches both the p and .highlight rules. The .highlight class is more specific than the type selector, so the paragraph will be green.

The <p id="special"> element will be orange because the ID selector is more specific than the type selector.

Always keep specificity in mind when writing CSS to avoid unexpected results and to ensure your styles apply as intended.

question mark

Which of these selectors is the most specific?

Select the correct answer

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 2. Kapitel 6

Fråga AI

expand

Fråga AI

ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

Awesome!

Completion rate improved to 6.67

bookSpecificity

Svep för att visa menyn

Understanding how CSS decides which rule to apply is essential, especially when multiple selectors target the same element. This decision is made using specificity, a system that calculates which selector is more "specific" and should therefore win when rules conflict.

Specificity is determined by assigning values based on the types of selectors used:

  • Inline styles: highest specificity (not shown in this example);
  • ID selectors (#id): high specificity;
  • Class selectors (.class), attribute selectors, and pseudo-classes: medium specificity;
  • Type selectors (element) and pseudo-elements: low specificity.

To break it down, specificity is calculated as a four-part value (a, b, c, d):

  • a: inline styles (not shown here);
  • b: number of ID selectors;
  • c: number of class selectors, attributes, and pseudo-classes;
  • d: number of element selectors and pseudo-elements.
index.html

index.html

style.css

style.css

copy

For the rules in the example, here is how specificity applies:

  • p { color: blue; } has specificity (0,0,0,1) — only a type selector;
  • .highlight { color: green; } has specificity (0,0,1,0) — one class selector;
  • #special { color: orange; } has specificity (0,1,0,0) — one ID selector;
  • #main-title.highlight { color: purple; } has specificity (0,1,1,0) — one ID and one class.

When more than one rule matches, the rule with the highest specificity wins. If two selectors have the same specificity, the one that comes last in the stylesheet is applied.

For example, the <h1 id="main-title" class="highlight"> element matches both .highlight and #main-title.highlight. The latter is more specific because it combines an ID and a class, so it will set the color to purple.

Similarly, <p class="highlight"> matches both the p and .highlight rules. The .highlight class is more specific than the type selector, so the paragraph will be green.

The <p id="special"> element will be orange because the ID selector is more specific than the type selector.

Always keep specificity in mind when writing CSS to avoid unexpected results and to ensure your styles apply as intended.

question mark

Which of these selectors is the most specific?

Select the correct answer

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 2. Kapitel 6
some-alt