在 CSS id 或类中使用驼峰式大小写是否可以?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11411568/
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
Is using camelCase in CSS ids or classes ok or not?
提问by James P.
Question is in the title. I'm used to using camelcase in development and, since there are crossovers with design, was wondering if there are any technical reasons (regardless of opinions) against using it in HTML. Thanks in advance for your replies.
问题在标题中。我习惯于在开发中使用驼峰命名法,并且由于与设计存在交叉,我想知道是否有任何技术原因(不管意见)反对在 HTML 中使用它。提前感谢您的回复。
采纳答案by Jezen Thomas
Technically, no, there are no technical issues. Do what you like.
从技术上讲,不,没有技术问题。你喜欢什么。
Do try to follow a good style-guide though, like this one.
尽管如此,请尝试遵循一个好的风格指南,就像这个。
回答by srigi
There is one technical limitation if you use camelCase identifiers in your CSS - the |=
selector specifier:
如果您在 CSS 中使用驼峰式标识符,则存在一项技术限制 -|=
选择器说明符:
<form class="registration"></form>
<form class="search-movies"></form>
<form class="search-actress"></form>
To match only search forms, you can write:
要仅匹配搜索表单,您可以编写:
[class|="search"] { font-size: 150% }
You cannot do this with camelCase class names.
你不能用驼峰命名法来做到这一点。
回答by Kutalia
I'd been using camelCasing before today, let me tell the story: I created a bootstrap modal by the id name written in camelCasing. I couldn't manipulate it using bootstrap's own JQuery function. After searching for days, finally my co-worker found out that camelCasing was causing it. JavaScript case sensitivity can be tricky and unpredictable. So I suggest you to use hyphens.
今天之前我一直在使用camelCasing,让我讲一个故事:我通过camelCasing中写的id名称创建了一个引导模式。我无法使用引导程序自己的 JQuery 函数来操作它。经过几天的搜索,我的同事终于发现是camelCasing造成的。JavaScript 区分大小写可能很棘手且不可预测。所以我建议你使用连字符。
回答by Dave Haigh
Good question. I personally use camelCase in class/id names.There is no technical reason why you can't.
好问题。我个人在 class/id 名称中使用驼峰命名法。没有技术原因你不能。
However, after doing some quick reading on opinions, it seems alot of other developers/designers use hyphens over camelCase due to better readability.
然而,在快速阅读一些意见之后,似乎很多其他开发人员/设计师由于更好的可读性而使用连字符而不是驼峰命名。
Go with what you are comfortable coding in. I have got by fine using camelCase, I work in a team environment and never had an issue with readability for other developers.
使用你喜欢的编码方式。我已经很好地使用了驼峰命名法,我在团队环境中工作,对于其他开发人员来说,可读性从来没有问题。
Opinions on this that I have been reading can be found here:http://css-tricks.com/new-poll-hyphens-or-dashes/
我一直在阅读的关于此的意见可以在这里找到:http : //css-tricks.com/new-poll-hyphens-or-dashes/