Apply style to ALL elements

Learn apply style to all elements with practical examples, diagrams, and best practices. Covers css development techniques with visual explanations.

Styling Every Element: The Universal Selector in CSS

Hero image for Apply style to ALL elements

Learn how to use the universal selector (*) in CSS to apply styles to all elements on a webpage, understand its implications, and discover best practices for its effective use.

In CSS, the universal selector, denoted by an asterisk (*), is a powerful tool that allows you to select and apply styles to every single element within a document. While incredibly versatile, its broad reach requires careful consideration to avoid unintended consequences and maintain performance. This article will guide you through the fundamentals of the universal selector, its common use cases, and important considerations for its application.

Understanding the Universal Selector (*)

The universal selector matches any element type. This means if you apply a style to *, that style will cascade down to every HTML element on your page, including <body>, <div>, <p>, <a>, <img>, and so on. It's often used for global resets or to establish baseline styles across an entire project.

/* Basic usage of the universal selector */
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-family: Arial, sans-serif;
}

A common CSS reset using the universal selector to remove default margins and paddings, set box-sizing, and define a base font.

Common Use Cases and Examples

Beyond basic resets, the universal selector can be combined with other selectors or used for specific global styling needs. Here are a few practical examples:

flowchart TD
    A[Start Styling] --> B{"Apply to ALL elements?"}
    B -->|Yes| C["Use Universal Selector (*)"]
    C --> D["Global Reset (margin, padding, box-sizing)"]
    C --> E["Base Font/Color (e.g., font-family, color)"]
    C --> F["Debug Outlines (e.g., border: 1px solid red)"]
    B -->|No| G["Use Specific Selectors (e.g., .class, #id, element)"]
    G --> H[Targeted Styling]
    D --> I[Consistent Layout]
    E --> J[Consistent Typography]
    F --> K[Visual Debugging]
    I & J & K --> L[End Styling]

Decision flow for when to use the universal selector in CSS.

/* Example: Applying a default text color to all elements */
* {
  color: #333;
}

/* Example: Adding a subtle border for debugging purposes */
* {
  border: 1px solid rgba(0, 0, 0, 0.1);
}

/* Example: Combining with other selectors (less common but possible) */
div * {
  /* Styles applied to all elements *inside* a div */
  background-color: #f0f0f0;
}

Examples demonstrating global text color, debugging borders, and a combined selector.

Performance and Specificity Considerations

While convenient, using the universal selector extensively can have performance implications, especially on large, complex documents. Browsers have to check every single element against the * selector, which can be computationally intensive. Additionally, styles applied with * have very low specificity, meaning they are easily overridden by more specific selectors (e.g., element selectors, class selectors, ID selectors).

To mitigate performance issues and maintain control, it's often better to apply styles to specific elements or use class-based styling whenever possible. The universal selector is best reserved for foundational styles that truly need to affect everything.

1. Identify Global Needs

Determine if the style truly needs to apply to every single element on the page (e.g., a CSS reset, a base font-family, or a debugging outline).

2. Apply Universal Styles

Place your universal selector styles at the beginning of your stylesheet to ensure they are applied first and can be easily overridden by more specific rules.

3. Override with Specificity

For any elements that require different styling, use more specific selectors (e.g., element, class, ID) to override the universal styles. This is a natural part of the CSS cascade.

4. Monitor Performance

If you notice performance issues, review your use of the universal selector. Consider if more targeted selectors could achieve the same result with less overhead.