Golang Tutorials - Learn Go Programming with Easy Step-by-Step Guides

Explore comprehensive Golang tutorials for beginners and advanced programmers. Learn Go programming with easy-to-follow, step-by-step guides, examples, and practical tips to master Go language quickly.

Specificity in CSS

Specificity in CSS

CSS Specificity — What It Is & How It Works

Specificity is a rule that browsers use to decide which CSS rule applies when multiple rules target the same element.


Why is Specificity Important?

When multiple CSS selectors match the same element, specificity determines which rule wins.


How Specificity is Calculated

Specificity is calculated based on the types of selectors used. It’s usually represented as a 4-part value:

Selector TypeWeight (Points)
Inline styles1000
ID selectors100
Class, attribute, and pseudo-class selectors10
Element and pseudo-element selectors1

Specificity Hierarchy Examples

SelectorSpecificity ScoreExplanation
div0,0,0,1Element selector
.class0,0,1,0Class selector
#id0,1,0,0ID selector
style="..." (inline style)1,0,0,0Inline style
div.class#id0,1,1,1Combination of all above

Examples

/* Element selector specificity = 1 */p {  color: blue;}/* Class selector specificity = 10 */.text {  color: red;}/* ID selector specificity = 100 */#main {  color: green;}

If an element matches all three, the text will be green because the ID selector is most specific.


Important Note About !important

  • !important overrides normal specificity.

  • Use sparingly, as it can make debugging harder.

p {  color: blue !important;}

Summary

Selector TypeSpecificity Value
Inline styles1000
ID selectors100
Class/attribute/pseudo-class10
Element/pseudo-element1

If you want, I can help you calculate specificity for your CSS code or show examples of specificity conflicts and how to fix them!

Disclaimer for AI-Generated Content:
The content provided in these tutorials is generated using artificial intelligence and is intended for educational purposes only.
html
docker
php
kubernetes
golang
mysql
postgresql
mariaDB
sql