How to name CSS classes
- Prefix with is/has
- use
.is
; that describes the element's own state
e.g.
.isOverflowHidden
.isHidden
.isScrollable
- use
.has
; this describes the state of something inside the element - try to make the end part a noun
e.g.
.hasOverflowHidden
.hasHiddenChildren
.hasScrollableChildren
- if you can, avoid this because it can be hard to use; try to re-write as a positive
- Use
isNot
and/orhasNo
- Use an adjective for "is"
- Try to stick a noun at the end for "has"; "Els" is a fine abbreviation for elements
- if all else fails, add "able"
e.g.:
.isNotHidden
, .hasNoHiddenEls
.isNotScrollable
, .hasNoScrollable
, .hasNoScrollableEls
In general, try to write the class in a way that it directly answers a close-ended question that could start with is, has, will, did, can, or should.
- Prefix with js/ui
- use
.js
e.g.
.jsSubmitButton
.jsVideoPlay
- use
.ui
- use an adjective or a past-tense
e.g.
.uiSubmitted
.uiVideoPlayed
- use
.qa
- Avoid words that prescribe the content
- Avoid words that prescribe the markup
- Avoid ambiguously describing scope or presentation
- Avoid using words to describe variation or relationships
e.g.: avoid these
.logos // suggests it can only have logos (prescribed content)
.logoList // suggests can only be used with ul, ol, dl (prescribed markup)
.logoGridList // suggests it always makes it looks like a grid (ambiguous presentation)
.darkLogoGridList //not obviously related, (words describe variation or relation)
.logoGridListBlack // Not clear what is black (ambiguous scope)
- Prefer words that describe the purpose
- Prefer describing scope
- Consider abbreviating scope if meaning can be obvious
- Prefer convention to prescribe variations
- Prefer convention to describe relationships
e.g. prefer these:
.mediaSet // 'Set' -> purpose, 'media' -> scope (could apply to video, picture, img)
.mediaSet--grid-md // '--' -> variation by convention, 'grid' -> purpose , 'md' -> scope (abbreviated)
.mediaSet--theme-dark // '--' -> variation by convention, 'theme' -> scope, '-dark' -> variation
.mediaSet__media // '__' -> relationship by convention , 'media' -> purpose
BEM is a well-known convention that helps address relationships and variations. You don't have to use BEM; you just have to be consistent.
- Be consistent
- If everyone else is using hyphens, you should, too.
- If you're the first person on the project, use camelCase; it's easier to select camelCased text in an IDE than it is hyphenated text
Sometimes a class only has one property because it's used in combination with other classes.
- Don't use the value of a specific CSS property
- Do give the purpose of that specific CSS property
- Avoid suggesting usage with only specific HTML elements
- Try suggesting usage with kinds of content
e.g. Avoid this:
.button--yellow {
color: yellow;
}
And instead try this:
.button--warn {
color: yellow
}
Avoid this:
.imgFloatLeft {
float: left;
}
.imgFloatRight {
float: right;
}
And instead try
.rtfMedia--left {
float: left;
}
.rtfMedia--right {
float: right;
}
-
don't let the variable name indicate the variable value (what if the value changes)
-
indicate relative aspects of the value like hue, lightness
-
use relatives and superlatives (er, est) for variations on a hue
--colorNeutralDarkest --colorNeutralDarker --colorNeutralDark --colorWarmDarker --colorWarmerDarkest
-
start with the UI element name
-
end with the CSS property it affects
-
never put any information about the value of the variable in the name
$linkColor: rgb(165,220,220,220) --linkColor: var(--colorCoolDark)
-
start with the UI element name
-
Then add the CSS property it affects
-
End with the state
$linkColor: rgb(165,220,220); $linkColorHover: rgb(165,165,220); --linkColorHover: var(--colorCoolDarker);
-
start with class name that the variable is used in
-
end with the CSS property it affects
.foo { $fooHeight: $containerHeight / 3; width: $fooHeight / 2; height: $fooHeight; }