tags:

views:

60

answers:

4

Hi,

Which HTML tags can use the class attribute?

(In which standards — e.g. HTML/XHTML/HTML5 etc. — and in which browsers.)

For example, is the following legal or not?

<i class="myclass">text inside</i>

Thanks.

+1  A: 

Well I am pretty sure, that each and every tag may contain the class-attribute (correct me, if I am wrong).

EDIT

Okay Gumbo stated the tags, that a not allowed to use the class-attribute for good reason (see my comment at his answer). Thank you.

faileN
+6  A: 

In HTML 4, the class attribute can be used in all elements except but BASE, BASEFONT, HEAD, HTML, META, PARAM, SCRIPT, STYLE, and TITLE (see index of attributes in the HTML 4.01 specification). And since XHTML 1.0 is (briefly) just HTML 4 with XML syntax, this does also apply to XHTML 1.0.

Gumbo
okay since this tags don't display any visual content, it should be clear that applying a class-attribute to this tags is senseless
faileN
@faileN — you seem to have fallen into the trap of assuming that the class attribute exists for no reason other than to allow CSS to be applied. That isn't the case.
David Dorward
It is probably worth pointing out that http://www.w3.org/TR/html401/index/attributes.html has a summary of which elements each attribute in HTML 4.01 apply to.
David Dorward
A: 

I would suggest using <em>text inside</em> rather than <i> as it is more semantically correct.

Adam Beizsley-Pycroft
This should be a comment, not an answer (it doesn't address the question at all) and is wrong anyway. It is possible to have text formatted as italics without meaning "this has emphasis".
David Dorward
On the comment vs answer front I'm still getting used to StackOverflow. Regardless, <i> tags are presentational and their use goes completely against the accepted wisdom of separating content from presentation.
Adam Beizsley-Pycroft
@Adam: “<i> tags are presentational and their use goes completely against the accepted wisdom of separating content from presentation” — this has changed in HTML5, `<i>` tags have meaning there: http://dev.w3.org/html5/spec/Overview.html#the-i-element
Paul D. Waite
+3  A: 

In HTML5, the class attribute, along with the accesskey, contenteditable, contextmenu, dir, draggable, hidden, id, lang, spellcheck, style, tabindex and title attributes, is valid on all elements. See http://dev.w3.org/html5/spec/elements.html#global-attributes

Alohci
That's also in the [Working Draft](http://www.whatwg.org/specs/web-apps/current-work/multipage/elements.html#classes): “Every HTML element may have a class attribute specified.”
Marcel Korpel