DEV Community

Md Shahab Uddin
Md Shahab Uddin

Posted on

5 Quick facts on Universal Selector

4 quick learning at CSS:

  1. Specificity of a universal selector is zero. You can override it with a class or an id.
  2. Browser reads CSS selectors from right to left. So try to put class or id at the end of the selector
  3. You should only use a universal selector only in two cases. The first is while reset and the second is when you want to select all child elements inside a selector. Using universal selector is a controversial topic, so you shouldn't use this other than these two cases.
  4. If you ever saw that your style isn't affecting the webpage, there might be a problem with CSS specificity. Determine the specify with a specificity calculator.

Top comments (4)

Collapse
 
afif profile image
Temani Afif

can you give more detail about the (2) ? I don't understand why you think we should put ID or class at the end. It depends on the nature of the selector unless you are talking about some particular cases

Collapse
 
shahab570 profile image
Md Shahab Uddin

Please read this article. I hope you will get your answer dev.to/clairecodes/my-misconceptio...

Collapse
 
afif profile image
Temani Afif

I am not looking for an answer. I wanted you to elaborate your point (2) because it's not clear what you meant

Thread Thread
 
shahab570 profile image
Md Shahab Uddin • Edited

It's not compulsory to put id/class at the end of the selector. It is better to style elements in this way as much as possible because browser checks the rightmost selector first. So being specific is crucial.

The browser checks the rightmost selector first before moving left.
Therefore, when writing selectors, it's best to put something performant like a class or ID at the end.