Class attributes are about more than styling

When people talk about microformats, they often have the same bit of confusion about the class attribute; it is often referred to as a “css class.”

I’m not criticizing anyone here, I just want to point out that there’s some confusing regarding the class attribute.

In the HTML 4 spec (from which XHTML is derived), classes are defined to have these purposes:

The class attribute, on the other hand, assigns one or more class names to an element; the element may be said to belong to these classes. A class name may be shared by several element instances. The class attribute has several roles in HTML:

  • As a style sheet selector (when an author wishes to assign style information to a set of elements).
  • For general purpose processing by user agents.

There’s a couple of points I’d like to highlight here:

First, notice that classes are multivalued, meaning that elements can have one or more, space-sepearted values.

Next, notice that class values are not just for styling. While this is certainly the most common and well-known use for class values, they are open “for general purpose processing by user agents,” which could easily include non-intrusive scipting and semantics. In fact, classes actually work best when treated as a mechanism for applying semantics, rather than just hooks for styling (see Tantek’s Class for Meaning, not for show).

For a more thorough primer on the use of class values, and how they relate to document structure, see Tantek‘s post, A Touch of Class, which though its 3 years old, is just as relevant today.

Technorati Tags:

6 Responses to “Class attributes are about more than styling”

  1. torresburriel.com » Los atributos class son algo ms que presentacin :

    [...] He cado en un texto, pequeo y rpido de leer, de Ryan King en Microformats, que se llama Class attributes are about more than styling, en el que pone encima de la mesa la [...]

    October 20th, 2005 at 4:14 pm

  2. Max Völkel :

    First, notice that classes are multivalued, meaning that elements can one or more, space-sepearted class values.

    this is not a sentence.

    elements can one or more … missing “have”

    I love microformats!

    October 23rd, 2005 at 3:01 pm

  3. Al :

    Hi just discovered : http://www.microformat.org/ by accident, are they related to the official site, i.e. this site?

    whats the story?

    Regards
    Al

    October 24th, 2005 at 4:16 pm

  4. Ryan :

    Al-

    Microformat.org is not related to this site in any way. As far as I can tell, it is a completely indepent project, run by people (who remain anonymous other than via whois data) with no desire to cooperate with this community (other than to take content).

    In short, no relation.

    October 24th, 2005 at 4:38 pm

  5. Ryan :

    Max-

    Should be fixed now. Thanks!

    October 24th, 2005 at 4:40 pm

  6. Etan Wexler :

    Notice that a class is an abstraction whose only serialized property is a name. Notice that a given “class” attribute, like any SGML or XML attribute, has exactly one value. Notice that the value of a “class” attribute, if specified in the markup, must comprise a list of one or more class names. Notice that the list of class names must use white space to separate the class names. Notice that the specification of HTML 4.01 specifies that white space comprises a sequence of characters from one repertoire. Notice that the specifications of XHTML specify, by reference to the specification of XML 1.0, that white space comprises a sequence of characters from another, albeit overlapping, repertoire.

    More than one novice has attempted duplicate attribute declarations for a given element. The phrasing “elements can have one or more, [...] values” unintentionally promotes such mistakes.

    November 11th, 2005 at 11:58 pm