Questions tagged [css-specificity]

In CSS, specificity is a measure that determines the strength of a selector. The properties within selectors with the highest specificity get applied, overriding the same properties of selectors with lower specificity, regardless of their position within a stylesheet.

In CSS, specificity is a measure applied to a style rule that provides a ranking, based on its selector, for resolving conflicts between its style declarations and the declarations of other style rules.

Calculating Specificity

As defined Section 6.4.3 of the CSS2.1 specification and improved upon in Selectors Level 3:

A selector's specificity is calculated as follows:

  • count the number of ID selectors in the selector (= a)
  • count the number of class selectors, attributes selectors, and pseudo-classes in the selector (= b)
  • count the number of type selectors and pseudo-elements in the selector (= c) ignore the universal selector
  • Selectors inside the negation pseudo-class are counted like any other, but the negation itself does not count as a pseudo-class.

Concatenating the three numbers a-b-c (in a number system with a large base) gives the specificity.

Examples:

*/* a=0 b=0 c=0 -> specificity =   0 */
LI              /* a=0 b=0 c=1 -> specificity =   1 */
UL LI           /* a=0 b=0 c=2 -> specificity =   2 */
UL OL+LI        /* a=0 b=0 c=3 -> specificity =   3 */
H1 + *[REL=up]  /* a=0 b=1 c=1 -> specificity =  11 */
UL OL LI.red    /* a=0 b=1 c=3 -> specificity =  13 */
LI.red.level    /* a=0 b=2 c=1 -> specificity =  21 */
#x34y           /* a=1 b=0 c=0 -> specificity = 100 */
#s12:not(FOO)   /* a=1 b=0 c=1 -> specificity = 101 */

Note: Repeated occurrences of the same simple selector are allowed and do increase specificity.

You may calculate and test the specificity of your selectors here

361 questions
-1
votes
2 answers

CSS !important and inline style being overwritten

I am using Bulma to create a website and I have been having a weird issue with the Tabs component when being used in a Hero component. Tabs has a margin-bottom of 1.5rem by default. However, this is not something that I desire and I want to remove…
Prejith P
  • 195
  • 3
  • 12
-1
votes
2 answers

What is the use of width:100%!important and height:100%!important in CSS?

.full-block { display: block; } h1 { margin-left: 10px; font-family: "Open Sans", Arial, sans-serif; } .block-1 { width: 100%; height: 100%; background-color: #000; display: inline-block; color: #fff; } .block-2…
Sifatur Rahman
  • 803
  • 2
  • 9
  • 15
-1
votes
3 answers

How can i style differently a link in a div adding a class to it?

I have multiple divs inside a div with a class of .grid. Inside those divs i have a link in each. I styled those links by using the selector .grid div a. After that i added another link in each div, and gave it a class .name and selected it using…
Razvan Pop
  • 198
  • 9
-1
votes
2 answers

Overriding CSS property

I think I am overlooking something. I have this code where I am trying to change the color of list element text to signal that it is inactive. Shouldn't the second style take precedent over the first? Inspector says the inactive style is getting…
Reid Barber
  • 101
  • 8
-1
votes
1 answer

Doubts about the specificity in CSS3

I don't understand even if I have studied the specificity why my CSS rule below is not applied. CSS body { font-family: 'BNPSans', Arial, Helvetica, sans-serif; font-size: 12px; color: black; } /** Navbars */ .navbar#brandbar { …
Silvio S.
  • 547
  • 5
  • 20
-1
votes
3 answers

Styles applied to HTML element are always overridden

I apply styles to the element and to the element. Is it possible to have the styles on the element apply over the doesn't seem to follow the usual rules of CSS specificity. Is this…
Don P
  • 60,113
  • 114
  • 300
  • 432
-2
votes
2 answers

my * is taking a higher specificity over my element tag and !important tag

[*" at 0.0 transparency](https://i.stack.imgur.com/V5fil.png) [*" at 0.5 transparency](https://i.stack.imgur.com/gfSNP.png) [*" at 1.0 transparency](https://i.stack.imgur.com/yxOrn.png) The problem is that the "*" tag is taking a higher specificity…
-2
votes
1 answer

I have a CSS specificity

This is my html code This is my css code When in CSS I write the CSS code for .keys class like this and add class .transistionOfKeys to html then my border properties does not add to it But when I change my CSS code to this by removing .container…
-2
votes
1 answer

How can I use specificity to fix a CSS problem using Extenal sheets with multiple classes within a p tag?

Below is an index.html page with five

elements inside a

element and a element

Normal and green!

Small and orange!

codMW
  • 1
  • 1
-2
votes
1 answer

Whats wrong with my CSS Selector?

I have a div inside a div. The .wrapper is parent of .level.
Engkus Kusnadi
When i gave style to the .level with .wrapper include as selector, its working fine and the background…
Engkus Kusnadi
  • 2,386
  • 2
  • 18
  • 40
-2
votes
2 answers

CSS rules order

Here I have 8 CSS rules and I'd like to know their order of importance. I'm reading since a while on that but I can't figure out this precise example. a > b + a, a > b {color: red;} a > a, a + b {color: brown;} c > b, c > a > b {color: yellow;} a >…
Simon L.
  • 55
  • 4
-2
votes
2 answers

Why ID selector takes advantage on class selector?

Given one element with a class and ID ; if I apply some CSS with a class selector to an element, and override it with some CSS with an ID selector, the later is applied.