“Demystifying CSS Specificity: A Beginner’s Guide”

sidverma
4 min readDec 11, 2022

CSS specificity refers to the rules that determine which style declarations will be applied to an element in a web page. These rules are used by the browser to determine which styles take precedence over others, allowing developers to control the appearance of their pages in a consistent and predictable manner.

At its core, CSS specificity is based on the idea that some styles are more important than others. For example, a style that is declared directly on an element will be considered more important than a style that is declared on a parent element. This means that if there are conflicting styles, the browser will apply the more specific style to the element.

To understand how specificity works, it’s important to understand the different types of style declarations that can be used in CSS. There are three main types: inline styles, internal styles, and external styles.

Inline styles are applied directly to an element using the style attribute. These styles have the highest specificity because they are applied directly to the element, so they will always take precedence over other styles.

Internal styles are defined in the <style> element within the <head> of a web page. These styles are applied to the elements on the page based on the selector that is used. For example, if a <p> element is selected using the p selector, the styles defined in the internal stylesheet will be applied to that element.

External styles are defined in a separate CSS file and are linked to the web page using the <link> element. These styles are applied to the elements on the page in the same way as internal styles, but they are considered to have lower specificity because they are defined in a separate file.

When multiple styles are applied to an element, the browser uses the rules of specificity to determine which styles will be applied. The more specific a style is, the more likely it is to be applied to the element. In general, the order of specificity is as follows:

  1. Inline styles
  2. Internal styles
  3. External styles

For example, if an element has an inline style, an internal style, and an external style applied to it, the inline style will take precedence because it is the most specific. If there are multiple inline styles or multiple internal styles applied to an element, the browser will use additional rules to determine which style takes precedence.

Here are the different components of a CSS selector and the specificity value assigned to each:

  • Type selectors (e.g. h1, p, div) and pseudo-elements (e.g. ::before, ::after) have a specificity value of 0, 0, 1, 0.
  • Class selectors (e.g. .my-class), attribute selectors (e.g. [type="text"]), and pseudo-classes (e.g. :hover, :focus) have a specificity value of 0, 0, 10, 0.
  • ID selectors (e.g. #my-id) have a specificity value of 0, 0, 100, 0.
  • Inline styles (i.e. styles applied directly to an element with the style attribute) have a specificity value of 0, 0, 1000, 0.

CSS specificity is determined by the weight of each selector in a style rule. A selector with a higher weight will take precedence over a selector with a lower weight. This means that if two style declarations try to apply different styles to the same element, the one with the higher specificity will be the one that is ultimately applied.

The weight of a selector is determined by several factors, including the number of elements it matches and the type of selector used. For example, an ID selector (#my-id) has a higher weight than a class selector (.my-class), which in turn has a higher weight than a type selector (p, h1, etc.).

In addition to the type of selector, the weight of a selector can also be increased by adding additional selectors to a style rule. For example, a style rule that uses both an ID selector and a class selector will have a higher weight than a style rule that uses only one of those selectors.

When working with CSS, it’s important to understand how specificity works and how to use it to your advantage. By using specific selectors and combining them in the right way, you can ensure that the styles you apply to your web page are applied consistently and correctly.

It’s important to understand the rules of specificity in CSS because they can help you avoid conflicts and make your styles more predictable. By understanding how specificity works, you can create styles that are easy to maintain and debug, ensuring that your web pages always look their best.

--

--