All Packages  Class Hierarchy  This Package  Previous  Next  Index

Class org.w3c.css.css.CssCascadingOrder


public final class CssCascadingOrder
extends Object

   Cascading order

Conflicting rules are intrinsic to the CSS mechanism. To find the value for an element/property combination, the following algorithm must be followed:

  1. Find all declarations that apply to the element/property in question. Declarations apply if the selector matches the element in question. If no declarations apply, the inherited value is used. If there is no inherited value (this is the case for the 'HTML' element and for properties that do not inherit), the initial value is used.
  2. Sort the declarations by explicit weight: declarations marked '!important' carry more weight than unmarked (normal) declarations.
  3. Sort by origin: the author's style sheets override the reader's style sheet which override the UA's default values. An imported style sheet has the same origin as the style sheet from which it is imported.
  4. Sort by specificity of selector: more specific selectors will override more general ones. To find the specificity, count the number of ID attributes in the selector (a), the number of CLASS attributes in the selector (b), and the number of tag names in the selector (c). Concatenating the three numbers (in a number system with a large base) gives the specificity. Some examples:
       LI            {...}  /* a=0 b=0 c=1 -> specificity =   1 * /
       UL LI         {...}  /* a=0 b=0 c=2 -> specificity =   2 * /
       UL OL LI      {...}  /* a=0 b=0 c=3 -> specificity =   3 * /        {...}  /* a=0 b=1 c=1 -> specificity =  11 * /
       UL OL  {...}  /* a=0 b=1 c=3 -> specificity =  13 * / 
       #x34y         {...}  /* a=1 b=0 c=0 -> specificity = 100 * / 

    Pseudo-elements and pseudo-classes are counted as normal elements and classes, respectively.

  5. Sort by order specified: if two rules have the same weight, the latter specified wins. Rules in imported style sheets are considered to be before any rules in the style sheet itself.

The search for the property value can be terminated whenever one rule has a higher weight than the other rules that apply to the same element/property combination.

This strategy gives author's style sheets considerably higher weight than those of the reader. It is therefore important that the reader has the ability to turn off the influence of a certain style sheet, e.g. through a pull-down menu.

A declaration in the 'STYLE' attribute of an element has the same weight as a declaration with an ID-based selector that is specified at the end of the style sheet:

 <STYLE TYPE="text/css">
   #x97z { color: blue }
 <P ID=x97z STYLE="color: red">

In the above example, the color of the 'P' element would be red. Although the specificity is the same for both declarations, the declaration in the 'STYLE' attribute will override the one in the 'STYLE' element because of cascading rule number 5.

The UA may choose to honor other stylistic HTML attributes, for example 'ALIGN'. If so, these attributes are translated to the corresponding CSS rules with specificity equal to 1. The rules are assumed to be at the start of the author style sheet and may be overridden by subsequent style sheet rules. In a transition phase, this policy will make it easier for stylistic attributes to coexist with style sheets.

Constructor Index

 o CssCascadingOrder()

Method Index

 o order(CssProperty, StyleSheet, CssSelectors)
Order all properties and returns the winner.


 o CssCascadingOrder
 public CssCascadingOrder()


 o order
 public CssProperty order(CssProperty property,
                          StyleSheet style,
                          CssSelectors selector)
Order all properties and returns the winner.

property - The default value returned if there is no property.
style - The current style sheet where we can find all properties.
selector - The current context.
the property with the right value.

All Packages  Class Hierarchy  This Package  Previous  Next  Index