# Specificity

Apart from floats, the **CSS Specificity** is one of the most difficult concepts to grasp in Cascading Stylesheets. The different weight of selectors is usually the reason why  CSS-rules don’t apply to some elements in your code, although you think they should have. In order to minimize the time for bug hunting you need to understand, how browsers interpret your code. And to understand that, you need to have a **firm understanding on how specificity works**.

* Specificity **determines, which CSS rule is applied** by the browsers.
* Specificity is usually the reason why your CSS-rules don’t apply to some elements, although you think they should.
* Every selector has its place in the specificity hierarchy.
* If two selectors apply to the same element, the one with **higher specificity wins**.
* There are four distinct categories which define the specificity level of a given selector: inline styles, IDs, classes, attributes, and elements.
* You can understand specificity if you love poker: CSS Specificity for Poker Players
* When selectors have an equal specificity value, the latest rule is the one that counts.
* When selectors have an unequal specificity value, the more specific rule is the one that counts.
* Rules with more specific selectors have a greater specificity.
* The last rule defined overrides any previous, conflicting rules.
* The embedded style sheet has a greater specificity than other rules.
* ID selectors have a higher specificity than attribute selectors.
* You should always try to **use IDs to increase the specificity**.
* A class selector beats any number of element selectors.
* The universal selector and inherited selectors have a specificity of 0, 0, 0, 0.
* You can calculate CSS specificity with CSS Specificity Calculator.

#### Specificity Hierarchy <a href="#specificity-hierarchy" id="specificity-hierarchy"></a>

Every selector has its place in the specificity hierarchy. There are four distinct categories which define the specificity level of a given selector:

1. Inline styles (Presence of style in document). An inline style lives within your XHTML document. It is attached directly to the element to be styled. E.g. `<h1 style=“color: #fff;”>`
2. IDs (# of ID selectors) ID is an identifier for your page elements, such as `#div`.
3. Classes, attributes and pseudo-classes (# of class selectors). This group includes `.classes`, `[attributes]` and pseudo-classes such as `:hover`, `:focus` etc.
4. Elements and pseudo-elements (# of Element (type) selectors). Including for instance `:before` and `:after`.

If you don’t know what exactly each of these terms stands for, you can take a look at the brief overview of them; in the last section of this article.

#### :thinking: How  Specificity can be measured? <a href="#how-to-measure-specificity" id="how-to-measure-specificity"></a>

**Memorize how to measure specificity.** “Start at 0, add 1000 for style attribute, add 100 for each ID, add 10 for each attribute, class or pseudo-class, add 1 for each element name or pseudo-element. So in

```css
body #content .data img:hover
```

the specificity value would be 122 (**0,1,2,2** or 0122): 100 for #content, 10 for .data, 10 for :hover, 1 for body and 1 for img.” \[CSS Specificity]

**Alternative way:** “Count the number of ID attributes in the selector (= a). Count the number of other attributes and pseudo-classes in the selector (= b). Count the number of element names and pseudo-elements in the selector (= c). Concatenating the three numbers **a-b-c gives the specificity**.&#x20;

#### Specificity Principles <a href="#specificity-basic-principles" id="specificity-basic-principles"></a>

**Equal specificity: the latest rule is the one that counts.** “If you have written the same rule into your external style sheet twice, then the lower rule in your style sheet is closer to the element to be styled, it is deemed to be more specific and therefore will be applied. When selectors have an equal specificity value, such as

```css
#content h1 {
padding: 5px;
}

#content h1 {
padding: 10px;
}
```

where both rules have the specificity **0, 1, 0, 1**, the latter rule is always applied.

#### Specificity Rules <a href="#specificity-rules" id="specificity-rules"></a>

**ID selectors have a higher specificity than attribute selectors.** For example, in HTML, the selector `#p123` is more specific than `[id=p123]` in terms of the cascade. Example: in

```css
A:
a#a-02 { background-image : url(n.gif); }

and

B:
a[id="a-02"] { background-image : url(n.png); }
```

the first rule (A) is more specific than the second one (B). \[[W3C CSS 2.1 Specification](http://www.w3.org/TR/CSS21/selector.html)]

**Contextual selectors are more specific than a single element selector.** It also holds for other selectors involving more than one HTML element selector.

**The embedded style sheet is closer to the element to be styled.** So in the following situation

CSS:

```css
#content h1 {
padding: 5px;
}
```

(X)HTML:

```css
<style type="text/css">
#content h1 {
padding: 10px;
}
</style>
```

the latter rule will be applied.

**The last rule defined overrides any previous, conflicting rules.** For example, given these two rules

```css
p { color: red; background: yellow }
p { color: green }
```

…paragraphs would appear in green text. They would also have a yellow background, however, because the first rule is not completely negated.&#x20;

**A class selector beats any number of element selectors.** `.introduction` beats `html body div div h2 p`.&#x20;

**The universal selector has a specificity of 0, 0, 0, 0.** `*`, `body *` and similar selectors have a zero specificity. *Inherited values also have a specificity of 0, 0, 0, 0.*&#x20;

#### Specificity Example <a href="#specificity-example" id="specificity-example"></a>

Consider three code fragments:

```css
A: h1
B: #content h1
C: <div id="content">
<h1 style="color: #fff">Headline</h1>
</div>
```

The specificity of A is **0,0,0,1** (*one* element), the specificity of B is **0,1,0,1** (one ID reference point and one element), the specificity value of C is **1,0,0,0**, since it is an inline styling.

Since

0001 = 1 < 0101 = 101 < 1000,

…the third rule has a greater level of specificity, and therefore will be applied. If the third rule didn’t exist, the second rule would have been applied.

#### Specificity In Practice <a href="#specificity-in-practice" id="specificity-in-practice"></a>

**Use LVHA for link styling.** “To ensure that you see your various link styles, you’re best off putting your styles in the order “link-visited-hover-active”, or “LVHA” for short.” \[[Link Specificity](http://meyerweb.com/eric/css/link-specificity.html)]

**Never use `!important`.** “If you’re having specificity issues, there’s some quick ways to solve it. First, avoid `!important`.” “The `!important` declaration overrides normal declarations, but is unstructured and rarely required in an author’s style sheet.”&#x20;

**Use id to make a rule more specific.** Replacing `a.highlight` with `ul#blogroll a.highlight` changes the specificity from **0, 0, 1, 1** to **0, 1, 1, 2**.

**Minimize the number of selectors.** “Use the least number of selectors required to style an element.”&#x20;

#### CSS Specificity Tools  <a href="#css-specificity-tools-resources" id="css-specificity-tools-resources"></a>

[CSS Specificity for Poker Players](http://carl.camera/?id=95) If you’re not from the programming world and CSS seems a bit confusing, perhaps this analogy may help clear some concepts up. Think of CSS rules as poker hands. The best hand determines an element’s style.

[CSS specificity calculator](https://specificity.keegan.st/) Calculates the specificity of a given selector.

A **selector** is the element that is linked to a particular style. E.g. `p` in

```css
p { padding: 10px; }
```

A **class selector** is a selector that uses a defined class (multiple per page). E.g. `p.section` in

```css
p.section { padding: 10px; }
```

An **ID selector** is a selector that uses an individually assigned identifier (one per page). E.g. `p#section` in

```css
CSS: #section { padding: 10px; }
(X)HTML: <p id="section">Text</>
```

A **contextual selector** is a selector that defines a precise cascading order for the rule. E.g. `p span` in

```css
p span { font-style: italic; }
```

defines that all span-elements within a p-element should be styled in *italics*.

An **attribute selector** matches elements which have a specific attribute or its value. E.g. `p title` in

```css
p[title] { font-weight: bold; }
```

matches all p-elements which have a `title` attribute.

**Pseudo-classes** are special classes that are used to define the behavior of HTML elements. They are used to add special effects to some selectors, which are applied automatically in certain states. E.g. `:visited` in

```css
a:visited { text-decoration: underline; }
```

**Pseudo-elements** provide designers a way to assign style to content that does not exist in the source document. Pseudo-element is a specific, unique part of an element that can be used to generate content “on the fly”, automatic numbering and lists. E.g. `:first-line` or `:after` in

```css
p:first-line { font-variant: small-caps; }
a:link:after { content: " (" attr(href) ")"; }
```
