0

Is it "correct" to put a string as an id? for example id="Nick Drake". I read here that in html4 it is not. It works well for my purpose, just wondering if it's right to do so.

Thanks

2 Answers2

3

There is no problem what so ever to use a string as an ID.

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

This does mean however, that you should not use spaces in your ID. "Nick Drake" should become "Nick_Drake"

Source: https://www.w3schools.com/tags/att_global_id.asp https://www.w3.org/TR/html4/types.html#type-id

If you ever have doubt about the validity of your html, run it through this service:

https://validator.w3.org/

Erik Baars
  • 2,278
  • 1
  • 8
  • 14
1

From MDN:

This attribute's value must not contain whitespace (spaces, tabs etc.). Browsers treat non-conforming IDs that contain whitespace as if the whitespace is part of the ID. In contrast to the class attribute, which allows space-separated values, elements can only have one single ID.

Also, I don't know what your purpose is, but keep in mind that (emphasis is mine):

This attribute's value is an opaque string: this means that web author must not use it to convey any information. Particular meaning, for example semantic meaning, must not be derived from the string.

Jordi Nebot
  • 3,355
  • 3
  • 27
  • 56