-1

In HTML, what characters are valid as the first character in an id or class attribute?

For example, is <div id="4bla"></div> valid or must they start with only letters?

neelsg
  • 4,802
  • 5
  • 34
  • 58
Oto Shavadze
  • 40,603
  • 55
  • 152
  • 236

2 Answers2

7

HTML5:

As of HTML5, it is indeed valid to start an ID with a digit.

In HTML5, even this is valid:

<p id="#">Foo.
<p id="##">Bar.
<p id="♥">Baz.
<p id="©">Inga.
<p id="{}">Lorem.

As is your example:

<div id="4bla"></div>

Note: It may be valid in HTML5, however it is not valid in CSS.

That means <div id="4bla"></div> is valid, but #4bla { background-color:red; } isn't.

Start ID's with characters instead for maximum compatibility.

HTML4:

It is invalid if you're still using HTML4:

"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 (".")."

dsgriffin
  • 66,495
  • 17
  • 137
  • 137
2

No, its not XHTML valid. You can use the XHTML-Validator to check your HTML code.

XHTML Validator Image

But even if its not XHTML-Valid it should work in almost every browser.

fnkr
  • 9,428
  • 6
  • 54
  • 61