During Kolosek’s work on multiple projects there was often a confusion about how to use CSS properly. This document helped us and provided clarification so I wanted to share it with you.
The SASS 3 preprocessor introduces a new syntax known as SCSS which is used in our projects.
One of the most important things to learn is nesting in Sass.
The two most frequently used guide systems are RSCSS and BEM. I’ll go through the basics of both systems in order to ensure they will be used properly in any current and future projects.
First, let’s take a look at some general CSS rules that should be followed.
When working with RSCSS you should think in components. Each piece of UI should be thought as an individual component.
Classes of components should be named with two words, with a dash in the middle.
Some of the naming examples include:
Each component contains elements. Classes should have only one word.
If an element contains multiple words, they should be joined together to make one word.
For example:
Using tag names and ids while writing your CSS should be avoided.
Variants can be used for both component and element names. Class names for variants are prefixed with a dash.
For example:
In the example below, I broke down the components into elements and showed a real life usage of RSCSS.
The image shows a simple input field with a button and placeholder text. First, let’s identify the component and break it down to elements.
The component includes the input field and submit button.
Now, it is time to write the CSS classes. Let’s name the component, element, and variable.
.signup-form { .inputfield { ... } .submitbtn { ...
&.-inverted { .. } }}
For naming the elements, you should connect the two words (don’t use a dash or underscore). As you can see, a variable was used for defining the style of the button. Therefore, both submitbtn and -inverted of the classes should be used on the button element.
BEM is a methodology that based on the Block — Element — Modifier system. Only class name selectors are used, ids are to be avoided.
Blocks can be nested, or can interact with each other. They are similar to components in RSCSS, and are semantically equal.
Bloks usually have a class name with one word, or two words separated by a dash. Block names may consist of Latin letters, digits, and dashes.
Elements are a part of the block.
Element names may consist of Latin letters, digits, dashes and underscores. The class name is formed with the name of the block plus two underscores and the element name. For example: “ forminput”.
Modifiers are used on elements to show a change in appearance, behavior or state. An elements design is changed by adding or removing a modifier.
Modifier names may consist of Latin letters, digits, dashes and underscores. A modifier class name is formed by taking the name of the element class and adding two dashes plus a name of the modifier. When an element has a modifier, it must have both the element class and modifier class.
For example: “blockinput blockinput — dashed”.
In the next example, I’ll show in a practical example of the BEM methodology.
The above image shows a block which has the class “form”. It consists of an input field and a submit button.
Next, you have identified the elements within the block. Those elements are “input” and “submit”. The “submit” element has an appearance modifier which makes the button wider.
According to all of this information, your stylesheet will be written as:
.form { }.form__input { }.form__submit { }.form__submit--wide { }
The HTML file will look like this:
<form class="form"> <input class="form__input" placeholder="your email address" / <input type="submit" class="form__submit form__submit--wide" /> </form>
I hope this article helped you understand the basics of RSCSS and BEM, as well as the class naming conventions.
Check out other detailed articles related to CSS properties such as this one: CSS Positions, SASS and LESS Nesting, CSS Columns.
Originally published at kolosek.com on May 24, 2018.