Many (if not all) HTML tags can have
Despite the wide use of
id attribute, many of us get it wrong, the value of the
id tag attribute to be precise.
According to the HTML 4.0 specification for basic types:
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 is a very basic rule and yet many of us get it wrong, not on purpose of course.
The most common mistake come with web applications that display data from a database. Most commonly a database record is uniquely identified in the database by a record id. This is number that is unique per database table. The common mistake is to use this (database)
id as a value of the
id attribute on a HTML page. The problem is that the database id is a number, but HTML ids cannot start with a digit. Remember HTML ids must start with a letter A-Z or a-z. Therefore the database id needs to be pre-pended with at least a single letter.
Even worse, I have seen web applications to use entity names as ids. These names are semi-unique, but may contain international characters, characters outside of A-Z and a-z range and even spaces.
If you are going to use a prefix before the database
id and you want to separate the two, I strongly advise you to use underscore ("_"). My reasons are the following:
Don't use spaces (" ")! The reason for this is simple. Space character is not a valid for id or name attribute.
Don't use hyphens ("-")! If you intend to use an
:hover for links).
For the geeky ones, it is possible to start an id with a number (if you really want to) but you need to represent this number with its Unicode escaped character.