The CSS cascade
Derek Bridge
Department of Computer Science,
University College Cork
The CSS cascade
Aims:
- to understand the idea of inheritance in CSS
- to know what a rule conflict is in CSS
- to understand the cascade in CSS
Inheritance
- Many CSS properties (but not all) are inherited
inheritance in CSS
a property applied to an element is also automatically applied to the
element's descendants
Over-riding inheritance
- The inheritance of a property is blocked if some other CSS rule specifies a value
for that property
- Class exercise: What colour will "CS1102" be? What colour will
"Internet Computing" be?
Resolving conflicts: origin
- Stylesheets have different origins
- author stylesheets
- user stylesheets
- the browser's default stylesheet
- (Simplified) If two rules conflict,
author rules win over user rules, which win over browser rules
Resolving conflicts: specificity
- But suppose both rules come from the same stylesheet...
- Some selectors are more specific than others
- Roughly(!), selecting by id is the most specific, then selecting by class
- Then, a selector with more descendants is more specific
- If two rules conflict, the more specific wins
Resolving conflicts: specificity
Class exercise: Which is the more specific in each
of these pairs of rules
h3 {color: blue;}
.cocktail {color: green;}
p em {color: yellow;}
em {color: gray;}
#instructions .action {color: teal;}
div ol li span {color: purple;}
Resolving conflicts: ordering
Class exercise
In the first paragraph of mojitos.html,
what colour will the word "Mojitos" be and what colour will the rest
of the paragraph be?
body p {color: red;}
p span {color: blue;}
#intro {color: green;}