我可以在 CSS 类名中使用驼峰命名法吗
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1547986/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can I use camelCase in CSS class names
提问by KJai
I want to name a CSS class and call it imgSuper. Can I use camelCasing in CSS classes?
我想命名一个 CSS 类并将其命名为 imgSuper。我可以在 CSS 类中使用 camelCasing 吗?
回答by Warren Young
Technically yes, but it's risky because while CSS syntax is mostly case-insensitive, in some browsers under certain conditions, class names are treated as case-sensitive, as the spec does not specify how browsers should handle case when matching CSS rules to HTML class names.
从技术上讲是的,但这是有风险的,因为虽然 CSS 语法大多不区分大小写,但在某些浏览器中,在某些条件下,类名被视为区分大小写,因为规范没有指定在将 CSS 规则与 HTML 类匹配时浏览器应如何处理大小写名称。
From the spec, section 4.1.3:
All CSS syntax is case-insensitive within the ASCII range...
In CSS, identifiers (including element names, classes, and IDs in selectors) can contain only the characters [a-zA-Z0-9] and ISO 10646 characters U+00A1 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".
...the case-sensitivity of values of the HTML attributes "id" and "class", of font names, and of URIs lies outside the scope of this specification.
所有 CSS 语法在 ASCII 范围内都不区分大小写...
在 CSS 中,标识符(包括元素名称、类和选择器中的 ID)只能包含字符 [a-zA-Z0-9] 和 ISO 10646 字符 U+00A1 及更高,加上连字符 (-) 和下划线 ( _); 它们不能以数字开头,也不能以连字符后跟数字开头。标识符还可以包含转义字符和任何 ISO 10646 字符作为数字代码(参见下一项)。例如,标识符“B&W?” 可以写成“B\&W\?” 或“B\26 W\3F”。
... HTML 属性“id”和“class”、字体名称和 URI 的值的大小写敏感不在本规范的范围内。
Rather than learn the browsers and conditions where case is sensitive, it's best that you just realize that the best approach is the most compatible: use the same case in all code for a given CSS class, and don't create two or more CSS class names that differ only in case.
与其学习区分大小写的浏览器和条件,不如让您意识到最好的方法是最兼容的:在给定 CSS 类的所有代码中使用相同的大小写,并且不要创建两个或多个 CSS 类仅大小写不同的名称。
回答by Jacob Mattison
Sure. Hereare the official rules; basically you shouldn't start the name with a number, and you can use letters, numbers, hyphen, underscore, and escaped or encoded characters.
当然。 这是官方规则;基本上你不应该以数字开头,你可以使用字母、数字、连字符、下划线和转义或编码的字符。
However, as a matter of convention, hyphens are generally used instead of camel case.
但是,按照惯例,通常使用连字符代替驼峰式大小写。
回答by Guffa
Yes, class names are case sensitive, so that works fine.
是的,类名区分大小写,所以工作正常。
However, you should be aware that some browsers get this wrong, and don't treat class names as case sensitive. Therefore you should avoid using both the upper and lower case variations of the same name. The classes imgSuper
and imgsuper
may be treated as the same by some browsers.
但是,您应该意识到某些浏览器会出错,并且不要将类名视为区分大小写。因此,您应该避免同时使用同名的大写和小写变体。类imgSuper
和imgsuper
可能被某些浏览器视为相同。
回答by ozona
Yes you can. Just be sure that if you call a class "fooBar" in your css file that you use consistent caps when assigning class="fooBar" in your markup.
是的你可以。请确保如果在 css 文件中调用类“fooBar”,则在标记中分配 class="fooBar" 时使用一致的大写字母。