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 document is purely an Editor's Draft. It has not yet been adopted by the Working Group, and should not be considered to be part of CSS.
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, allowing the inner rule's selector to reference the elements matched by the outer rule. 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 }
&:first-child, &: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 the 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 matched by the parent rule set's selector list.
Introducing the ''&'' character will cause issues with CSS embedded directly in XML, as it's the first character used in CSS syntax that either requires escaping or using CDATA. Do we need to change this?
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, div .constant, p .keyword, 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 both appear in a single declaration block, and in any order.
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) "]";}
Note: Though it's unlikely that stylesheets authored with Hierarchies will be useful in legacy user agents, as the nested sections will be ignored by the error-handling rules, authors can minimize the damage caused by error-recovery by always putting properties before nested rules.
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 DOMString expandedSelectorText;
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.
The expandedSelectorText attribute must return a DOMString object containing the result of replacing the ''&'' selector in the selectorText attribute with the expandedSelectorText attribute from the parent rule. If the rule has no parent rule, the expandedSelectorText attribute must return a DOMString object containing the same text as the selectorText attribute.
The OM here is meant to reflect the OM for ''@media'' rules. In particular, the lack of a link from a rule to its parent matches rules nested in ''@media''. Should we add such a property to both of these?
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]