The CSS guidelines of the project are fairly simple, and are based in common sense, semantic nomenclature and the BEM structure (Block, element, modifier).
For starters we have to state that we are targeting the latest and greatest browsers, so the use of CSS3 is allowed and suggested.
Don't write a rule for an HTML element, instead give the element a class attribute and write a rule for that class name. Why not using element tags? because one day you might have to correct your tags to give more semantical meaning, and you would have to change the stylesheet as well (there . is an exception for this, please read next item).
If you are creating a web component, eg, Angular component, React Component, Polymer component it is permitted to write a rule for the default state of this component targeting the html tag you chose for such component. This would mimic the default styles that regular web elements come bundled with such as the paddings and margins of p, h1, ht etc.
Don't write a rule for an id (#elementId), instead give the element a class attribute and write a rule for that class name. Why not using id's? Because you might change the id in the future for some reason, and then you would have to correct the stylesheet.
Specify font-sizes, line-heights, paddings and margins as rems. (1rem equals the font size declared for the body tag, 16px by default, if you need 8px of padding, then it would be 8px/16px which equals to 0.5rem).
Don't use visual descriptions on class names. For example .button–red is wrong. Use semantical meaning of the style instead, for example, ir the red color is used for dangerous actions, then use .button–danger. Why do we do this? because in the future we might style a dangerous action with purple instead of red. Same goes with adjectives like round, square, light, dark, etc. For those you could use (soft, sharp, regular, inverted, etc.)
Simple Straightforward, take the name of your Block (header, jumbotron, main menu), add double underscore (__) and the Element name for styling the block descendants, and add double dash (–) to add the Modifiers for those elements.
So, if we have a red and a green button on a footer the css nomenclature for all three would be:
/* The footer (B)lock */
/* The common button (E)lement rule */
/* a default background color */
/* The button (M)odifiers */
What about grandchilds?
First thing that comes to your mind is actually using double underscore to mimic the DOM, but no need of that, within BEM, there is only one level when referring to all of the descendants of the Block, so, for example when you have a list, you could name elements like this:
<li class="main-menu__list_item"><a class="main-menu__link" href="#!/one">One</a></li>
<li class="main-menu__list_item"><a class="main-menu__link" href="#!/two">Two</a></li>
/* a wrapping nav element */
For more reference please visit the following links:
Copyright © 2020, CEMEX International Holding AG. All rights reserved.