What's the exact meaning of the term "Cascading" in CSS? I am getting different views, so I ask here. An example would help.
What's the exact meaning of the term "Cascading" in CSS? I am getting different views, so I ask here. An example would help.
"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/
When I teach CSS, I always tell the students that "cascading style sheets" means something like "fighting style sheets".
One rule tells your H3 tag to be red, another rule tells it to be green -- the rules are contradicting each other, who will win!? Stylesheet deathmatch!
OK maybe that's a slight exaggeration, but it's far more amenable to non-code, non-programming people who are just starting out than any notion of a cascade, or inheritance.
I do of course make sure to tell them that it's not a problem for the style sheets to be fighting each other, that's the way the language was designed.
Every rule from style sheets participate in a 'war' that is similar to cascade. Cascade is a pretty rarely used word and that's why it's problematic to understand the 'C' in CSS.
What is a cascade?
Cascade word means waterfall. Not any waterfall. It's the one that has steps that consists of rocks.

Now imagine that every step is representing a differently placed rule that can apply style to your HTML.
When the water falls from one rock to another 'downwards' then it's not possible to go back with water 'up'. Water has fallen and that's it.
Coming back to CSS world.
The simplified (there is more to that) order is:
'Cascade' algorithm chooses the 'step' that is the lowest as the most important. Because these are on the 'lowest' place of 'waterfall'. Whatever rule is below overrides what is above.
Let's say you have sample.html file.
Inside sample.html you have a link to external CSS file (step 2), where you put selector and a rule like below:
p
{
color: red;
}
and in the same time in the head tag of sample.html, you put:
<style>
p
{
color: blue;
}
</style>
According to 'cascade' the lowest step is step nr 3.
That's why your paragraph is 'blue' not 'red'.
Why bother using 'cascade' word in this case that represents waterfall with rocks? Because when the water falls it TAKES everything with itself down to the bottom.
Why does it matter?
Because if you put inside sample.html in external CSS file also:
p
{
color: red;
font-weight: bold;
}
You won't remove font-weight: bold; using:
<style>
p
{
color: blue;
}
</style>
You will just change the color. Every other rule from 'steps' before are 'kept'. And that's the beauty in it.
Cascade and Specificity what you need to know:
CSS declaration marked with !important have the highest priority.
But only use !important as a last resource. It's better to use correct specificities- more maintainable code!
Inline styles will always have priority over styles in external stylesheets.
A selector that contains 1 ID is more specific than one with 1000 classes.
A selector that contains 1 class is more specific that one with 1000 elements.
The universal selector * has no specificity value(0,0,0)
Rely more on specificity than on the order of selectors.
But rely on order when using 3rd party stylesheets-always put your author stylesheet last.
In choosing what CSS styles to apply to an HTML element, specificity overrides generality according to a cascading set of rules that settle conflicts between styles:
A CSS selector that matches a more specific combination of tags, classes, and/or IDs will take priority. Of the following examples, the first will take precedence over the second, regardless of their order of appearance in the CSS:
ol#identity li.firstname { color: red; }
#identity .firstname { color: blue; }