Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Understanding CSS Specificity Rules
📖 Scenario: You are creating a simple webpage with a heading and a paragraph. You want to learn how CSS specificity affects which styles are applied when multiple rules target the same element.
🎯 Goal: Build a small HTML page with a heading and paragraph, then write CSS rules with different selectors to see which style wins based on specificity.
📋 What You'll Learn
Create an HTML skeleton with a <h1> and a <p> element
Write CSS rules using element selectors, class selectors, and ID selectors
Apply conflicting color styles to the same elements using different selectors
Observe which CSS rule applies based on specificity
💡 Why This Matters
🌍 Real World
Web developers often need to control which CSS styles apply to elements, especially when multiple styles conflict. Understanding specificity helps them write CSS that works as intended.
💼 Career
Knowing CSS specificity is essential for front-end developers to maintain and debug styles in websites and web applications.
Progress0 / 4 steps
1
Create the HTML structure
Write the HTML code to create a <!DOCTYPE html> document with <html lang="en">, <head> including <meta charset="UTF-8"> and <title> 'Specificity Demo', and a <body> containing a <h1> with text 'Welcome' and a <p> with text 'This is a paragraph.'
CSS
Hint
Start by writing the basic HTML5 document structure with a heading and paragraph inside the body.
2
Add CSS with element selectors
Inside a <style> tag in the <head>, write CSS rules that set the color of h1 elements to blue and p elements to green using element selectors.
CSS
Hint
Use simple element selectors like h1 and p to set their text colors.
3
Add CSS with class selectors
Add a class called highlight to the <p> element in the HTML. Then, inside the <style> tag, add a CSS rule using the class selector .highlight that sets the color to red.
CSS
Hint
Add the class attribute to the paragraph and write a CSS rule for .highlight to change its color.
4
Add CSS with ID selector and observe specificity
Add an ID called main-title to the <h1> element in the HTML. Then, inside the <style> tag, add a CSS rule using the ID selector #main-title that sets the color to orange. This will override the previous h1 color because ID selectors have higher specificity.
CSS
Hint
Add the ID attribute to the heading and write a CSS rule for #main-title to change its color.
Notice how this color overrides the previous h1 color.
Practice
(1/5)
1. Which CSS selector has the highest specificity?
easy
A. An ID selector like #header
B. A class selector like .menu
C. An element selector like div
D. A universal selector like *
Solution
Step 1: Understand selector types and specificity
ID selectors have the highest specificity, followed by class selectors, then element selectors.
Step 2: Compare given selectors
#header is an ID selector, which beats class .menu and element div selectors.
Final Answer:
An ID selector like #header -> Option A
Quick Check:
ID selector > class selector > element selector [OK]
Hint: ID selectors always outrank classes and elements [OK]
Common Mistakes:
Thinking class selectors have higher specificity than IDs
Confusing element selectors with class selectors
Ignoring the universal selector has lowest specificity
2. Which of the following CSS selectors is written with correct syntax?
easy
A. .container > #main .item
B. #main .container > .item#
C. .container #main .item#
D. #main > .container .item#
Solution
Step 1: Check each selector for valid CSS syntax
Valid selectors use IDs with # before the name, classes with ., and combinators like > properly placed.
Step 2: Identify invalid parts
Options A, B, and D end with # which is invalid syntax. .container > #main .item is correctly formed.