What is the meaning of cascading in CSS?
"Cascading" in this context means that because more than one stylesheet declaration could apply to a particular piece of HTML, there has to be a known way of determining which specific stylesheet rule applies to which piece of HTML.
The rule used is chosen by cascading down from the more general declarations to the specific rule required. The most specific declaration is chosen.
Read the official W3C specification on cascading here: https://www.w3.org/TR/css-cascade-4/
What is the meaning of “cascading” in Cascading Style Sheets?
The cascade describes in what order properties are applied on elements.
CSS cascading with examples
When only considering "author" stylesheets (not user or user agent), then what you are probably confused about is not so much the "cascade", but the "specificity" rules for CSS.
Here are some good explanations of how specificity works and how to figure out why certain rules are applied to your elements:
http://coding.smashingmagazine.com/2007/07/27/css-specificity-things-you-should-know/
http://css-tricks.com/specifics-on-css-specificity/
EDIT: The cascade:
All applicable rules for an element are gathered, then ordered according to their origin (higher number means higher precedence):
- user agent declarations
- user normal declarations
- author normal declarations
- author important declarations
- user important declarations
Specificity is used as a tie-breaker if the origin is the same. Finally, if specificity is the same, then declaration order is the final tie-breaker. Hopefully this is understandable without a tedious example...
In CSS, what is the difference between cascading and inheritance?
Inheritance is about how properties trickle down from an element to its children. Certain properties, like font-family
inherit. If you set a font-family on the body
, that font family will be inherited by all the elements within the body
. The same is true for color
, but it is not true for background
or height
which will always default to transparent
and auto
. In most cases this just makes sense. Why would the background inherit? That would be a pain. What if fonts didn't inherit? What would that even look like?
The cascade is about what take precedence when there is a conflict. The rules of the cascade include:
- Later properties override earlier properties
- More specific selectors override less specific selectors
- Specified properties override inherited properties
And so on. The cascade solves any conflict situations. It is the order in which properties are applied.
(update) Specificity is the calculation used to determine selector priority in the cascade. When two selectors apply to the same element, the one with higher specificity takes precedence.
- Inline styles have a very high specificity (
1000
) - ID's have a specificity of
100
- classes/attributes and pseudo-classes add
10
- elements and pseudo-elements add
1
Add up all the parts in a selector chain to determine the total specificity. In case of a tie, the last selector takes precedence.
Of course, that comes with various edge-cases and caveats. One class will always override plain elements, no matter how many. More targeted selectors are given priority over inherited properties from parent selectors. And you can throw out all your calculations if someone used !important
— that trumps everything.
CSS cascading order - author vs user
Author
The developer of the original CSS code for that application. Let's say the Front-end developer of StackOverflow website is the Author here.
Any changes made on Author code will effect the application style for
everyone.
User
You want to have custom style for the pages that you view. Eg: If you need to change the background color of Stackoverflow website, you can use Stylish extension to append your custom style rules. Now you are the user here.
You can filter the user styles in the Style Side Panel which shows the system applied styles as well as user defined custom CSS.
Any changes made on this code will affect only your browser.
Are CSS @media query statements cascading properties specified in em unit?
In your example, font size will end up being 0.5em
, because the second font-size
definition overrides the first one according to the usual CSS rules.
And if you would change the order of the two media queries, then the resulting font size would be 2em
.
Here's a snippet which demonstrates this.
Note that the text will be bold (1st media query), italic (2nd media query) and red (the definition from the 2nd media query "wins" according to the usual CSS rules).
Font-size will be set to 0.5em
, because the 2nd definition overwrites the first one (NOT 1em
, as it would be if the 2nd definition would be applied cumulatively "on top" of the 1st one).
@media screen and (max-width:10000px) { p.styled { color: blue; font-weight: bold; font-size: 2em; }}
@media screen and (min-width:1px) { p.styled { color: red; font-style: italic; font-size: 0.5em; }}
<p class="styled">Text with styles from both media queries applied.</p><p>Text with no styles applied.</p>
Related Topics
Bootstrap 4 Carousel Responsive (Image and Text)
Align an Image and Some Text on the Same Line Without Using Div Width
How to Override Default Primefaces CSS With Custom Styles
Prevent Content from Expanding Grid Items
Click Through Div to Underlying Elements
How to Force a Div Block to Extend to the Bottom of a Page Even If It Has No Content
Is There a CSS Parent Selector
Select Elements by Attribute in Css
Footer At Bottom of Page or Content, Whichever Is Lower
Css Floating Divs At Variable Heights
Align ≪Div≫ Elements Side by Side
Remove Padding from Columns in Bootstrap 3
How Does Flex-Wrap Work With Align-Self, Align-Items and Align-Content
Center a Column Using Twitter Bootstrap 3
How to Change Whole Page Background-Color in Angular
How to Use HTML to Print Header and Footer on Every Printed Page of a Document