Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it valid to start an ID with a number in HTML5? Do other technologies support these id's if we are using HTML5 doctype? [duplicate]

Recently had a debate on a question here where the user in the answer said that ID starts with a number is acceptable in HTML5, so is that true? I don't see any relevant documentation on this topic, also he provided me a link to this answer here, but seems like relevant link is no more.

So the question is, do HTML5 allow ids to start with a number?

Now for sure this fails in CSS --

<div id="5hello">Fails Completely</div>

#5hello {
   color: red;
}

Update: I am not even sure, that HTML5 allows the id's to start with a number or not, it's just that declaring id on an element is not enough, as HTML alone is not a question here, but does using <!DOCTYPE html> enables other technologies like CSS and JavaScript to work with the id's starting with no.? Am sure CSS fails, not tested with JavaScript but am sure that will fail too.

like image 828
Mr. Alien Avatar asked Oct 03 '22 23:10

Mr. Alien


1 Answers

According to this guy the answer is YES.

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.

You should follow W3C documents regarding this question: link

3.2.3.1 The id attribute

The id attribute specifies its element's unique identifier (ID). [DOMCORE]

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.

An element's unique identifier can be used for a variety of purposes, most notably as a way to link to specific parts of a document using fragment identifiers, as a way to target an element when scripting, and as a way to style a specific element from CSS.

Identifiers are opaque strings. Particular meanings should not be derived from the value of the id attribute.

Edit 1: I really like this comment, so if the author does not mind I will add it to my answer.
“CSS fails” – no, you just have to do it right: link

like image 191
radu florescu Avatar answered Oct 19 '22 22:10

radu florescu