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
6
votes
3 answers

CSS specificity of :not() pseudo class

I have this small HTML:
  • Item 1
For ul elements outside of the .ticker class, but inside of the #column id exists this CSS: #column…
Juuro
  • 1,487
  • 5
  • 16
  • 27
6
votes
2 answers

Precedence in CSS selector specifity conflicts (type vs class selector)

I learned about selector precedence from this tutorial. I have trouble understanding the behavior of this in one case. I have an element in HTML: The…
Borut Flis
  • 15,715
  • 30
  • 92
  • 119
6
votes
1 answer

Efficient Algorithm To Compare Specificity Of CSS Rules

I was wondering what an efficient algorithm would be in the following scenario: Given a parsed set of css rules, eg. p.pStyle{margin-bottom:20px;font-family:Arial;} p{font-family:Verdana;} p.anotherPStyle{margin-bottom:10px;} from a…
Darius
  • 5,180
  • 5
  • 47
  • 62
6
votes
2 answers

Order CSS based on Selector Specificity

I have parsed a given CSS file/string into a JSON object like so: { "#header": { "color": "#000000" }, "#header h1": { "color": "#000" }, "h1": { "color": "#4fb6e5" } } What I want to do now is…
Abs
  • 56,052
  • 101
  • 275
  • 409
5
votes
2 answers

Is there a way to force a lower specificity in CSS?

I have a CSS template where I want to use a minimal amount of attributes within some HTML markup, while allowing for easy customization of that markup via classes (not IDs) later, if needed.
  • A
Kithraya
  • 358
  • 2
  • 10
5
votes
2 answers

CSS Universal selector (*) specificity

I'm trying to figure out why .x has higher specificity than *.x when the latter is expected to win. Isn't *.x supposed to have a specificty of 0-0-1-1 (1 class, 1 tag) while .x is just a single class 0-0-1-0? Consider the following basic code: *.x…
Aziz
  • 7,685
  • 3
  • 31
  • 54
5
votes
1 answer

"*" is appearing top in CSS specificity, why?

I'm having trouble understanding why my CSS isn't being styled according to the way I understand the specificity rules. According to my reading across the web (including this calculator), the * (matches everything) has no specificity, while an…
J-bob
  • 8,380
  • 11
  • 52
  • 85
4
votes
3 answers

CSS specificity when Javascript modifies CSS?

What is the CSS specificity when Javascript modifies CSS? Such as: document.getElementById("demo").style.color = "red"; Is it considered inline styling?
Kingamere
  • 9,496
  • 23
  • 71
  • 110
4
votes
2 answers

Specificity and Direct Targeting

I'm running loops between my understanding of specificity and targeting an element. I understand the base concept of specificity and read numerous articles on how to calculate specificity using a,b,c,d, where d is the lowest specificity and a is the…
HelloWorld
  • 10,529
  • 10
  • 31
  • 50
4
votes
1 answer

Sorting a set of CSS selectors on the basis of specificity

How can a set of CSS selectors be sorted on the basis of CSS specificity in a JS function? function SortByCssSpecificity(input_array_of_css_selectors) { ... return sorted_array_of_css_selectors; }
GeekTantra
  • 11,580
  • 6
  • 41
  • 55
4
votes
4 answers

Specificity rules for comma delineated lists

When using Cascading Style Sheets I have observed the order of specificity as follows: 1st Laws: In-line Styles 2nd Laws: Number of ID Selectors 3rd Laws: Number of Class Selectors 4th Laws: Number of Element Selectors So, items with in-line…
user6441853
4
votes
4 answers

Understanding specificity: achieving desired selector outcomes without using !important

I am trying to understand specificity in CSS. My current understanding is that specificity is very similar to inheritance, but in some way more specifically defined. Mozilla Specificity Definition: Specificity is the means by which browsers decide…
Peter David Carter
  • 2,548
  • 8
  • 25
  • 44
4
votes
1 answer

How does a strong selector override an id selector? Isn't an id selector considered more specific?

in the following snippet, how come the strong selector overrides the id selector? Isn't an id selector considered more specific, thus having priority? Sample document