Copyright © 2011 W3C® (MIT, ERCIM, Keio), All Rights Reserved. W3C liability, trademark and document use rules apply.
CSS is a language for describing the rendering of structured documents (such as HTML and XML) on screen, on paper, in speech, etc. This module contains the features of CSS level 3 relating to the hierarchical nesting of style rules. It includes and extends the functionality of CSS level 2 [CSS21], which builds on CSS level 1 [CSS1]. The main extension compared to level 2 is the ability to nest a style rule within another rule, allowing greater modularisation and readibility of CSS documents.
This is a public copy of the editors' draft. It is provided for discussion only and may change at any moment. Its publication here does not imply endorsement of its contents by W3C. Don't cite this document other than as work in progress.
The (archived) public mailing list www-style@w3.org (see instructions) is preferred for discussion of this specification. When sending e-mail, please put the text “css3-hierarchies” in the subject, preferably like this: “[css3-hierarchies] …summary of comment…”
This document was produced by the CSS Working Group (part of the Style Activity).
This document was produced by a group operating under the 5 February 2004 W3C Patent Policy. W3C maintains a public list of any patent disclosures made in connection with the deliverables of the group; that page also includes instructions for disclosing a patent. An individual who has actual knowledge of a patent which the individual believes contains Essential Claim(s) must disclose the information in accordance with section 6 of the W3C Patent Policy.
The following features are at risk: …
This section is not normative.
CSS beyond level 2 is a set of modules, divided up to allow the specifications to develop incrementally, along with their implementations. This specification is one of those modules.
This module describes support for nesting a style rule within another style rule, causing the inner rule to adopt the selector of the outer rule as a selector prefix. This feature allows related styles to be aggregated into a single structure within the CSS document, improving readability and maintainability.
This module introduces new parser rules that extend the [CSS21] parser model. This module introduces new parser rules that extend the [[CSS4SELECTORS]] module.
This specification does not define any new properties or values.
CSS Rules for even moderately complicated web pages include lots of duplication for the purpose of styling related content. For example, here is a portion of the CSS markup for one version of the [CSS3COLOR] module:
table.colortable td {text-align:center } table.colortable td.c { text-transform:uppercase } table.colortable td:first-child, table.colortable td:first-child+td { border:1px solid black } table.colortable th {text-align:center; background:black; color:white } table.tprofile th.title {background:gray; color:white} table.tprofile th { width:29%;padding:2px } table.tprofile td { width:71%;padding:2px } table.hslexample { background: #808080; padding:1em; margin:0; float:left; } table.hslexample td,table.hslexample th { font-size:smaller;width:3em }
Hierarchies allow the grouping of related style rules, like this:
table { &.colortable { & td {text-align:center } &.c { text-transform:uppercase } & td:first-child, & td:first-child+td { border:1px solid black } & th {text-align:center; background:black; color:white } &.tprofile { & th { width:29%;padding:2px; &.title {background:gray; color:white} } & td { width:71%;padding:2px } } &.hslexample { background: #808080; padding:1em; margin:0; float:left; & td, & th { font-size:smaller;width:3em } } }
Besides removing duplication, the grouping of related rules improves readability and maintainability of the resulting CSS.
This specification provides a mechanism that allows for the nesting of style rules within other style rules. A nested style rule can used anywhere that a declaration can be used.
In order to accomplish nesting, this specification defines a new simple
selector called the nesting selector, represented in selectors by the
‘&
’ character. All complex
selectors in the selector lists of nested rule sets must start with the
nesting selector. The nesting selector represents the elements matches by
the parent rule set's selector list.
The following example using Hierarchies:
div { & .keyword {color: red;} &:hover {background-color: rgb(200, 255, 255);} }
...produces the same results as the following CSS:
div .keyword {color:red;} div:hover {background-color: rgb(200, 255, 255);}
The following example using Hierarchies:
div, p { & .keyword, & .constant {color: red;} }
...produces the same results as the following CSS:
div .keyword {color:red;} div .constant {color:red;} p .keyword {color:red;} p .constant {color:red;}
Multiple style rules can be embedded within a style rule. Style rules can be embedded arbitrarily deeply. Embedded style rules and properties can co-exist.
The following example using Hierarchies:
div, p { & .keyword {color: green;} font-size: 10px; & .constant { color: red; &:hover:after { content: " [" attr(value) "]";} background-color: green; } }
...produces the same results as the following CSS:
div, p {font-size: 10px;} div .keyword, p .keyword {color: green;} div .constant, p .constant {color: red; background-color: green;} div .constant:hover:after, p .constant:hover:after {content: " [" attr(value) "]";}
This specification alters the CSS Core Grammar and the Selectors grammar.
The following modifications are required to the [CSS21] grammar as defined in Section 4.1.1.
The ruleset rule is changed to:
ruleset : selectors_group? ruleset-body
The following rules are added:
ruleset-body : '{' S* ruleitem? [ ';' S* ruleitem? ]* '}' S* ruleitem : declaration | nested-ruleset nested-ruleset : nested-selectors-group ruleset-body
The following modifications are required to the [[!CSS4SELECTORS]] grammar as defined in Section 16.1.
The following rules are added:
nested-selectors-group : nested-selector [ COMMA S* nested-selector ] nested-selector : nested-compound-selector [ combinator compound-selector ]* nested-compound-selector : NEST [ HASH | class | attrib | pseudo | negation ]*
The following modifications are required to the [[!CSS4SELECTORS]] lexical scanner as defined in Section 16.2.
Immediately below:
"-->" return CDC;
the following rule is inserted:
"&" return NEST;
The following attribute is required to be added to the CSSStyleRule object defined in Section 6.4.3 of [CSSOM]:
interface CSSStyleRule : CSSRule { attribute DOMString selectorText; readonly attribute CSSRuleList cssRules; readonly attribute CSSStyleDeclaration style; };
The cssRules
attribute must return a
CSSRuleList
object for the list of CSS rules specified within
the style rule.
Conformance requirements are expressed with a combination of descriptive assertions and RFC 2119 terminology. The key words “MUST”, “MUST NOT”, “REQUIRED”, “SHALL”, “SHALL NOT”, “SHOULD”, “SHOULD NOT”, “RECOMMENDED”, “MAY”, and “OPTIONAL” in the normative parts of this document are to be interpreted as described in RFC 2119. However, for readability, these words do not appear in all uppercase letters in this specification.
All of the text of this specification is normative except sections explicitly marked as non-normative, examples, and notes. [RFC2119]
Examples in this specification are introduced with the words “for
example” or are set apart from the normative text with
class="example"
, like this:
This is an example of an informative example.
Informative notes begin with the word “Note” and are set apart from
the normative text with class="note"
, like this:
Note, this is an informative note.
Conformance to CSS TEMPLATE Module is defined for three conformance classes:
A style sheet is conformant to CSS TEMPLATE Module if all of its declarations that use properties defined in this module have values that are valid according to the generic CSS grammar and the individual grammars of each property as given in this module.
A renderer is conformant to CSS TEMPLATE Module if, in addition to interpreting the style sheet as defined by the appropriate specifications, it supports all the features defined by CSS TEMPLATE Module by parsing them correctly and rendering the document accordingly. However, the inability of a UA to correctly render a document due to limitations of the device does not make the UA non-conformant. (For example, a UA is not required to render color on a monochrome monitor.)
An authoring tool is conformant to CSS TEMPLATE Module if it writes style sheets that are syntactically correct according to the generic CSS grammar and the individual grammars of each feature in this module, and meet all other conformance requirements of style sheets as described in this module.
So that authors can exploit the forward-compatible parsing rules to assign fallback values, CSS renderers must treat as invalid (and ignore as appropriate) any at-rules, properties, property values, keywords, and other syntactic constructs for which they have no usable level of support. In particular, user agents must not selectively ignore unsupported component values and honor supported values in a single multi-value property declaration: if any value is considered invalid (as unsupported values must be), CSS requires that the entire declaration be ignored.
To avoid clashes with future CSS features, the CSS2.1 specification reserves a prefixed syntax for proprietary and experimental extensions to CSS.
Prior to a specification reaching the Candidate Recommendation stage in the W3C process, all implementations of a CSS feature are considered experimental. The CSS Working Group recommends that implementations use a vendor-prefixed syntax for such features, including those in W3C Working Drafts. This avoids incompatibilities with future changes in the draft.
Once a specification reaches the Candidate Recommendation stage, non-experimental implementations are possible, and implementors should release an unprefixed implementation of any CR-level feature they can demonstrate to be correctly implemented according to spec.
To establish and maintain the interoperability of CSS across implementations, the CSS Working Group requests that non-experimental CSS renderers submit an implementation report (and, if necessary, the testcases used for that implementation report) to the W3C before releasing an unprefixed implementation of any CSS features. Testcases submitted to W3C are subject to review and correction by the CSS Working Group.
Further information on submitting testcases and implementation reports can be found from on the CSS Working Group's website at http://www.w3.org/Style/CSS/Test/. Questions should be directed to the public-css-testsuite@w3.org mailing list.
[Change or remove the following CR exit criteria if the spec is not a module, but, e.g., a Note or a profile. This text was decided on 2008-06-04.]
For this specification to be advanced to Proposed Recommendation, there must be at least two independent, interoperable implementations of each feature. Each feature may be implemented by a different set of products, there is no requirement that all features be implemented by a single product. For the purposes of this criterion, we define the following terms:
The specification will remain Candidate Recommendation for at least six months.
[acknowledgments]