Sub topic 3
The universal selector
This chapter covers the * selector: what it matches, what it costs in specificity, and where it behaves unexpectedly. It assumes you can write type, class, and ID selectors, and that you know a rule has a selector and a declaration block. You should also know that specificity is a score used to settle conflicts. The full calculation comes in a later chapter. Here you only need the zero-score case.
What `*` matches
The universal selector is written *. It matches every element in the document, whatever its type, class, or ID.
* {
box-sizing: border-box;
}This rule applies box-sizing: border-box to html, body, div, p, input, and every other element. It does not match text nodes, comments, or pseudo-elements. A pseudo-element is a generated part of an element, such as ::before. The last point causes a common reset mistake, covered below.
It is usually implied
Inside a compound selector, * is optional. A compound selector is a chain of simple selectors on one element with no spaces between them. These pairs are identical:
*.note { color: #555; }
.note { color: #555; }
*[hidden] { display: none; }
[hidden] { display: none; }Writing * is only required when nothing else would stand in that position, such as the bare * in .card > *. Omitting it elsewhere is the common style.
Specificity of `*`
The universal selector adds nothing to specificity. Specificity is written as an (IDs, classes/attributes/pseudo-classes, types/pseudo-elements) triple. A lone * scores (0,0,0). Combinators also add nothing.
* { color: black; } /* (0,0,0) */
* + * { margin-top: 1rem; } /* (0,0,0) */
.card * { color: gray; } /* (0,1,0) */
div * { color: gray; } /* (0,0,1) */
*.note { color: green; } /* (0,1,0) */
#main > * { color: navy; } /* (1,0,0) */In each case the score comes from the other parts of the selector. A selector with only * is as weak as a selector can be. Any type selector, such as p, beats it. Equal scores fall back to source order, so a later * rule can still override an earlier * rule.
Common uses
Global resets
The most common use is a small reset applied once at the top of a stylesheet:
*,
*::before,
*::after {
box-sizing: border-box;
}The three-part list exists because * does not match ::before or ::after. Without the pseudo-element entries, generated content keeps the default content-box sizing and can break layouts that rely on border-box math. A comma-separated list like this is a selector list. Each entry is evaluated separately.
Spacing between siblings
Combining * with the adjacent-sibling combinator + gives a compact "space between neighbors" rule:
.stack > * + * {
margin-top: 1rem;
}This selects every child of .stack that has a previous sibling, so the first child gets no margin. The specificity is (0,1,0), from .stack alone.
Targeting all children of a container
.toolbar > * {
margin-right: 0.5rem;
}This is useful when the children vary in type. Adding or changing a child element requires no selector edits.
Debug outlines
A temporary outline on every element shows the box structure of a page:
* {
outline: 1px solid rgba(255, 0, 0, 0.3);
}outline does not affect layout, unlike border. That makes it safe for this purpose. Remove the rule before shipping.
The inheritance trap
Some properties, such as color and font-family, are inherited. An element with no rule for them takes the parent's computed value. The mistake with * is that it matches every element directly. A directly matched value always beats an inherited one, even when the matching rule is as weak as (0,0,0).
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Universal selector trap</title>
<style>
* { color: black; }
.card { color: rebeccapurple; }
</style>
</head>
<body>
<div class="card">
<p>Which color is this paragraph?</p>
</div>
</body>
</html>The div is purple, because .card (0,1,0) beats * (0,0,0) on that element. The paragraph is black. The p is matched directly by *, so it never needs to inherit from its parent. Inheritance only applies when no rule targets the element for that property.
Without the * rule, the paragraph would inherit purple from .card. This is the usual reason * { color: ...; } or * { font-family: ...; } makes themes hard to apply. Setting such properties on body or html and letting them inherit keeps them easy to change per section.
Form controls and other replaced elements
Inputs, buttons, and select elements often have browser-supplied styles. A broad * rule overrides many of them at once. A * { margin: 0; padding: 0; } reset flattens the default spacing on every control. That is sometimes intended, but it can also remove the padding that makes a checkbox group or a button readable. If you add this reset, check form controls and lists afterward.
Keeping a universal rule easy to override
Because * scores zero, a global rule is already easy to beat. The trouble comes from the compound forms that look harmless:
.page * { margin: 0; } /* (0,1,0) */
#app * { margin: 0; } /* (1,0,0) */These are no longer weak. #app * has an ID's weight, so a later .button { margin: 1rem; } at (0,1,0) loses to it on every element inside #app. If you want a scoped reset that stays weak, wrap it in :where(), which always contributes zero:
:where(.page) * {
margin: 0;
}This has specificity (0,0,0), because both :where(.page) and * add nothing. :where() is covered in a later chapter.
Performance
Browsers match selectors from right to left, and a bare * is cheap to evaluate. Modern engines handle it well, so a single * reset is not a measurable cost. Cost grows with how much work the rest of the selector does. A page-wide rule that sets expensive properties, such as large shadows, on every element can be slow, but the selector is not the cause.
Namespaces
In documents that mix XML namespaces, such as inline SVG in XML, *|* matches any element in any namespace, and ns|* matches any element in one namespace. In ordinary HTML pages you can ignore this. Plain * already matches HTML and inline SVG elements.
Common errors
| Mistake | Effect | Fix |
|---|---|---|
| `* { box-sizing: border-box; }` only | Pseudo-elements keep `content-box` | Add `*::before, *::after` |
| `* { color: #222; }` | Blocks inheritance, so section colors do not cascade | Set `color` on `body` |
| `#app * { ... }` for a "weak" reset | Selector scores (1,0,0) and wins over many class rules | Use `:where(#app) *` |
| `.stack > *` expecting text to match | Text nodes are not elements | Wrap text in an element |
| Assuming `*` wins by "being global" | It scores (0,0,0) and loses to any type selector | Compare scores, not reach |
Exercise
Given * { color: black; } and .card { color: rebeccapurple; }, and the markup <div class="card"><p>Hello</p></div>, what color is the paragraph, and why?
Show answer
Black. The * rule matches the p directly, so the paragraph does not inherit from .card. A directly matched value beats an inherited one, even though * has a specificity of (0,0,0). Removing the * rule would make the paragraph purple through inheritance.
Exercise
What is the specificity of #nav > * + .item, and which part contributes the score?
Show answer
(1,1,0). #nav adds one ID and .item adds one class. The * and the combinators > and + add nothing.
Exercise
Write a reset that sets box-sizing: border-box on all elements, including generated ::before and ::after content.
Show answer
*, *::before, *::after { box-sizing: border-box; }. The * alone does not match pseudo-elements, so they must be listed explicitly.