-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,250
  • 5
  • 30
  • 55
Oto Shavadze
  • 34,391
  • 48
  • 116
  • 201

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
  • 61,907
  • 17
  • 128
  • 134
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
  • 7,080
  • 6
  • 41
  • 54