Naming standards and best practices๐
Separation of powers ๐ฉโโ๏ธ๐จโโ๏ธ๐
You should always use CSS if you want to create or override styles.
When a mix of JavaScript and CSS is needed : logic should be handled in JavaScript while everything visual should be set in CSS. For exemple when components rely on user interaction, use JavaScript for event listeners, adding classes or changing HTML attributes but use CSS to hide, show, change something's color.
Naming standards๐
No IDs in CSS๐
Too specific. Too powerful. We want modularity โ
Class names should always be in English๐
Comments too.
No abbreviation๐
Do โจ ๐
1 |
|
Don't ๐ช ๐
1 |
|
Class names fit in a word๐
If they need more than a word, those words will be separated with a hyphen.
1 2 3 4 5 6 7 |
|
Class names have to be semantic๐
That means they should describe their component and its function. They shouldn't describe what the component looks like.
NB : That component can be super generic (e.g., card
, button
, banner
) or specific to the content (e.g., document-panel
, companies-list
).
Do โจ ๐
1 2 3 4 5 6 7 8 |
|
Don't ๐ช ๐
1 2 3 4 5 6 |
|
Rare exceptions can be made for some utility classes (e.g., show-screen-reader
, prevent-scroll
).
Class names are written following the BEM naming convention๐
BEM (Body Element Modifier) is a component based approach that considers each component as a brick that should be able to fit everywhere no matter the context. ๐งฑ That way you get modular styles and you can add, replace and remove blocks with minimum CSS. More about BEM
Best Practices๐
Use of '!important'๐
The !important
declaration in CSS is used to give a CSS property the highest
specificity
(the weight that is applied to a given CSS declaration).
1 2 3 |
|
With !important
, the background-color property will override any other conflicting declarations.
You should avoid using it as much as possible, because it leads to ; a reduced maintainability and reusability of your code, confusions, debugging difficulty or inheritance issues.
Do โจ ๐
-
Use it only on very specific cases
1 2 3 4
/* Using !important to override third-party library styles */ .third-party-button { background-color: blue !important; }
-
Be specific when writing CSS, and avoid using the same class for different looking components. Prefer creating specific CSS that applies to your particular needs rather than override existing code. As a best practice you can use the BEM naming convention for components.
1 2 3 4
/* BEM Naming Convention */ .block__element--modifier { background-color: red; }
Don't ๐ช ๐
-
Use it on a global scale
1 2 3 4
/* Don't use !important in a generic class */ .container { padding: 10px !important; /* Avoid this */ }
-
Use it for convenience
1 2 3 4
/* Avoid using !important for quick fixes */ .quick-fix { margin: 0 !important; }
-
Apply it to multiple properties unnecessarily
1 2 3 4 5 6
/* Bad Practice */ .bad-practice { padding: 10px !important; margin: 20px !important; background-color: red !important; }
-
Use inline styles AND !important ๐ช ๐๐ช ๐๐ช ๐
1 2
<!-- Bad Practice --> <div style="margin: 10px !important;">Content</div>