XML technologies / XML / XML in a Nutshell / Cascading Stylesheets (CSS) / Selectors

(Excerpt from “XML in a Nutshell” by Elliotte Rusty Harold & W. Scott Means)

CSS offers limited ways to select elements for a rule. Many sheets use only names or comma-separated name lists, as demonstrated in A CSS stylesheet for recipes. Other simple selectors exist, although far less powerful than the XPath syntax in XSLT.

The universal selector

An asterisk matches every element, applying a rule unless a more specific conflicting rule exists. This rule gives all document elements a large font:

* {font-size: large}

Targeting descendants, children and siblings

An element name A followed by another name B selects all B elements descending from A elements. This rule matches menge descendants of zutaten, but not those elsewhere:

zutaten menge {font-size: medium}

With a greater-than sign (>) between names, the second must be an immediate child of the first. This rule gives menge children of zutaten the same font size (font-size) as the zutaten element:

zutaten > menge {font-size: inherit}

With a plus sign (+), the second must be the next sibling immediately after the first. This rule sets border-top-style only for the first geschichte element following an anweisungen element:

anweisungen + geschichte {border-top-style: solid}

Attribute selectors

Brackets select elements by attributes or values. This rule hides all schritt elements with an attribute optional:

schritt[optional] {display: none}

This rule hides every element with the attribute optional, regardless of name:

*[optional] {display: none}

An equals sign selects by a specific attribute value. This rule hides all schritt elements with the attribute optional set to yes:

schritt[optional="yes"] {display: none}

The operator ˜= selects elements whose attribute value includes a whole word. Words are separated, as in an NMTOKENS or ENTITIES attribute, by whitespace. This is not substring matching. This rule makes all rezept elements bold when their quelle attribute includes “Anderson”:

rezept[quelle˜="Anderson"] {font-weight: bold}

The operator |= tests the first word of a hyphen-separated list, such as Anderson-Harold or fr-CA.

CSS also selects elements by ID even without knowing the ID attribute’s name. Separate the ID from the element name with a hash #. This rule matches only the schritt element whose ID value is P833:

schritt#P833 { font-weight: 800 }

Pseudo-class selectors

Pseudo-class selectors match conditions unrelated to names. There are seven, separated from element names by a colon.

  • first-child

    The pseudo-class first-child matches the named element’s first child. Applied to Marjorie Anderson’s Southern Corn Bread recipe, this rule italicizes the first and only the first schritt element:

    schritt:first-child {font-style: italic}

  • link

    The pseudo-class link matches only unvisited link sources of the given type. This rule makes links blue and underlined:

    *:link {color: blue; text-decoration: underline}

  • visited

    The pseudo-class visited matches visited links of the given type. This rule makes them purple and underlined:

    *:visited {color: purple; text-decoration: underline}

  • active

    The pseudo-class active applies to elements the user activates, for example by clicking. Activation depends on context and is not supported everywhere. This rule makes active elements red:

    *:active {color: red}

  • linking

    Link pseudo-classes have poor XML support because most browsers do not yet recognize XLinks. Mozilla and Netscape 6, the only two that do, are also the only ones applying these classes to XML.

  • hover

    The pseudo-class hover matches elements under the pointer but not yet activated. This rule makes them green and underlined:

    *:hover {color: green; text-decoration: underline}

  • focus

    The pseudo-class focus applies to the focused element. This rule draws a one-pixel red border around it, if one exists:

    *:focus {border: 1px solid red }

  • lang

    The pseudo-class lang matches elements in the language specified by xml:lang. This rule uses David New Hebrew for Hebrew elements, specifically those whose xml:lang attribute is he or a subtype:

    *:lang(he) {font-family: "David New Hebrew"}

Pseudo-element selectors

Pseudo-elements match things that are not elements. Like pseudo-classes, a colon attaches them to an element selector. There are four:

  • first-letter

  • first-line

  • before

  • after

The pseudo-element first-letter selects the first letter. This rule makes the first letter of geschichte a drop cap:

geschichte:first-letter {
   font-size: 200%;
   font-weight: bold;
   float: left;
   padding-right: 3pt
}

The pseudo-element first-line formats the first line of a block. Resizing the browser changes which characters are on that line, and formatting adapts. This rule displays the first line of geschichte in small caps rather than lowercase:

geschichte:first-line {font-variant: small-caps}

The pseudo-elements before and after select points immediately before and after an element. Font or text styles cannot apply to a zero-width point, but the property content inserts text there. This rule inserts the literal string “Zutaten!” before zutaten:

zutaten:before {content: "Zutaten! "}

This rule prefixes each schritt element with its step number as 1., 2., 3., etc.:

schritt:before {
   content: counter(schritt) ". ";
   counter-increment: schritt;
}

  

 

 

 

back to top