Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which casing should I use for CSS classes when using CSS-in-JS?

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;
}
like image 900
Anders Clark Avatar asked Oct 25 '25 05:10

Anders Clark


1 Answers

Ultimately 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}).

like image 105
Dexterians Avatar answered Oct 26 '25 19:10

Dexterians