命名和设置多字 CSS 类和 ID 的最佳实践是什么?
我试图找出命名和设置 css 类和 id 的最佳实践,尤其是多个单词名称。
举例来说,假设我有一个
,我想将其命名为“角色技能”。 似乎有 3 个选择:“characterskills”、“character_skills”或“character-skills”。以下哪一项是命名 css 类和 id 的行业标准?
在 css 名称中拆分多个单词的最佳实践是什么?
另外,最好的做法是始终使用全小写的 css 名称,因为它们不区分大小写?
I'm trying to find out what is the best practice for naming and casing css classes and ids, especially multiple word names.
So for instance, say I have a <div>
that I want to name "character skills".
It seems like there are 3 choices: "characterskills", "character_skills", or "character-skills".
Which one of these is the industry standard for naming css classes and ids?
What's the best practice for splitting multiple words in css names?
Also is it a best practice to always use all lowercase for css names, because they are case-insensitive?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
我也倾向于使用连字符的样式。我主要使用该样式,因为 CSS 属性遵循相同的大小写。同样,JavaScript 函数和变量倾向于使用小驼峰式大小写。例如,要在 JavaScript 中更改 CSS 属性,您可以输入
object.style.textDecoration
,但在 CSS 中,该属性将通过text-decoration
进行更改。I tend to use the hyphenated style as well. I mainly use that style since CSS properties follow the same casing. Similarly, JavaScript functions and variables tend to use lower camel case. For example, to change a CSS property in JavaScript, you would type
object.style.textDecoration
, but in CSS, that property would be changed withtext-decoration
.我使用 lowerCamel 作为类名,使用 UpperCamel 作为 ID。这非常重要,我正在击败这个旧答案,因为在我看来,应该劝阻连字符的样式,即使下划线也比连字符更好。
为什么?因为其他所有语言都不能有连字符的变量名称。例如,您的 IDE 可能会也可能不会正确地自动完成。 (我的 IDE 不能,是 VI :P)
CSS 与 JavaScript 密切相关,带连字符的类名也使得与 JavaScript 的互操作变得困难。考虑以下(人为的)jQuery:
除了主观美学之外,使用连字符样式没有明显的优势。缺点是它从其他编程语言中脱颖而出(好吧,CSS 可能不是一种编程语言,哦,好吧..),而且它技术上是不正确的。
表示空格的正确(嗯?)方法是下划线。考虑这个短语“一封写给自己的信件”,我该如何转换空格?
我想在这种情况下,最好始终使用 PascalCasing,因为它有助于提高可读性。
I use lowerCamel for class names and UpperCamel for IDs. This is quite important and I'm beating this old answer because IMO the hyphenated style should be discouraged, even underscore is better than hyphenated.
Why? Because every other language can't have hyphenated variable names. For e.g., your IDE may or may not pick up auto-completion properly. (My IDE can't, it's VI :P)
CSS being closely related to JavaScript, hyphenated classname also make it difficult to interop with JavaScript. Consider the following (contrived) jQuery:
There's no clear advantage to using the hyphenated style other than subjective aesthetics. The disadvantages are that it stands out from every other programming language (OK, CSS may not be a programming language, oh well..) and that it is technically incorrect.
The correct (huh?) way to represent a space is underscore. Consider this phrase "a self-addressed letter," how can I convert the spaces?
I guess in this case, it's a best practice to always use PascalCasing because it aids readability.
我个人使用连字符样式(即
some-class
),但您应该简单地选择您认为最好的样式并保持一致。这纯粹是一个审美问题。I personally use the hyphenated style (i.e.
some-class
) but you should simply choose what you feel is best and be consistent. It is purely an issue of aesthetics.我经常看到以下外壳样式:
characterSkills,
角色技能
但是,归根结底,您选择哪种风格并不重要。只需在您自己的应用程序中保持一致即可。
I see the following casing styles a lot:
characterSkills,
CharacterSkills
But, at the end of the day it doesn't matter which style you pick. Just be consistent within your own app.
我见过这个问题的几个不同的答案,这取决于你问的是谁。涵盖您提到的所有可能性以及可能更多。然而,我看到最常引用的一个是使用下划线(character_skills)并且全部小写。
不过,总是提到的一个答案可能比您选择哪种方法更重要,那就是选择一种方法并始终坚持下去。始终保持统一可以避免以后出现混乱和其他问题。
I've seen several different answers to this question, depending on who you ask. Ranging through all of the possibilities you mentioned and probably more. The one I see referenced most often, however is to use underscores (character_skills) and all lowercase.
The one answer thats always mentioned though and arguably more important than which method you choose, is to pick one and stick to it throughout. Keeping things uniform throughout allows you to avoid confusion and other problems later.
我使用 lowerCamelCase 表示类,使用 UpperCamel 表示 ID,如下所示:
但只要保持一致,它就完全没有区别:) 哦,并尽可能坚持使用单字类名称 - 您始终可以组合类,像这样:
...我更喜欢,因为您可以让“基”类保存核心定义,使 CSS 更小,并减少设计页面时必须记住的类总数。
I use lowerCamelCase for classes, and UpperCamel for IDs, like so:
But it really makes absolutely no difference so long as you're consistent :) Oh, and try to stick to one-word class names where possible - you can always combine classes, like so:
...which I prefer, as you can have your "base" classes hold core definitions, keeping your CSS smaller, and reducing the overall number of classes you have to remember when designing pages.
在阅读示例并犯下自己的错误后,我为自己找到了这个解决方案:
#products-MainContent {}
或#maincontent-Summary {}
。对我来说,这意味着 MainContent 是 products div 的子级。#summary-Statistics
(ID) 或.summary-statistics
(class)这目前对我有用。我不想使用连字符来分隔单词,因为我认为连字符应该显示依赖性/关系。另外,我不想混淆 ID 和类,因为它们看起来很相似,所以我更改了编写它们的大小写。
After reading examples and making my own mistakes, I landed on this solution for myself:
#products-MainContent {}
or#maincontent-Summary {}
. To me this means that MainContent is a child of the products div.#summary-Statistics
(ID) or.summary-statistics
(class)This works for me for now. I don't want to use hyphens to separate words because I think hyphens should show dependency/relationships. Also I don't want to mix up IDs and Classes because they look similar so I changed the case in which they were written.
我建议使用BEM(块元素修改器)。
从其网站检索:
有关更多详细信息,请访问其网站文档:
http://getbem.com/naming/
I recommend using the BEM (Block Element Modifier).
Retrieved from its website:
For more details please visit its website documentation:
http://getbem.com/naming/