pepsicode
pepsicode

Reputation: 695

ID and class attributes in HTML5

"The id attribute got more classy in HTML5" is written at some pages. If I use class attribute instead of id, does it conform to HTML5? Thanks for your help.

Upvotes: 2

Views: 8015

Answers (5)

Josh Davenport-Smith
Josh Davenport-Smith

Reputation: 5511

I believe you're talking about this article. Well, nothing has really changed — classes and ids are used the same way as in HTML4.

Except one thing: The HTML4 spec says the following

ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens (“-“), underscores (“_”), colons (“:”), and periods (“.”).

However, in the HTML5 spec, the requirement for ids is much less rigid

The value must be unique amongst all the IDs in the element’s home subtree and must contain at least one character. The value must not contain any space characters.

So HTML5 ids can accept more characters, which is what the article you're reffering to is talking about.

Upvotes: 5

gotofritz
gotofritz

Reputation: 3381

Nothing has really changed with HTML5 in that respect. IDs are still unique, and classes can be shared across elements. Not sure what the quote was referring to.

Upvotes: 2

tbraun89
tbraun89

Reputation: 2234

The difference between ID and class is that the ID has to be unique at one page but the class can be used multiply times.

Both is valid HTML 5, you can validate your page here: http://validator.w3.org/

Upvotes: 2

sarwar026
sarwar026

Reputation: 3821

class is for generic purpose and id is for unique identification purpose. I mean, id uniquely identifies an element and class specifies a group elements to have the same type of behavior. I hope, it clears the prupose of class and id.

Upvotes: 0

ThiefMaster
ThiefMaster

Reputation: 318478

The attributes still have two different purposes:

  • class can contain multiple classes and multiple elements can have the same classes
  • id contains a single ID and that ID can only be used for one element.

The statement you quoted exists because some restrictions on how an ID must look like have been lifted in HTML5 - classes never had those restrictions.

Upvotes: 5

Related Questions