Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assigning a class attribute to the <head> element in HTML5

Tags:

html

css

In HTML 4 and XHTML 1, you can't assign a class to the <head> element. However, in XHTML 1.0 you can give it an ID.  In HTML5 it seems you can give it a class.  I am curious, why you would want to?

like image 892
wboevink Avatar asked Aug 03 '11 00:08

wboevink


1 Answers

class is one of what are now called global attributes (along with global events). They'll have to apply to every single element in the DOM, regardless of its nature.

I believe it's related to the API. The spec defines every DOM HTML element to inherit from a base interface called HTMLElement that defines the aforementioned global attributes and events. Namely:

interface HTMLElement : Element {

  // ...

  // metadata attributes
           attribute DOMString id;
           attribute DOMString title;
           attribute DOMString lang;
           attribute DOMString dir;
           attribute DOMString className;
  readonly attribute DOMTokenList classList;
  readonly attribute DOMStringMap dataset;

With that said, the editor(s) of the spec did make the following note at the end of the list of global attributes/events:

Note: While these attributes apply to all elements, they are not useful on all elements. For example, only media elements will ever receive a volumechange event fired by the user agent.

So I suppose they don't expect you to, but they can neither think of a reason to allow it or not to allow it. It's just part of the API (i.e. an HTMLHeadElement is an HTMLElement anyway).

like image 149
BoltClock Avatar answered Sep 28 '22 06:09

BoltClock