In CSS, what is the difference between cascading and inheritance?
Or are both the same thing?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Inheritance is about how properties trickle down from an element to its children. Certain properties, like
font-familyinherit. If you set a font-family on thebody, that font family will be inherited by all the elements within thebody. The same is true forcolor, but it is not true forbackgroundorheightwhich will always default totransparentandauto. 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:
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.
1000)100101Add 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.