Concept of cascading in CSS

Most of us know that Cascaded Style Sheet abbreviates to CSS . But why is it called so? What is the cascading happening out in the stylesheet. Well the concept is pretty simple, but powerful.

You might come across scenarios wherein, you want to override a css class of a third party add-in, but still not able to apply your styles. Well the reason is nothing but the concept of cascading coming into play. Let me get into a bit theoretical  here.

Stylesheets are divided into three types:

  1. Author StyleSheet – Stylesheet created by the author of the webpage
  2. User StyleSheet – StyleSheet are set by the user of the Web page. These allow the user to have more control over how the pages display.
  3. User Agent StyleSheet – StyleSheet defined by your browser.

The order of precedence is Author with the highest priority, User and then User Agent StyleSheet. Again, the exception would be the !important rule, wherein the StyleSheet  with selector carrying properties with !important will be given precedence over any other.

You might wonder, then a scenario can come with conflicts in using this property. Yes, such scenarios arise. In that case, the order of precedence is given to the Author Stylesheet, followed by User and then the User Agent one.

Cascade Conflict Resolution:

To resolve conflicts, Web browsers use the following sorting order to determine which style has precedence and will be used:

  • First, look for all declarations that apply to the element in question, and for the assigned mediatype.
  • Then look at what style sheet it’s coming from. As above, author style sheets come first, then user, then user agent. With !important user styles having higher precedence than author !important styles.
  • The more specific a selector is, the more precedence it will get.

For example, a style on “ p” will have a higher precedence than one just on the “p” tag.

  • Finally, sort the rules by the order they were defined. Rules that are defined later in the document tree have higher precedence than those defined earlier. And rules from an imported style sheet are considered before rules directly in the style sheet.

Hope this article was useful.