Definition and Usage
The * selector (asterisk, also known as the universal selector) selects all elements in the document.
Basic Example
* {
color: #222222;
}
Note
Let’s look at the following example.
<style>
* {
border: 1px solid red;
padding: 10px;
}
p::before {
content: "Paragraph ::before ";
font-weight: 600;
color: blue;
background-color: gold;
}
</style>
<div>
<p>This is a paragraph element.</p>
<p>This is a paragraph with a <span>span</span> element.</p>
</div>
This is a paragraph element.
This is a paragraph with a span element.
In the example above, a pseudo-element is generated for the <p> using CSS p::before (the part that reads Paragraph ::before ), and the text and background are also styled.
This is one of the most common points of confusion when using the * selector, so be careful.
Using with Other Selectors
section * { /* Select all descendant elements of the section element */
background-color: gold;
}
<section>
<h3>Section title</h3>
<p>Section content</p>
</section>
Section title
Section content
In the example above, section * selects all elements inside the <section> element. The * selector can be used in combination with other selectors.
Common Use Cases of the * Selector
The * selector, which selects all elements in HTML, is commonly used to remove user agent styles—the default styles built into the browser—before any custom CSS is applied.
If you do not reset these default browser styles, unexpected styling may occur because you might not account for the browser's default settings when styling the actual web page. This can make CSS styling highly inconvenient.
The following CSS example shows how to reset the default margin and padding from all elements and then apply custom styles to specific elements.
/* Reset default styles built into the browser */
* {
padding: 0;
margin: 0;
}
/* Apply custom styles */
p {
margin-top: 0.5em;
margin-bottom: 1em;
}
button {
padding: 0.5em 1.5em;
}
Specifications
| Specification | |
|---|---|
*
|
Selectors Level 4 #the-universal-selector |
Browser Compatibility
| Selector |
Desktop Chrome
|
Desktop Edge
|
Desktop Firefox
|
Safari
|
|---|---|---|---|---|
*
|
1 | 12 | 1 | 1 |
References
See Also
- CSS Element Selector – Select by Element Name
- CSS Class Selector (.class) – Select Elements by Class Name
- CSS ID Selector (#id) – Select Elements by ID Value
- CSS :root Pseudo-Class – Select the Root Element of the Document
- CSS :not(selectorX) Pseudo-Class Selector – Select Elements NOT Matching Selector