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/54356520/

相关文章:

html - 将样式赋予 tr 标签内的 css 类

html - 棘手的响应式水平菜单栏

jquery - 使用 jQuery 检查 div 是否有文本和某个类的跨度

css - 是否有 CSS 父级选择器?

jquery - 使用 jQuery 的多重选择器

html - 我如何修复我的 HTML 菜单(简单菜单在 WebKit 浏览器中行为不当)

javascript - 如何让每个元素点击触发对另一个元素的另一次点击

html - Font Awesome 不适用于 CSS 旋转

selenium - WebDriver with Java - 单击图像打开链接

html - 仅子元素的后半部分有不同的背景