- Home
- /
- Tutorials
- /
- CSS Tutorial
- /
- CSS Universal Selector
CSS Selectors
CSS Universal Selector
Use the universal selector deliberately for box-sizing resets, scoped defaults, and namespace-aware matching without applying unnecessary rules everywhere.
The universal selector, an asterisk, matches every element on the page. Its most common use is a short reset at the top of a stylesheet, especially * { box-sizing: border-box; }.
Its specificity is zero, so any other selector overrides it easily. But because it matches everything, a careless rule on * can cause problems in places you never looked at.
Syntax for Global Selector
css
* {
property: value;
}Example of Global Selector
css
* {
margin: 0;
padding: 0;
}That removes the browser's default margins and padding from every element.
Example
Global Selector Complete Example
html
<!DOCTYPE html>
<html>
<head>
<title>CSS Universal Selector</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
h1 {
color: blue;
}
p {
color: green;
padding: 10px;
}
</style>
</head>
<body>
<h1>Universal Selector Example</h1>
<p>This paragraph has no default margin.</p>
<p>Spacing is controlled using CSS.</p>
</body>
</html>The heading is blue and the paragraphs are green with 10px of padding. Because the reset removed all default margins, the paragraphs touch each other; spacing now has to be added on purpose.
Browser Support
Feature | Chrome | Edge | Firefox | Safari |
|---|---|---|---|---|
| Universal selector | 1 | 12 | 1 | 1 |
Versions show the first stable desktop release with unprefixed support. Data source: MDN Browser Compatibility Data 8.0.8.
A safer reset
Removing every margin also removes useful defaults, like the space between paragraphs and the indent of lists, which you then have to add back. Many modern resets only fix the things that cause trouble:
A small, common reset
css
*, *::before, *::after {
box-sizing: border-box;
}
body {
margin: 0;
}The universal selector does not match pseudo-elements, so ::before and ::after are listed separately. The reason this one rule is so popular is explained in Box Sizing.
Inside other selectors
* can also be scoped: .card * { margin: 0; } affects only what is inside cards, and .stack > * + * adds space between direct children, a well-known layout trick. Its zero weight is covered in CSS Specificity.
Good to know
* { font-size: 16px; }breaks inheritance: headings lose their size andemunits stop scaling. Set inherited properties onbodyinstead.- Modern browsers handle
*quickly. Performance is rarely a problem; unexpected side effects are.
CSS Universal Selector exercises
No outer space
Remove the margin of every element with one rule.
The HTML your code works on
<div class="card">
<h2>Title</h2>
<p>Text</p>
<ul>
<li>One</li>
</ul>
</div>
<p class="out">Outside</p>Result
