DEV Community

Anil Kumar Khandei
Anil Kumar Khandei

Posted on

CSS Precedence

Have you ever wondered how many places we can define css?

We usually define

  • in a separate css file,
  • occassionally inline as well

but which one takes the precedence when we have styles defined in multiple places which target the same behaviour.

Ideally its best to define all css in one place but in real world it may not happen that way so in this short but productive post I highlight the precedence of css(starting with the highest order of precedence)-

  • !Important styles

li{
list-style-type: none !important;
}

  • Inline styles

<a style="color: blue;" aria-current="page" href="#">Home</a>

  • Id attribute styles

#myHeader {
background-color: lightblue;
color: black;
padding: 40px;
text-align: center;
}

  • class styles

<a class="nav-link text-black" href="#">Home</a>

when 2 css classes are used and if both happen to define the same behaviour then the last css style takes precedence

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 (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay