COM271, Week 3

The cascade
Inheritance, specificity, and the cascade

Syllabus | Table of Pages | Assignments | References and Useful Links

Resolving Conflicts Between CSS Rules

Our first look at selectors showed that there may be several ways for an html element to be selected. Selectors include

So what if there are conflicts? For example, if the style sheet includes

p{color:white;}
#contents p{color:blue;}

What do we know about the color of a paragraph?

One factor in deciding which rule will be used ("win out") is the specificity of rules. Here is what Meyer says:

The answer is found in the specificity of each selector. For every rule, the user agent [=browser] evaluates the specificity of the selector and attaches it to each declaration inthe rule. When an element has two or more conflicting property declarations, the one with the highest specificity will win out.

A selector's specificity is determined by the components of the selector itself. A specificity value is expressed in four parts, like this: 0, 0, 0, 0. The actual specificity of a selector is determined as follows:

For example, the following rules' selectors result in the indicated specificities:

What wins in the following?

h1 {color:red;} /* 0, 0, 0, 1 */
body h1 {color:green;} /* 0, 0, 0, 2 (winner) */

h2.grape { color: purple } /* 0, 0, 1, 1 (winner) */
h2 { color: silver;} /* 0, 0, 0, 1 */

—Meyer, 2007, p. 63-64

But what is the final (left-most) column for? It is reserved for in-line styles. These have higher specificity than any rule from a style sheet, and thus always win.

If I have a style rule
h1 { color: green;}
and an inline style
<h1 style="color:red;">The University of Rhode Island</h1>
the style attribute always wins.

Importance

It is also possible to make some declarations so important that they outweigh all other considerations. These are called important declarations and you can mark them by inserting !important just before the terminating semicolon:

p.warn { color: #f9c !important; background: yellow; }

Inheritance

A style applied to an element is also applied to any elements that it contains. For example, if I set
ul {color:yellow;}
then list items contained within the list are also yellow. This is the idea of inheritance.

The Cascade

So how are conflicting rules sorted out to decide which ones win. Meyers lists 4 steps:

  1. Find all rules that contain a selector that matches a given element.
  2. Sort by explicit weight all declarations applying to the element. Sort by origin all declarations applying to a given element. There are three origins: author, reader, and user agent. Under normal circumstances, the author's styles win out over the reader's styles. !important reader styles are stronger than any other styles, including !important author styles. Bother author and reader styles override the user agent's default styles.
  3. Sort by specificity all declarations applying to a given element. Those elements with a higher specificity have more weight than those with lower specificity.
  4. Sort by order all declarations applying to a given element. The later a declaration appears in the style sheet or document, the more weight it is given. Declarations that appear in an imported style sheet are considered to come before all declarations within the style sheet that imports them.

W3C Schools
CSS Reference
Quick Index

CSS Basic

Selectors and Declarations CSS Introduction CSS Syntax CSS Id & Class CSS How To

CSS Styling

Styling Backgrounds Styling Text Styling Fonts Styling Links Styling Lists Styling Tables

CSS Box Model

CSS Box Model CSS Border CSS Outline CSS Margin CSS Padding

CSS Advanced

CSS Grouping/Nesting CSS Dimension CSS Display CSS Positioning CSS Floating CSS Align CSS Pseudo-class CSS Pseudo-element CSS Navigation Bar CSS Image Gallery CSS Image Opacity CSS Image Sprites CSS Media Types CSS Attribute Selectors CSS Don't CSS Summary

CSS Examples

CSS Examples

CSS Quiz

CSS Quiz CSS Certificate

CSS References

CSS Reference CSS Reference A to Z CSS Reference Aural CSS Web Safe Fonts CSS Units CSS Colors CSS Colornames CSS Colorvalues