admin管理员组文章数量:1356304
I've read that the CSS-spec does not specify case-sensitivity, which has led to some browsers treating, for example, .myheader
as equal to .myHeader
.
But since the standard in Javascript is to write variables in camelcase, how do I reconcile these two when using CSS-in-JS?
My React code could look like this:
import style from './style.module.css'
//...
<Button className={style.myButton} />
And the CSS would then break casing-convention:
.myButton {
background-color: blue;
}
OR my React code could look like this (which is kinda ugly):
import style from './style.module.css'
//...
<Button className={style['my-button']} />
And the CSS would then follow the kebab-case convention:
.my-button {
background-color: blue;
}
I've read that the CSS-spec does not specify case-sensitivity, which has led to some browsers treating, for example, .myheader
as equal to .myHeader
.
But since the standard in Javascript is to write variables in camelcase, how do I reconcile these two when using CSS-in-JS?
My React code could look like this:
import style from './style.module.css'
//...
<Button className={style.myButton} />
And the CSS would then break casing-convention:
.myButton {
background-color: blue;
}
OR my React code could look like this (which is kinda ugly):
import style from './style.module.css'
//...
<Button className={style['my-button']} />
And the CSS would then follow the kebab-case convention:
.my-button {
background-color: blue;
}
Share
Improve this question
asked Jul 20, 2021 at 6:33
Anders ClarkAnders Clark
751 silver badge9 bronze badges
5 Answers
Reset to default 2Ultimately it is up to you. If you are the only person who will be working on the project it doesn't matter. If you are working with a team, discuss with them their preference.
Personally, I've used all forms across my projects. It just depended on what looked right or felt right with the code I was working with, like in your case where you've mentioned kebab-case looks a bit ugly in amongst your React code.
Best practice would suggest kebab-case; but yeah, you don't have to, however, kebab-case may not work so well in the instance of modules/objects (className={style.like-this-example}
).
While agree with @Dexterians, its a choice that as team we should make. However, I would give preference to naming conventions over it's usage in a specific language. To me I would choose class name my-button over myButton and then usage style['my-button']
This way we stay consistent on names irrespective of language constraint.
In React first one always works in case of modules, so you do not need to worry about it. So, going with the below convention works fine.
React:
import style from './style.module.css'
//...
<Button className={style.myButton} />
CSS
.myButton {
background-color: blue;
}
Refer to this Medium link for more insight: CSS in JS
Wut? CSS is not case sensitive?
I think you need to read.
Are class names in CSS selectors case sensitive?
Are CSS selectors case-sensitive?
You've kind of contradicted yourself @Anders - while you're trying to follow conventions - you're doing as @Dexterians and @Bharat have said, doing your own thing that suits your needs :P
What is "convention" to one developer may not neccessaily be "convention" to another. Alas why you need to work with your team.
kebab-case is preferred and suggested by people for casing CSS classes. You can read here why it should be used
You can use it in react like this :
style['my-button']
I know you said it is ugly. But it is the only way.
本文标签: javascriptWhich casing should I use for CSS classes when using CSSinJSStack Overflow
版权声明:本文标题:javascript - Which casing should I use for CSS classes when using CSS-in-JS? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743958662a2568607.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论