CSS 类名广泛支持哪些字符?
正如此处以及其他地方所详细说明的, html/css 类名是 az、AZ、0-9、连字符和下划线,第一个字符应该是字母。但实际上,大多数浏览器实际上支持哪些字符?更具体地说,我想知道哪些浏览器可以正确理解类名中的斜杠(/),以及哪些浏览器支持以数字开头的类名。
我主要感兴趣的是获得 html 而不是 xhtml 的答案,以防存在差异。
谢谢。
As detailed here among other places, the only valid characters in a html/css class name is a-z, A-Z, 0-9, hyphen and underscore, and the first character should be a letter. But in practice, what characters are in fact supported by most browsers? More specifically, I wonder what browsers properly understands a slash (/) in a class name, and what browsers support class names starting with a number.
I'm primarily interested in getting an answer for html rather than xhtml, in case there is a difference.
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
请注意,类名是由 HTML 定义的,而不是由 CSS 定义的。 HTML4 说
class
属性是一个cdata-list
,它是空格分隔的标记。因此,单个类名标记可以包含除空白字符之外的任何字符。要在 CSS 类选择器中引用此类名称,您需要使用转义。例如:
匹配:
所有当前浏览器都支持此功能。 IE5 不支持它,但幸运的是这不再是一个问题。 (如果您担心字符编码不匹配,您可能更愿意将
ä
编码为\E4
,但这不是 CSS 本身的限制。)如果您询问哪些浏览器可以让您摆脱无效选择器的困扰
那么,谁在乎呢?只需使用有效的即可。
Note that class names are defined by HTML, not CSS. HTML4 says the
class
attribute is acdata-list
, which is space-separated tokens. So a single classname token can contain any character except the whitespace characters.To refer to such names in a CSS class selector you would need to use an escape. eg.:
is matched by:
This is supported by all current browsers. It wasn't supported by IE5, but that's thankfully no longer a concern. (If you had concerns about character encoding mismatches, you might prefer to encode the
ä
as\E4
, but that's not a limitation of CSS as such.)If you're asking which browsers will let you get away with the invalid selector
Then, well, who cares really? Just use the valid one.
http://www.w3.org/TR/2008/ REC-CSS2-20080411/syndata.html
如果您查看语法,您就会明白标识符的定义如下,
这就是您的答案..
(这是针对 CSS2 的)
http://www.w3.org/TR/2008/REC-CSS2-20080411/syndata.html
If you look at the grammar you understand that identifiers are defined as
so here's your answer..
(this is for CSS2)
Unicode 有效 http://snook.ca/archives/html_and_css/unicode_for_css_class_names
Unicode works http://snook.ca/archives/html_and_css/unicode_for_css_class_names
/ 和其他符号用作选择器(尤其是在 CSS3 中),因此不能在类名中使用。支持的字符是您已经说过的字符。
/ and other symbols are used as selectors (especially in CSS3) so can not be used in class names. The supported characters are the ones you already said.