哪些字符在CSS类名/选择器中有效? [英] Which characters are valid in CSS class names/selectors?
问题描述
在CSS类选择器中允许使用什么字符/符号?我知道以下字符无效,但什么字符有效
What characters/symbols are allowed within CSS class selectors? I know that the following characters are invalid, but what characters are valid?
~ ! @ $ % ^ & * ( ) + = , . / ' ; : " ? > < [ ] \ { } | ` #
推荐答案
您可以直接在 CSS语法中进行检查。
基本上 1 ,名称必须以下划线开头( _
),连字符( -
)或字母( a
- 如果第一个字符是连字符,则第二个字符必须是 2 ,否则为 2
Basically1, a name must begin with an underscore (_
), a hyphen (-
), or a letter(a
–z
), followed by any number of hyphens, underscores, letters, or numbers. There is a catch: if the first character is a hyphen, the second character must2 be a letter or underscore, and the name must be at least 2 characters long.
-?[_a-zA-Z]+[_a-zA-Z0-9-]*
以连字符或下划线开头的标识符通常保留对于浏览器特定的扩展程序,如 -moz-opacity
。
Identifiers beginning with a hyphen or underscore are typically reserved for browser-specific extensions, as in -moz-opacity
.
1
2 请注意,根据语法我链接,规则从TWO连字符开始,例如 - indent1
,无效。但是,我很确定我在实践中见过这个。
2 Note that, according to the grammar I linked, a rule starting with TWO hyphens, e.g. --indent1
, is invalid. However, I'm pretty sure I've seen this in practice.
这篇关于哪些字符在CSS类名/选择器中有效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!