DEV Community

Cover image for How CSS Specificity Works🤹🏼‍♀️
Sai gowtham
Sai gowtham

Posted on • Edited on

12 4

How CSS Specificity Works🤹🏼‍♀️

We already know CSS stands for the cascading style sheets.

What the Cascading really mean is the browser first resolves the which stylesheet need to be applied to the HTML.Like we see blue color to the
links which come by the browser default.

Let's talk about the Specificity

Specificity is the weights applied to the CSS when we declared.

Weights really mean how we are applying a style to the particular HTML
element.

Like we are using id attributes to target the Html element or we used
class attributes sometimes we used direct element name like h1, p to apply
the styles.

Here the specificity comes in we are applying the styles using different selectors.
But browser applies styles to an element based on their weights.

In weights

  • First is inline CSS
  • Second id
  • Third class
  • Fourth Elements/Pseudo classes/Pseudo selectors

If We apply styles to the element with any selectors we have.If we just place an inline CSS it overrides the all styles which are before applied by using id, class, element.

(Important is never recommended)

If we add !important to the any of our selectors. Important overrides all of the other styles before we added to that element.

Check out in code pen I showed it clearly fork this pen and see the changes by removing important, inline CSS, id etc...

Find me on Twitter

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (5)

Collapse
 
andrewlucker profile image
Andrew Lucker

Had to research this one. Specificity is a multi-digit ordinal with the four ranks you listed. For example: 0,1,0,4

smashingmagazine.com/2007/07/css-s...

Previously I thought it was a weighted floating point number, but that is wrong. Thanks for the clarification.

Collapse
 
sait profile image
Sai gowtham • Edited

Thanks for your comment 🤗🤗

Collapse
 
dev_nope profile image
Vasile Stefirta 🇲🇩 ✈️ 🇺🇸

This online Specificity Calculator is really handy specificity.keegan.st/ 🔥

Collapse
 
sait profile image
Sai gowtham

Ss good tool

Collapse
 
shmidtelson profile image
shmidtelson

Guys, dont use !important tag.
Use if tomorrow armageddon

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Instrument, monitor, fix: a hands-on debugging session

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️