Sunday, October 28, 2018

What are valid values for the id attribute in HTML?

HTML5: Permitted Values for ID & Class Attributes



As of HTML5, the only restrictions on the value of an ID are:





  1. must be unique in the document

  2. must not contain any space characters

  3. must contain at least one character



Similar rules apply to classes (except for the uniqueness, of course).



So the value can be all digits, just one digit, just punctuation characters, include special characters, whatever. Just no whitespace. This is very different from HTML4.




In HTML 4, ID values must begin with a letter, which can then be followed only by letters, digits, hyphens, underscores, colons and periods.



In HTML5 these are valid:



...

...

...

...

...


...

...

...



Just bear in mind that using numbers, punctuation or special characters in the value of an ID may cause trouble in other contexts (e.g., CSS, JavaScript, regex).



For example, the following ID is valid in HTML5:



...




However, it is invalid in CSS:



From the CSS2.1 spec:




4.1.3 Characters and case



In CSS, identifiers (including element names, classes, and IDs in

selectors) can contain only the characters [a-zA-Z0-9] and ISO 10646
characters U+00A0 and higher, plus the hyphen (-) and the underscore
(_); they cannot start with a digit, two hyphens, or a hyphen
followed by a digit
.




In most cases you may be able to escape characters in contexts where they have restrictions or special meaning.







W3C References



HTML5




3.2.5.1 The id
attribute



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




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.



Note: There are no other restrictions on what form an ID can take; in
particular, IDs can consist of just digits, start with a digit, start
with an underscore, consist of just punctuation, etc.



3.2.5.7 The class
attribute




The attribute, if specified, must have a value that is a set of
space-separated tokens representing the various classes that the
element belongs to.



The classes that an HTML element has assigned to it consists of all
the classes returned when the value of the class attribute is split on
spaces. (Duplicates are ignored.)



There are no additional restrictions on the tokens authors can use in

the class attribute, but authors are encouraged to use values that
describe the nature of the content, rather than values that describe
the desired presentation of the content.


No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...