Other factors that impact the cascade are the origin of the declaration and the importance of the declaration. Origin refers to where the declaration comes from. CSS declarations could be written by the developer who built the website, from the users who visit your website, or from the browser itself.
The full list of origins as defined in the CSS specification, in descending order of precedence looks like this:
- Transition declarations
- Important user agent declarations
- Important user declarations
- Important author declarations
- Animation declarations
- Normal author declarations
- Normal user declarations
- Normal user agent declarations
where declarations from origins earlier in this list win over declarations from later origins.
For now, let’s set aside #1 and #5 as they are related to CSS animations, which we are not covering in this course. You will notice that the three sources of declarations are in the list but there are two types of each, normal and important.
User agent stylesheets
Now is a good time to explain what user agent stylesheets are. Browsers provide a set of sensible defaults for styling elements via the browser’s own stylesheet. If you examine the list of origins above, under normal circumstances, any author declared CSS will override these user agent styles. The terms browser stylesheet and user agent stylesheet can be used interchangeably.
The purpose of user agent styles is to ensure that “unstyled” HTML doesn’t end up looking like a completely plain text document. Every browser has slight differences what default values they apply, and most of these styles relate to spacing and font sizes. Some people find it confusing and prefer to reset everything, while other folks are happy with overriding where necessary.
There is no right way to do things, so it really boils down to your design requirements and to some extent, personal preference. There is no way to “turn off” these styles directly, so it is something to keep in mind when you are building and styling your web pages.
Important declarations
By default, the rules in the developer’s stylesheet will override those in the user’s stylesheet, which in turn will override those in the browser’s stylesheet. But CSS provides an escape hatch where a declaration can be marked as important, increasing its weight in the cascade and flips the order of precedence.
To mark a declaration as important, you will have to annotate it with !important, like so:
.hidden-text { display: none !important;}If you look at the precedence order again, you will notice that important declarations will always win over normal declarations. This means that even if you have a selector that is infinitely specific, a declaration marked important will always win over it. Because of how powerful !important is, it should be used sparingly and after proper consideration.
There is nothing stopping you from using !important all over the place (and there might be times that you encounter some code that is just littered with !importants). But HTML and CSS are relatively forgiving, in that, unlike some programming languages which will fail to run altogether unless you do things in a strictly specified manner, HTML and CSS will sort of shrug and make the best of things.
When your project is small, and there are only a handful of elements and a couple of style rules, this may not seem like a big deal, but as your project grows and more style rules get written, things will start to become very messy as you lose track of which declarations are important.
It’s very frustrating to be unable to figure out why your style rule is not behaving like you expected only to find that somewhere earlier in the stylesheet was a declaration marked !important that messed things up.
Welcome! Unfortunately, you don’t have access to this lesson. To get access, please purchase the course or enroll in Magical Dev School.
Unlock this lesson