Open In App

CSS * (Universal) Selector

Last Updated : 04 Jan, 2025
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

The universal selector (*) applies styles to all elements on a page or within a specified context, regardless of their type, class, or ID. It’s commonly used for global resets or universal styling.

* {
/* styles */
}

1. Basic Use case of universal selector

The universal selector applies styles to all elements in the document, making it perfect for resets or global rules. The margin: 0; and padding: 0; reset the default browser margins and paddings for all elements.


    <style>
        * {
            margin: 0;
            padding: 0;
        }
    </style>

2. Applying Default Styles to All Elements

You can use the universal selector to set consistent styles like box-sizing or font across all elements .The box-sizing: border-box; makes element dimensions include padding and borders, while font-family ensures consistent typography.


    <style>
        * {
            box-sizing: border-box;
            font-family: Arial, sans-serif;
        }
    </style>

3. Highlighting Elements with Universal Selector

The universal selector is useful for debugging layouts by adding borders or outlines. The outline: 1px solid red; highlights the boundaries of every element.


    <style>
        :root {
            outline: none;
        }
        * {
            outline: 1px solid red;
        }
        div {
            display: flex;
            gap: 30px;
            justify-content: center;
            align-items: center;
        }
    </style>

4. Targeting Elements with Attributes

Combine the universal selector with attribute selectors to style all elements with a specific attribute .The *[title] targets all elements with a title attribute, adding a dashed blue border.


    <style>
        *[title] {
            border: 2px dashed blue;
        }
    </style>

5. Scoped Styling with Descendant Selector

The universal selector can style all children of a specific parent element .The .container * applies styles to all elements inside the .container class.


    <style>
        .container * {
            color: green;
        }
    </style>

6. Excluding Specific Elements

Use the universal selector with the :not() pseudo-class to exclude elements .The *:not(h1) targets all elements except h1, changing their text color to gray.


    <style>
        *:not(h1) {
            color: gray;
        }
    </style>

7. Responsive Styling with Universal Selector

Apply global changes using media queries and the universal selector. The universal selector applies a smaller font size when the viewport is narrower than 600px.


    <style>
        * {
            font-size: 18px;
        }
        @media (max-width: 600px) {
            * {
                font-size: 14px;
            }
        }
    </style>

CSS * (Universal)Selector – FAQ’s

What is the purpose of the universal selector in CSS?

  • Applying global styles like resets or box-sizing.
  • Debugging layouts by adding borders or outlines to all elements.
  • Ensuring consistency in styling across the entire webpage.

How does the universal selector affect performance?

Using the universal selector indiscriminately can negatively impact performance, especially on large webpages.

Can the universal selector be combined with other selectors?

Yes, the universal selector can be combined with other selectors to create more specific styles.

Does the Universal Selector Apply to Pseudo-Elements?

Yes, the universal selector (*) applies to pseudo-elements because they are part of the rendered DOM structure.

Is the universal selector a good alternative to CSS reset libraries?

While the universal selector can reset styles (e.g., setting margins and paddings to 0), it is not a full replacement for CSS reset libraries like Normalize.css. Reset libraries also address browser inconsistencies, default styles, and edge cases that the universal selector alone cannot handle effectively.



Next Article

Similar Reads

three90RightbarBannerImg