Hello!
Some text.
# The Cascade, Specificity, and Inheritance by [kirupa](https://www.kirupa.com/me/index.htm) | filed under [HTML and CSS](https://www.kirupa.com/html5/learn_html_css.htm) **TL;DR:** When several CSS rules set the same property on one element, the browser asks three questions in order: is one more important, is one more specific, and which one comes last? That process is the cascade. Specificity counts IDs, then classes, then types, and text properties like color and font also pass down from parents through inheritance. - Conflicts are settled one property at a time, so a paragraph can take its color from #intro, its font size from p, and its weight from .note. - Specificity is compared one column at a time, so #intro at 1-0-0 beats .card .note:hover at 0-3-0, and no number of classes can outrank an ID. - Inline styles beat every selector, and !important beats inline styles, which is why it's best saved for the rare rule that truly must win. - Inherited values have no specificity, so even a plain p rule beats the color a paragraph would have inherited from its parent. We write a CSS rule, save the file, refresh the page, and...nothing changes. The rule is right there, the selector looks correct, and the element stubbornly keeps its old color anyway. What happened is that our rule lost a fight. Some other rule was also trying to style that same element, and the browser picked that one instead. The browser doesn't pick winners at random. It follows a short list of tie-breakers, and once we know them, we can look at any two rules and predict which one wins. That's what we're going to learn here, along with the one way an element can get styled without any rule targeting it at all. Onwards! ## What Happens When Two Rules Collide? Let's start with a single paragraph that has both an id and a class: ```html
Hello!
``` Now, let's say our stylesheet has three rules that all match it: ```css p { color: gray; font-size: 20px; } .note { color: blue; font-weight: bold; } #intro { color: red; } ``` Before reading on, take a guess at what color our paragraph ends up. All three rules match it, and all three set a `color`, but only one of them can win. Here's what the browser does with them: [](https://www.kirupa.com/html5/images/cs_per_property.png?v=0b80e1334a6a) Our paragraph ends up red, thanks to the `#intro` rule. What's noteworthy is that the other two rules didn't lose completely. The browser settles the fight one property at a time. The `color` goes to `#intro`, but no other rule sets `font-size`, so that still comes from the `p` rule. In the same way, `font-weight` still comes from `.note`. Our "Hello!" ends up red, 20 pixels, and bold, with a little bit from each rule. ## How Does the Browser Pick a Winner? When two or more declarations try to set the same property on the same element, the browser asks a few questions, in order. The first question with a clear answer picks the winner, and the rest never get asked: [](https://www.kirupa.com/html5/images/cs_cascade_steps.png?v=62f43c6cb1e8) Let's walk through each of those questions: 1. **Is one more important?** Our own styles beat the browser's built-in styles (the ones that make headings big and links blue). A declaration marked `!important` beats one that isn't, and we'll look at that a bit later. 2. **Is one more specific?** If both declarations are equally important, the one with the more specific selector wins. An ID selector is more specific than a class selector, and a class selector is more specific than a type selector. 3. **Which one comes last?** If the selectors are equally specific, the declaration that shows up later in our CSS wins. This process of sorting through competing declarations is called the **cascade**, and it's the "C" in CSS. In our example, all three rules are ours and none of them use `!important`, so the first question is a tie. The second question settles it, since `#intro` is the most specific selector of the three. The last question is the easiest one to see in action. Here are two rules with the exact same selector: ```css .note { color: blue; } .note { color: green; /* this one wins */ } ``` The second rule wins because it comes later. This also works across files. If our HTML links two stylesheets, every rule in the second one comes after every rule in the first, which is why the order of our `link` elements matters. The [Where Will Your Styles Live?](https://www.kirupa.com/html5/where_will_your_styles_live.htm) tutorial covers the different places our CSS can live. Newer CSS also has a feature called cascade layers (`@layer`), which adds one more question between importance and specificity. It lets us say that one whole group of styles should always beat another group, no matter what selectors they use, and the [Cascade Layers in CSS](https://www.kirupa.com/html5/css_cascade_layers.htm) tutorial goes deep on it. For styles that don't use layers, the three questions above are the whole story. ## How Is Specificity Calculated? "More specific" needs a more precise definition, and it has one. The browser gives every selector a score by counting three kinds of things in it: - **IDs**, like `#intro` - **Classes**, like `.note`, along with attribute selectors like `[type="email"]` and pseudo-classes like `:hover` - **Types**, like `p` or `article`, along with pseudo-elements like `::before` This score is known as **specificity**. We write it as three numbers in that order, and here's how a handful of selectors add up: [](https://www.kirupa.com/html5/images/cs_specificity.png?v=8cafd618330c) To compare two scores, we start with the IDs. If one selector has more IDs, it wins, and we don't even look at the other columns. If the IDs are tied, we compare the classes, and if those are tied too, we compare the types. It works a lot like comparing version numbers, where version 1.0.0 is newer than version 0.3.0 even though 3 is bigger than 1. That's why `#intro` at 1-0-0 beats `.card .note:hover` at 0-3-0. The columns never carry over into each other, so even a selector with 11 classes can't beat a single ID. A few things don't count toward specificity at all. The universal selector `*` and the combinators that connect selectors (a space, `>`, `+`, and `~`) add nothing. The `:is()`, `:not()`, and `:has()` pseudo-classes don't count themselves, but they take on the score of the most specific selector inside them. Then there's `:where()`, which always scores 0-0-0 no matter what's inside it. That makes it handy for writing default styles that any other rule can easily override. If you'd like a refresher on the selectors themselves, the [CSS Selectors: Type, Class, and ID](https://www.kirupa.com/html5/css_selectors_type_class_id.htm) and [CSS Attribute Selectors](https://www.kirupa.com/html5/css_attribute_selectors.htm) tutorials cover them. Here's a place to try all of this out. Two rules are trying to set the color of the same paragraph, and we can type any selector we like into either one. The demo scores both selectors and tells us who won: [Live demo: Two CSS rules, one setting a paragraph blue and one setting it red, with editable selectors, their specificity scores, and an explanation of which rule wins](https://www.kirupa.com/html5/examples/specificity_battle.htm) **Try it:** 1. Type `article p` into **Rule B** 2. Now try `#intro` 3. Then `.note`, for a tie Whenever both rules match the paragraph, the higher score wins. When the scores are tied, Rule B wins because it comes second. If you type a selector that only matches the article around the paragraph, like `article`, something different happens, and we'll see why in a little bit. ## What About Inline Styles and !important? There are two ways to jump ahead of everything we've seen so far. The first is an inline style, which is CSS written directly on an element using its `style` attribute: ```htmlHello!
``` An inline style beats any selector in our stylesheets, no matter how many IDs that selector has. This is also what's going on when JavaScript sets a style with something like `element.style.color`, which the [Changing CSS using JavaScript](https://www.kirupa.com/html5/setting_css_styles_using_javascript.htm) tutorial covers. The second way is `!important`. Adding it to the end of a declaration bumps that declaration up past all of the normal ones, inline styles included: ```css .note { color: green !important; } ``` Here's how all of these stack up against each other, from weakest to strongest: [](https://www.kirupa.com/html5/images/cs_ladder.png?v=b69bc785e040) If two declarations both use `!important`, the browser goes back to comparing their specificity and then their order. That leads to the problem with `!important`. Once one rule uses it, the only way to beat that rule is with another `!important`, and soon our stylesheet turns into an arms race where every declaration is shouting. It's fine for the rare case where something truly must win, like a utility class whose whole job is to hide an element. For everyday conflicts, a better fix is to lower the specificity of the rule that's winning or to move our rule later in the file. ## What Gets Inherited? So far, every rule we've looked at matched our paragraph directly. There's one more way an element can get a style, and that's from its parent. Let's say we style an article and nothing else: ```htmlSome text.