The universal selector helps you style all elements on a webpage at once. It saves time when you want a common style everywhere.
Universal selector in CSS
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
CSS
* { property: value; }The asterisk (*) means 'all elements'.
Put the styles inside curly braces { }.
Examples
CSS
* { margin: 0; padding: 0; }CSS
* { font-family: Arial, sans-serif; }CSS
* { box-sizing: border-box; }Sample Program
This example uses the universal selector (*) to remove all default margin and padding, set a font, and add a light background color to every element. Then it adds some spacing and colors to specific elements.
CSS
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Universal Selector Example</title> <style> * { margin: 0; padding: 0; font-family: Arial, sans-serif; background-color: #f0f8ff; } body { padding: 1rem; } h1 { color: #333; margin-bottom: 1rem; } p { color: #555; } </style> </head> <body> <h1>Welcome!</h1> <p>This page uses the universal selector to style all elements.</p> </body> </html>
Important Notes
The universal selector can slow down your page if overused with complex styles.
It applies to every element, so be careful when combining it with other selectors.
Use it mainly for simple, broad styles like resets or fonts.
Summary
The universal selector (*) targets all elements on the page.
It is useful for setting common styles like margin, padding, and fonts.
Use it wisely to avoid unexpected style changes or performance issues.
Practice
1. What does the CSS universal selector
* do?easy
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]
Hint: Remember * means every element on the page [OK]
Common Mistakes:
- Thinking * selects only specific tags
- Confusing * with class or ID selectors
- Assuming * targets only visible elements
2. Which of the following is the correct syntax to apply a margin of 0 to all elements using the universal selector?
easy
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]
Hint: Universal selector is just * without dot or hash [OK]
Common Mistakes:
- Adding dot or hash before *
- Using invalid selector names like 'all'
- Forgetting curly braces
3. Given this CSS code:
What will be the padding of a paragraph (
* { padding: 10px; }
p { padding: 5px; }What will be the padding of a paragraph (
<p>) element?medium
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]
Hint: More specific selector overrides * [OK]
Common Mistakes:
- Assuming * always overrides other selectors
- Adding padding values instead of overriding
- Ignoring CSS specificity rules
4. You wrote this CSS:
But the browser ignores it and uses default font sizes. What is the likely error?
* { font-size: 16px }But the browser ignores it and uses default font sizes. What is the likely error?
medium
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]
Hint: If * styles not applying, verify CSS file link [OK]
Common Mistakes:
- Thinking universal selector can't style fonts
- Using quotes around numeric values
- Ignoring CSS file linking issues
5. You want to reset margin and padding for all elements but keep links (
<a>) with no margin and 5px padding. Which CSS achieves this?hard
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]
Hint: Use * first, then override with specific selector [OK]
Common Mistakes:
- Placing universal selector after specific selector
- Nesting selectors incorrectly
- Setting wrong padding values
