CSS
What Does * (Asterisk) Mean in CSS?
In CSS, the *
(asterisk) is known as the universal selector. It is a powerful selector that targets all elements on a webpage, making it useful for applying global styles, resetting margins and padding, and simplifying styling for consistent layouts.
In this blog, we’ll explore how the universal selector works, its use cases, and best practices for using it effectively in web development.
Understanding the Universal Selector (*
) in CSS
The *
selector matches all elements in an HTML document. Its basic syntax is:
* {
/* CSS properties */
}
This means any style rule applied using *
will affect every element on the page unless overridden by more specific selectors.
Example 1: Changing the Font of All Elements
* {
font-family: Arial, sans-serif;
}
<h1>Heading 1</h1>
<p>This is a paragraph.</p>
<button>Click Me</button>
In this example, all text elements (h1
, p
, button
, etc.) will be displayed using the Arial font.
Common Use Cases for *
in CSS
1. CSS Reset – Removing Default Margins and Padding
By default, browsers apply their own styles to elements, including margins and padding. The universal selector is often used to reset these styles and create a consistent starting point.
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
🔹 Why Use This?
- Removes unwanted spacing and ensures consistency across different browsers.
- The
box-sizing: border-box;
rule ensures that padding and borders don’t affect an element’s total width and height.
2. Applying a Default Style to All Elements
If you want to set a universal style for all elements, such as a default text color or background color, you can use *
.
* {
color: #333;
background-color: #f9f9f9;
}
This will apply a dark gray text color (#333
) and a light gray background (#f9f9f9
) to the entire page.
3. Selecting All Elements Within a Specific Container
You can combine the universal selector with other selectors to style all child elements of a specific parent.
.container * {
font-size: 18px;
}
<div class="container">
<p>This paragraph inside the container will have 18px text.</p>
<button>Button</button>
</div>
🔹 Use Case: Useful for applying styles only within a particular section of a webpage.
4. Applying a Border to Every Element (Debugging Purpose)
When debugging layout issues, you can use *
to add borders around all elements to visually inspect their sizes and spacing.
* {
border: 1px solid red;
}
This helps identify misaligned elements, unwanted margins, and overlapping sections.
When NOT to Use the Universal Selector
While the *
selector is powerful, it should be used carefully to avoid performance issues and unintended styling conflicts. Here are some scenarios where you should avoid using it:
❌ Applying Heavy Styles to All Elements
* {
font-size: 20px;
font-weight: bold;
}
👉 This could make all text elements too large or too bold, affecting readability.
❌ Using *
Too Broadly in Large Projects
- The universal selector can negatively impact performance, especially in large projects with many elements.
- Instead, use more specific selectors like
body
,.container
, or element-based selectors (p
,h1
, etc.).
❌ Overriding Important Component Styles
- If using
*
, ensure it doesn’t override styles of important UI components like buttons, forms, or third-party libraries.
Best Practices for Using *
in CSS
✔ Use for Global Resets Sparingly
- Instead of using
*
, consider using a well-structured CSS reset like Normalize.css.
✔ Combine with Other Selectors
- Use
*
inside specific containers to limit its scope.
✔ Use for Debugging, Then Remove It
- Apply
*
with debugging styles (e.g.,border: 1px solid red;
), then remove it before final deployment.
✔ Optimize for Performance
- Avoid setting complex styles globally with
*
to prevent slow rendering.
Conclusion
The *
universal selector in CSS is a powerful tool that allows you to target all elements on a webpage. It is commonly used for CSS resets, debugging, and global styling, but should be used with caution to prevent performance issues and unintended styling overrides.
By using the universal selector strategically and following best practices, you can create cleaner, more maintainable CSS while ensuring a consistent user experience across different devices and browsers.