bugl
bugl
HomeLearnPatternsPathsSearch
HomeLearnPatternsPathsSearch

Loading lesson path

Learn/CSS/CSS Foundations
CSS•CSS Foundations

CSS Specificity

Flash cards

Review the key moves

1/4
Core idea

What is the main idea behind CSS Specificity?

Lesson checks

Practice each idea before moving on

Short Mimo-style checks built from this lesson's code, terms, and sequence.

1Quick choice

Which statement best captures the main point of this lesson?

2Fill blank

Complete the missing token from the example code.

p {___: red;}
3Order

Put the learning moves in the order that makes the concept easiest to apply.

Now, look at next example:
If two or more CSS rules point to the same element, the declaration with the highest specificity will "win", and that style will be applied to the HTML element.
CSS specificity is an algorithm that determines which style declaration is ultimately applied to an element.

CSS specificity is an algorithm that determines which style declaration is ultimately applied to an element.

If two or more CSS rules point to the same element, the declaration with the highest specificity will "win", and that style will be applied to the HTML element.

Example

Formatted code
  <html>
<head>
  <style>
    p {color: red;}

  </style>
</head>
<body>
<p>Hello World!</p>

</body>

  </html>

Live preview

Expected output

The text will be red: <html> <head> <style> p {color: red;} </style> </head> <body> <p>Hello World!</p> </body> </html>

Look at the following examples

Now, look at next example:

Example

Formatted code
  <html>
<head>
  <style>
    .test {color: green;}

    p {color: red;}
  </style>
</head>
<body>

<p class="test">Hello World!</p>
</body>
</html>

Live preview

Expected output

The text will be green, because the class selector has higher specificity: <html> <head> <style> .test {color: green;} p {color: red;} </style> </head> <body> <p class="test">Hello World!</p> </body> </html>

Now, look at next example:

Example

Formatted code
  <html>
<head>
  <style>
    #demo {color: blue;}

  .test {color: green;}
    p {color: red;}

  </style>
</head>
<body>
<p id="demo" class="test">Hello
  World!</p>
</body>
</html>

Live preview

Expected output

The text will be blue, because the id selector has higher specificity: <html> <head> <style> #demo {color: blue;} .test {color: green;} p {color: red;} </style> </head> <body> <p id="demo" class="test">Hello World!</p> </body> </html>

Now, look at next example:

Example

Formatted code
  <html>
<head>
  <style>
    #demo {color: blue;}

  .test {color: green;}
    p {color: red;}

  </style>
</head>
<body>
<p id="demo" class="test"
  style="color: pink;">Hello World!</p>
</body>
</html>

Live preview

Expected output

The text will be pink, because the inline style has the highest specificity: <html> <head> <style> #demo {color: blue;} .test {color: green;} p {color: red;} </style> </head> <body> <p id="demo" class="test" style="color: pink;">Hello World!</p> </body> </html>

Previous

CSS Inheritance

Next

CSS Specificity Hierarchy