哪些字符在CSS类名/选择器中有效? [英] Which characters are valid in CSS class names/selectors?
问题描述
在CSS类选择器中允许使用什么字符/符号?我知道以下字符无效,但是什么字符有效
? 〜! @ $%^& *()+ =,。 /'; :?>< [] \ {} |`#
您可以直接在 CSS语法中进行检查。 / p>
基本上 1 ,名称必须以下划线开头( _
),连字符( -
)或字母( a
- z c>),后跟任意数量的连字符,下划线,字母或数字。有一个catch:如果第一个字符是连字符,第二个字符必须 2 字母或下划线,且名称长度必须至少为2个字符。
- ?[_ a-zA-Z] _a-zA-Z0-9 - ] *
以连字符或下划线开头的标识符通常保留浏览器特定的扩展程序,如 -moz-opacity
。
1
2 请注意,根据语法的不同,我链接,一个规则从TWO连字符开始,例如 - indent1
,无效。但是,我很确定我在实践中看到了这一点。
What characters/symbols are allowed within CSS class selectors? I know that the following characters are invalid, but what characters are valid?
~ ! @ $ % ^ & * ( ) + = , . / ' ; : " ? > < [ ] \ { } | ` #
You can check directly at the CSS grammar.
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-]*
Identifiers beginning with a hyphen or underscore are typically reserved for browser-specific extensions, as in -moz-opacity
.
1 It's all made a bit more complicated by the inclusion of escaped unicode characters (that no one really uses).
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屋!