Universal Selector
The Universal Selector in CSS, represented by the asterisk symbol (*), is a powerful tool that targets every element on a web page simultaneously. Imagine you are building a house: before painting each room with a unique color or adding furniture, you might first apply a base coat across all walls. Similarly, the Universal Selector allows you to apply foundational styles to all elements, creating a uniform starting point for more detailed, component-specific styling.
Its importance lies in its ability to simplify initial styling and create consistency across different browsers and devices. In a portfolio website, you can use it to remove default margins and paddings to ensure that image galleries and text blocks align perfectly. For a blog, it can reset spacing to achieve a clean layout without unexpected gaps. In an e-commerce store, it ensures product cards, buttons, and text start from a consistent base style. A news site benefits from cross-browser consistency, while a social platform leverages it to keep feed items and UI elements visually aligned.
By the end of this tutorial, you will understand how to use the Universal Selector effectively, learn about its syntax, explore advanced applications, and avoid common pitfalls. You will be able to initialize your page like organizing a library—first putting everything in order—before arranging each “book” (or element) in its proper place.
Basic Example
css/* Universal Selector to reset and normalize styles for all elements */
* {
margin: 0; /* Remove all default margins */
padding: 0; /* Remove all default paddings */
box-sizing: border-box; /* Include border and padding in element's total width/height */
}
In this example, we use the Universal Selector (*) to target all elements in the document. Here’s the breakdown:
margin: 0;
removes all default margins. Many browsers automatically add margins to headings, paragraphs, and lists. On a blog or news site, this ensures your layout starts without unexpected gaps that could disrupt your grid or article alignment.padding: 0;
removes internal spacing (padding) from all elements. This is particularly helpful for lists (ul, ol) or form elements that often carry unwanted default indentation. For an e-commerce site, clearing padding ensures product cards or pricing tables align perfectly without adjusting every individual component.box-sizing: border-box;
is an advanced property that changes how the browser calculates element dimensions. Instead of adding padding and border to the declared width and height, they are included in the total measurement. For responsive or mobile-first designs, this makes layouts more predictable because you can set widths without worrying about overflow caused by padding or borders.
Beginners often ask if using*
will slow down performance. Modern browsers handle this efficiently for foundational resets. However, adding complex properties like box-shadows or animations to all elements can reduce performance. Universal Selector should primarily be used for initializing a consistent baseline and then combined with specific selectors for fine-tuning individual components.
Practical Example
css/* Universal Selector applied to a portfolio or e-commerce website */
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: "Helvetica Neue", Arial, sans-serif; /* Unified typography */
}
body {
background-color: #f9f9f9; /* Neutral background for a clean start */
}
.card {
background: white;
margin: 20px;
padding: 20px;
border-radius: 8px;
box-shadow: 0 2px 6px rgba(0,0,0,0.1);
}
Best practices and common mistakes when working with the Universal Selector are crucial for writing maintainable, efficient CSS.
Best Practices:
- Start with a clean slate: Use it at the very top of your stylesheet to normalize spacing and sizing across all elements.
- Mobile-first approach: Combine it with responsive strategies, ensuring layouts are predictable and flexible on smaller screens first.
- Performance awareness: Keep Universal Selector rules lightweight—focus on resets (margins, paddings, box-sizing, fonts).
-
Maintainable code: Pair the Universal Selector with modular class-based selectors for component-level styling.
Common Mistakes: -
Overusing heavy styles: Applying shadows, gradients, or transitions to every element drastically affects performance.
- Specificity conflicts: Relying on
*
for solving styling issues can lead to conflicts with more specific selectors or require frequent!important
overrides. - Ignoring responsive testing: Without checking across devices, uniform resets might cause text or components to collapse unexpectedly.
- Using
*
for single fixes: Instead of targeting the specific element, using the Universal Selector as a shortcut can make long-term maintenance difficult.
Debugging Tip: Use browser DevTools to inspect computed styles. If something is behaving unexpectedly, temporarily disable the Universal Selector block to check if the baseline reset is the cause.
📊 Quick Reference
Property/Method | Description | Example |
---|---|---|
* | Selects all elements on the page | * { margin:0; } |
margin | Removes default outer spacing for all elements | * { margin:0; } |
padding | Removes default inner spacing for all elements | * { padding:0; } |
box-sizing | Makes sizing predictable by including padding/border | * { box-sizing:border-box; } |
font-family | Applies a global font to all elements | * { font-family:Arial,sans-serif; } |
In summary, the Universal Selector (*) is a foundational tool for creating a consistent and predictable base across your entire web page. It acts as the “organizing library” step of web design—resetting margins, paddings, and sizing before arranging each component individually.
Key takeaways include:
- It targets all elements on the page.
- Ideal for initializing layouts in portfolio, blog, e-commerce, news, or social platform projects.
- Use it primarily for lightweight resets and avoid heavy styles to maintain performance.
In relation to HTML, it affects every element, giving developers a clean starting point. For JavaScript, a unified base ensures that DOM manipulations behave consistently without unexpected spacing or sizing differences.
Next steps include exploring type selectors, class selectors, and ID selectors for more precise control. You should also experiment with CSS variables, media queries, and component-based architectures to build scalable, maintainable websites. Consistent practice on real projects will help you balance simplicity, performance, and flexibility in professional web development.
🧠 Test Your Knowledge
Test Your Knowledge
Test your understanding of this topic with practical questions.
📝 Instructions
- Read each question carefully
- Select the best answer for each question
- You can retake the quiz as many times as you want
- Your progress will be shown at the top