cgwebprojects
cgwebprojects

Reputation: 3472

HTML 5 classnames and IDs

I know that in HTML4 classes and id cannot start with numbers.

I am coding in HTML 5/php and alot of my ids and classes have just numbers in them that point to a primary key in my database for jquery ajax calls.

I cannot find the documentation for HTML5 classes and id spec. Is it valid in HTML 5 to have class and id names such as,

class="122"
id="13213"

I have ran my output code through w3c conformance checker and all is good, but still I would like confirmation!?

Thanks

Upvotes: 4

Views: 1091

Answers (2)

Christoph
Christoph

Reputation: 51181

The official html5 specs are here: id-attribute in html5.

(Here you go for html4)

So in html5 the only restriction is minimum 1 char and no whitespaces.

Upvotes: 1

Mathias Bynens
Mathias Bynens

Reputation: 149514

I’ve researched this thoroughly and wrote about my findings: The id attribute got more classy in HTML5. From that article:

HTML5 gets rid of the additional restrictions on the id attribute. The only requirements left — apart from being unique in the document — are that the value must contain at least one character (can’t be empty), and that it can’t contain any space characters.

To target a classname or ID that starts with a digit in CSS or in JavaScript using the Selectors API, you should escape them. For example, to target the element with id="404", you can’t just use #404 — you’d have to escape it as follows:

#\34 04 {
  background: pink;
}

Upvotes: 7

Related Questions