Explain Codes LogoExplain Codes Logo

What does the * * CSS selector do?

html
responsive-design
css-selectors
best-practices
Anton ShumikhinbyAnton Shumikhin·Jan 18, 2025
TLDR

In CSS, the * selector is a global targeting tool that applies styles to all elements in your HTML.

Example:

/* "I'm the king of the castle," said every box.*/ * { box-sizing: border-box; }

This setting ensures all elements use border-box model, keeping padding and borders within the element's specified width and height. It's a quick way to prevent unexpected layout shifts but use it wisely to avoid cascading troubles down your stylesheets.

Comprehensive * * selector

In CSS's vast syntax, the * * selector stands out for its unique utility. It's a descendant combinator that targets all descendants of all elements. Given its broad scope, it's a legitimate and valuable tool in specific scenarios. It stylizes elements nested in other elements regardless of nesting depth.

Example:

/* "Like a boss. I apply to all, my rule is the law." - says universal selector */ * * { color: red; }

This setting colors all nested elements across the board red, leaving top-level elements untouched. It's an effective way to trace cascading styles in your HTML structure or to set a universal style rule for nested elements.

Diving deeper - nests within nests

Just as * * targets multi-leveled nesting, adding more asterisks increases the specific depth targeted.

Examples:

  • If you want elements at least two levels deep, use * *.
  • For elements three levels deep or more, use * * *, and so on.

Basically, you can target your CSS styles as per the depth of nesting of your elements, highly useful for visualizing and understanding the hierarchical relationships in your HTML structure.

Element Aesthetics with * *

The * * selector provides a powerful toolset for singular uniformity across all descendant elements without any top-level influence. Here's how:

  • Apply a universal font style for all nested elements.
  • Visually uniform list items across all nested lists.
  • Set universally consistent margins and padding for all nested elements.

Balancing power: Overuse & Overrides

Using the * * selector without caution may lead to CSS conflicts and overdraws. Here are some important considerations for balancing power:

  • Maintenance: Overuse might complicate maintaining your codes.
  • Overrides: Cascading might unintentionally override other styles. Be on the lookout for specificity conflicts.
  • Performance: Use of descendant combinators could slow down rendering.

By mitigating these factors through practical usage, the * * selector remains a helpful tool for your CSS projects.