๐Ÿงฑ CSS Basic Selectors
Estimated reading: 3 minutes 427 views

CSS Universal Selectors โ€“ Select All with One Rule

The universal selector is a powerful CSS tool that allows you to target every element on your webpage with a single character. When you need to apply consistent styling across your entire document or reset default browser styles, the universal selector becomes your best ally. In this comprehensive guide, we’ll explore everything you need to know about this essential selector, from basic syntax to advanced usage patterns.


What Is a CSS Universal Selector?

The universal selector in CSS is represented by an asterisk (*) and matches elements of any type in your HTML document. Unlike other selectors that target specific elements, classes, or IDs, the universal selector applies styles to everything in the DOM (Document Object Model).

* {
color: blue;
}

This simple rule would change the text color of every element on your page to blue.

The universal selector acts as a “wildcard” that captures all elements regardless of their type, making it ideal for establishing baseline styles or performing global resets[2].


Syntax of Universal Selector

The syntax for using the universal selector is straightforward:

* {
/* CSS declarations */
property: value;
}

For example:

* {
padding: 2px;
margin: 5px;
}

It’s worth noting that the asterisk is optional with simple selectors. For instance, *.warning and .warning are equivalent[1].


Practical Use Cases

1๏ธโƒฃ CSS Reset or Normalize

* {
margin: 0;
padding: 0;
box-sizing: border-box;
}

This simple reset removes default margins and paddings.


2๏ธโƒฃ Applying Base Styles

* {
font-family: 'Arial', sans-serif;
line-height: 1.5;
color: #333;
}

Establish a consistent typography foundation across your entire website.


3๏ธโƒฃ Debugging Layout Issues

* {
outline: 1px solid red;
}

This is like having x-ray vision for your website’s structure!


Cautions and Performance Considerations

* { color: black; }      
p { color: blue; }
.note { color: green; }

The universal selector has zero specificity. It’s perfect for defaults but can be overridden easily by more specific selectors.


Combining Universal Selector with Other Selectors

โœณ๏ธ With Pseudo-elements

*, *::before, *::after {
box-sizing: border-box;
}

This ensures layout predictability across all elements and their pseudo-elements.


โœณ๏ธ With Attribute Selectors

*[lang^="en"] {
color: green;
}

Target all elements with lang attribute starting with “en”.


โœณ๏ธ With Pseudo-classes

*:hover {
cursor: pointer;
}

Be cautious: This makes everything react to hover states.


Comparison: Universal vs. Other Basic Selectors

Selector Type Example Specificity Usage Scenario
Universal*0,0,0Global resets, debugging, base styles
Typediv0,0,1Styling specific HTML elements
Class.container0,1,0Reusable component styles
ID#header1,0,0Unique element styling

CSS Universal Selectors Best Practices

  1. Start with box-sizing reset *, *::before, *::after { box-sizing: border-box; }
  2. Use for debugging temporarily
  3. Consider inheritance alternatives
  4. Be cautious with performance-heavy properties
  5. ๐Ÿ‘“ Improve selector readability article *:first-child { font-weight: bold; }

Summary: CSS Universal Selectors

The universal selector is perfect for:

  • Baseline resets
  • Layout debugging
  • Global font and spacing control

Use with care to avoid unintended cascading effects and specificity issues.

With smart use, this tiny selector can bring big benefits to your CSS architecture.


FAQs About CSS Universal Selectors

What does the * symbol do in CSS?

The asterisk (*) in CSS is the universal selector that matches all elements of any type on a webpage.

Is using the universal selector bad for performance?

Modern browsers handle the universal selector efficiently, but avoid applying heavy styles to all elements.

How do I use the universal selector with pseudo-elements?

*, *::before, *::after {
/* your styles here */
}

Use this to include all pseudo-elements in your styling scope.

Can the universal selector be overridden?

Yes, since it has zero specificity, it can be overridden by any other selector.

Does the universal selector affect pseudo-elements by default?

No. You must explicitly include ::before and ::after.


Share Now :
Share

๐ŸŒCSS Universal Selectors

Or Copy Link

CONTENTS
Scroll to Top