Class IDs and Attributes in HTML5

"The id attribute is more stylish in HTML5" is written on some pages. If I use class attribute instead of id, is it HTML5 compliant? Thanks for your help.

+2


source to share


5 answers


I believe what you are talking about in this article . Well, nothing has changed - class

es and id

are used the same as in HTML4.

Except for one thing: the HTML4 spec says the following

ID and NAME must start with a letter ([A-Za-z]) and can be followed by any number of letters, numbers ([0-9]), hyphen ("-"), underscore ("_"), colons (":") and periods (".").



However, in the HTML5 spec, the requirement for a id

much less stringent

The value must be unique among all identifiers in the elements of the subtree and must contain at least one character. The value must not contain any whitespace characters.

This way HTML5 id

can accept more characters, which means the article you are talking about .

+5


source


Attributes still have two different purposes:

  • class

    can contain multiple classes, and multiple elements can have the same classes
  • id

    contains one identifier, and this identifier can only be used for item one .


The assertion you indicated exists because some restrictions on how IDs should look like were removed in HTML5 - classes never had these restrictions.

+5


source


The difference between ID

and class

is that it ID

must be unique on the same page, but class

can be used to multiply multiple times.

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

+2


source


In this regard, nothing has changed since HTML5. IDs are still unique and classes can be shared between elements. Not sure what the quote was referring to.

+2


source


class

intended for general purposes, but id

intended for a unique identification purpose. I mean, id uniquely identifies an element, and a class indicates that the elements of a group have the same type of behavior. Hopefully it clears up the prupose from class

and id

.

0


source







All Articles