What are legal characters for an HTML element id?

This question already has an answer here:

  • What are valid values for the id attribute in HTML? 21 answers

  • In HTML5, the only restrictions are that the ID must be unique within the document, contain at least one character and contain no spaces. See http://www.w3.org/TR/2014/REC-html5-20141028/dom.html#the-id-attribute

    As other answers have pointed out, HTML 4 is more restrictive and specifies that

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


    In HTML 4, the id attribute holds a NAME token which are defined:

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

    As pointed out in a previous post, depending on your implementation you may have troubles with colons and periods along with others: What are valid values for the id attribute in HTML?


    From http://www.w3.org/TR/html4/types.html:

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

    链接地址: http://www.djcxy.com/p/15442.html

    上一篇: 为什么元素ID不能以整数开头?

    下一篇: 什么是HTML元素ID的合法字符?