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
216
votes
9 answers

What are the implications of using "!important" in CSS?

I've been working on a website for a few months, and a lot of times when I've been trying to edit something, I have to use !important, for example: div.myDiv { width: 400px !important; } in order to make it display as expected. Is this bad…
Kyle
  • 65,599
  • 28
  • 144
  • 152
135
votes
7 answers

How are the points in CSS specificity calculated

Researching specificity I stumbled upon this blog - http://www.htmldog.com/guides/cssadvanced/specificity/ It states that specificity is a point-scoring system for CSS. It tells us that elements are worth 1 point, classes are worth 10 points and…
Sam
  • 4,437
  • 11
  • 40
  • 59
77
votes
7 answers

Can I override inline !important?

If you have
Is there a way to override that in the style sheet to make it displayed? Preferably using something similar to this: div { display: block !important; }
Jeffrey Basurto
  • 1,425
  • 1
  • 10
  • 24
43
votes
7 answers

Change icon-bar (☰) color in bootstrap

I want to change ☰ color. HTML:
33
votes
4 answers

CSS Specificity Filter

This is a long shot, but is there a tool available that optimizes CSS selectors by removing unneeded specificity? I find that when I write CSS, I deliberately make my selectors more specific than necessary to avoid conflicts and for…
cantera
  • 24,479
  • 25
  • 95
  • 138
33
votes
1 answer

CSS specificity, Media Queries and min-width

I'm redesigning my blog with responsive web design in mind, and the "mobile first" method - In short I'm trying to use min-width to avoid any kind of replication or css.. no display:none's etc.. My problem is that when I do need to over-write a CSS…
George Katsanos
  • 13,524
  • 16
  • 62
  • 98
30
votes
2 answers

Relationship between !important and CSS specificity

Looking at the CSS specificity specification, there is no mention about how many "points" the !important rule is worth. When does one override another? What happens if one is declared after the other? Which rule is declared to be more important? Is…
Zirak
  • 38,920
  • 13
  • 81
  • 92
28
votes
2 answers

Does the :not pseudo class increase the specificity of a selector?

I have read on css tricks that :not should not add additional specificity. But it looks like it does? https://css-tricks.com/almanac/selectors/n/not/ The specificity of the :not pseudo class is the specificity of its argument. The :not() pseudo…
Paran0a
  • 3,359
  • 3
  • 23
  • 48
25
votes
8 answers

Why does the hover pseudo-class override the active pseudo-class

The title basically says it all. Suppose I have an element which I want to change color on :hover, but while clicked, I want it to switch back to its original color. So, I've tried this: a:link, a:visited, a:active { background: red; } a:hover…
Joseph Silber
  • 214,931
  • 59
  • 362
  • 292
25
votes
3 answers

CSS class repetition to increase specificity

According to the CSS docs: http://www.w3.org/TR/CSS21/cascade.html#specificity Specificity is defined by (amongst other things) the number of attributes and pseudo-classes in the selector. So, my question is, is it possible to increase specificity…
Georges Oates Larsen
  • 6,812
  • 12
  • 51
  • 67
25
votes
3 answers

What is the specificity of the attribute selector?

I'm wondering what the specificity of the attribute selector is. For example: Id = 100 points Class = 10 points Html Tag= 1 point Example: /* this specificity value is 100 + 10 + 1 = 111 */ #hello .class h2 { } With this HTML:
Rohit Azad Malik
  • 31,410
  • 17
  • 69
  • 97
25
votes
4 answers

CSS class priorities

I have a question about the priority of CSS classes after encountering a problem today. The situation is as follows: I have an unordered list which has a class associated with it. The LI tags have some styles defined too. I want to change the…
Stuart
  • 3,258
  • 4
  • 29
  • 39
23
votes
4 answers

Using multiple classes in one element and specificity

Just wondering when you use multiple classes on the one element such as class="foo bar" and those classes are setup as below: .foo { margin-right: 10px; } .bar { margin-right: 0px; } Which class will have specificity? Will the margin be…
Brett
  • 19,449
  • 54
  • 157
  • 290
23
votes
2 answers

Precedence of multiple classes defining color property being set by declaration order rather than specification order

Given two classes of equal specificity defining the color property I thought the last class listed in the element class attribute would take precedence. From http://htmlhelp.com/reference/css/structure.html: Order of Specification To make it easy,…
IdusOrtus
  • 1,005
  • 1
  • 16
  • 24
22
votes
4 answers

What is the most character-efficient way to increase CSS specificity?

If I want to increase the CSS specificity of a rule, I tend to prefix with html, but I wonder if there are more concise ways of doing this? (It may seem like a trivial issue, but over the course of my stylesheets defining a responsive grid,…
wheresrhys
  • 22,558
  • 19
  • 94
  • 162
1
2 3
24 25