What if you could style every element on your page with just one simple symbol?
Why Universal selector in CSS? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine you want to change the font style or add a border to every single element on your webpage. You start by writing CSS rules for each tag like <p>, <h1>, <div>, <nav>, and so on, one by one.
This approach is slow and tiring. If you add new elements later, you must remember to update your CSS everywhere. It's easy to miss some tags, causing inconsistent styles and extra work.
The universal selector (*) lets you target all elements at once. You write one simple rule, and it applies everywhere, saving time and avoiding mistakes.
p { margin: 0; }
h1 { margin: 0; }
div { margin: 0; }* { margin: 0; }You can quickly apply base styles or resets to every element, making your design consistent and your CSS easier to maintain.
When starting a new website, developers often use the universal selector to remove default spacing from all elements, so they can build their own clean layout from scratch.
Writing separate rules for every element is slow and error-prone.
The universal selector (*) targets all elements with one rule.
This makes styling faster, consistent, and easier to maintain.
Practice
* do?Solution
Step 1: Understand the universal selector symbol
The*symbol in CSS means "all elements" without exception.Step 2: Apply the selector meaning
Using*targets every element on the page, regardless of tag, class, or ID.Final Answer:
Selects all elements on the page -> Option CQuick Check:
Universal selector = all elements [OK]
- Thinking * selects only specific tags
- Confusing * with class or ID selectors
- Assuming * targets only visible elements
Solution
Step 1: Identify the universal selector syntax
The universal selector is just*without any prefix like dot or hash.Step 2: Check the CSS rule format
The correct CSS rule to apply margin 0 to all elements is* { margin: 0; }.Final Answer:
* { margin: 0; } -> Option DQuick Check:
Universal selector syntax = * { ... } [OK]
- Adding dot or hash before *
- Using invalid selector names like 'all'
- Forgetting curly braces
* { padding: 10px; }
p { padding: 5px; }What will be the padding of a paragraph (
<p>) element?Solution
Step 1: Understand selector specificity
The universal selector*applies padding 10px to all elements, but thepselector is more specific.Step 2: Apply CSS specificity rules
Sincepselector is more specific than*, the paragraph's padding will be 5px, overriding the universal selector.Final Answer:
5px -> Option BQuick Check:
More specific selector wins = 5px [OK]
- Assuming * always overrides other selectors
- Adding padding values instead of overriding
- Ignoring CSS specificity rules
* { font-size: 16px }But the browser ignores it and uses default font sizes. What is the likely error?
Solution
Step 1: Check CSS syntax
The CSS rule* { font-size: 16px }is syntactically valid; a trailing semicolon is optional at the end of a rule.Step 2: Understand common issues
If valid CSS is ignored by the browser, the stylesheet is likely not linked properly in the HTML with a <link rel="stylesheet" href="..."> tag.Final Answer:
CSS file not linked properly -> Option AQuick Check:
No styles applied? Check <link> tag [OK]
- Thinking universal selector can't style fonts
- Using quotes around numeric values
- Ignoring CSS file linking issues
<a>) with no margin and 5px padding. Which CSS achieves this?Solution
Step 1: Reset all elements margin and padding
The universal selector* { margin: 0; padding: 0; }sets margin and padding to zero for all elements.Step 2: Override padding for links
The selectora { padding: 5px; }specifically sets padding to 5px for all<a>elements, overriding the universal selector.Final Answer:
* { margin: 0; padding: 0; } a { padding: 5px; } -> Option AQuick Check:
Specific selector overrides universal [OK]
- Placing universal selector after specific selector
- Nesting selectors incorrectly
- Setting wrong padding values
