- CSS Tutorial
- CSS Introduction
- CSS Syntax
- CSS Comments
- CSS Selectors
- CSS Fonts
- CSS Colors
- CSS Backgrounds
- CSS Box Model
- CSS Borders
- CSS Margins
- CSS Padding
- CSS Text
- CSS Images
- CSS Links
- CSS Lists
- CSS Tables
- CSS Outline
- CSS Icons
- CSS Display
- CSS max-witdh
- CSS Position
- CSS z-index
- CSS Overflow
- CSS Float
- CSS Align
- CSS Opacity
- CSS Navigation Bar
- CSS Dropdowns
- CSS Forms
- CSS Units
- CSS !important
- CSS Specificity
- CSS Combinators
- CSS inline-block
- CSS Hover
- CSS Cursors
- CSS Selectors
- CSS Type Selector
- CSS Class Selector
- CSS ID Selector
- CSS Attribute Selector
- CSS Pseudo-class Selector
- CSS Pseudo-element Selector
- CSS Universal Selector
- CSS Advanced
- CSS Text Formatting
- CSS Gradients
- CSS Shadow
- CSS Rounded Corners
- CSS Text Effects
- CSS 2D Transform
- CSS 3D Transform
- CSS Border Images
- CSS Inherit
- CSS Transitions
- CSS Animations
- CSS Box Sizing
- CSS Tooltip
- CSS Masking
- CSS Pagination
- CSS Styling Images
- CSS object-fit
- CSS object-position
- CSS Buttons
- CSS Multiple Columns
- CSS Variables
- CSS Flexbox
- CSS Grid
- CSS Media Queries
CSS Universal Selector
The universal selector in CSS is represented by an asterisk (*). It is a powerful "wildcard" selector that matches every single element on a web page, from the root <html> tag down to the deepest nested paragraph or span.
Because it targets everything, it is typically used to apply global styles or to perform a "CSS Reset" to ensure a consistent look across different web browsers.
Basic Usage Example:
/* Selects every element on the page */
* {
text-align: center;
color: blue;
}
In this example, every piece of text, every heading, and every container on your site will inherit center alignment and a blue text color. While this is great for demonstration, applying such broad styles in a production environment is rare because it removes the unique styling of specific elements like headers or buttons.
* { outline: 1px solid red; }. This will draw a box around every element on the page, helping you instantly see where margins, padding, or alignment issues are happening.
The Most Common Real-World Use Case
In modern web development, the universal selector is most frequently used to set box-sizing. By default, adding padding or borders to an element increases its physical width. Most developers prefer the border-box model, where padding and borders are included inside the defined width.
/* The industry standard "Reset" */
*, *::before, *::after {
box-sizing: border-box;
margin: 0;
padding: 0;
}
*::before and *::after when using the universal selector for box-sizing. This ensures that pseudo-elements also behave predictably within your layout.
Understanding Specificity
One of the most important things to remember is that the universal selector has a specificity of 0 (0,0,0,0). This means that any other selector—whether it's a class (.button), an ID (#header), or even a basic element selector (p)—will override the styles defined in the universal selector.
h1 { color: red; } will override * { color: blue; }.
Scoped Universal Selector
You don't always have to use the asterisk on its own. You can "scope" it to apply to all children within a specific parent element. This is useful when you want to apply a rule to everything inside a specific section without affecting the rest of the page.
/* Selects all elements inside the .container class */
.container * {
border: 1px solid #ccc;
}
.main-nav * * *) can cause performance issues on very large, complex DOM trees because the browser has to check every single element. Use scoped universal selectors sparingly.