css - 哪些字符在 CSS 类名/选择器中有效?

标签 css css-selectors

CSS 类选择器中允许使用哪些字符/符号?

我知道以下字符无效,但哪些字符有效

~ ! @ $ % ^ & * ( ) + = , . / ' ; : " ? > < [ ] \ { } | ` #

最佳答案

您可以直接在CSS grammar查询.

基本上1,名称必须以下划线 (_)、连字符 (-) 开头, 或一个字母 (az),后跟任意数量的连字符、下划线、字母或数字。有一个问题:如果第一个字符是连字符,第二个字符必须2 是字母或下划线,并且名称必须至少有 2 个字符长。

-?[_a-zA-Z]+[_a-zA-Z0-9-]*

简而言之,前面的规则转换为以下规则,摘自 W3C specification :

In CSS, identifiers (including element names, classes, and IDs in selectors) can contain only the characters [a-z0-9] and ISO 10646 characters U+00A0 and higher, plus the hyphen (-) and the underscore (_); they cannot start with a digit, or a hyphen followed by a digit. Identifiers can also contain escaped characters and any ISO 10646 character as a numeric code (see next item). For instance, the identifier "B&W?" may be written as "B&W?" or "B\26 W\3F".

以连字符或下划线开头的标识符通常保留给特定于浏览器的扩展,如 -moz-opacity

1 由于包含转义的 Unicode 字符(没有人真正使用),这一切都变得更加复杂。

2 请注意,根据我链接的语法,以两个 连字符开头的规则(例如,--indent1)是无效的.但是,我很确定我已经在实践中看到了这一点。

关于css - 哪些字符在 CSS 类名/选择器中有效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44633791/

相关文章:

java - 无法使用 CSS 选择器在模式窗口中提取密码字段 - Selenium Java

css - 为什么这个li :last-child not work?

html - 是否可以组合更多的 CSS 组合器?

html - 用 :nth-child? 选择一半的元素

CSS 元素定位

html - 我网站底部的白条

css - react CSS 闪烁动画不工作无限

javascript - 根据表中最低的 <span> 设置一组 <span> 的位置 jQuery

wordpress - 如何更改移动 View 菜单中最后一个按钮的文本颜色?

css - 在 IE11 中悬停时边距发生变化