@satrop
Posted
@KyrieeWen - great question.
As a rule of thumb CSS classes are dash-separated, so I would take CreditCardForm
and change that to credit-card-form
. Camel case is usually reserved for JS.
So if you couple this naming convention with BEM, Block, Emelent, and Modifier you can keep things clean and easy to "human read" in the HTML layer.
Let's take your credit-card-form
for instance. This will sit on the parent <form>
and has children. Suppose your form has two buttons, submit and cancel following the BEM naming convention their class names might look like this...
credit-card-form__button
- this would sit on both buttons and give general styling to them. The __button
is the "E" (element) in BEM. Now we want to style one of the buttons differently. It might look like this...
credit-card-form__button--cancel
- this would sit on just one of the buttons and give different styles to that button, it has the modifier class attached to it denoted by the --cancel
double dash followed by the name.
You want to keep your class name "human readable" to make it easy for future you or other devs to come in and quickly see what's what.
To add to this, BEM doesn't have logic for grandchildren baked in. And so this isn't a rule at all but something I might do (anyone please feel free to comment about this idea), using your button as an example, might use a single underscore, like this...
credit-card-form__button_button-icon
- this might get a little long so you could keep it simple (ish) and stick to just using the BEM method adding a class name to a grandchild...
credit-card-form__button-icon
- this doesn't feel great to me.
Hope this helps!
All the best, Steve
Marked as helpful