Basic Rules of RSCSS and BEM Systems
Tags: css
Published: 2018-05-24 12:41:10

---
title: Basic Rules of RSCSS and BEM Systems
published: true
description: There is a lot of confusion about how to use CSS properly. Hopefully, this article will provide you clarity on how to use these systems in the right way.
tags: CSS.
---

Often, there is a lot of confusion on how to use CSS properly. This document is here to provide a clarification.

The **[SASS 3](http://sass-lang.com/)** preprocessor introduces a new syntax known as **[SCSS](http://sass-lang.com/documentation/file.SCSS_FOR_SASS_USERS.html)** which is used in Kolosek's projects.

One of the most important things to learn is [nesting in Sass](https://kolosek.com/nesting-in-less-and-sass/).

The two most frequently used guide systems are [RSCSS](http://rscss.io/index.html) and [BEM](http://getbem.com/introduction/). I will go through the basics of both systems in order to ensure you use them properly in any current and future projects.

##General Rules

First, let's take a look at some general CSS rules that should be followed. 

1. When writing CSS, **classes** should be used, while **IDs should be avoided**. 
2. Use the **SCSS** syntax, **never the SASS** syntax.
3. Use **two spaces** for indentation.
4. When using multiple selectors in the rule declaration, **give each selector its own line**.
5. When a **margin or padding helper class** needs to be used, first see if it could be incorporated into the elements CSS rules, to **avoid having too many classes added to a single element**.
6. Do not nest selectors more than [**2 levels deep**](https://kolosek.com/nesting-in-less-and-sass/). 

##**[RSCSS](http://rscss.io/index.html)**


####**Components**

When working with RSCSS you should think in **components**. Each piece of UI should be viewed 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: 
* search-form,
* photo-container,
* primary-button.


####**Elements**


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:
* userimg,
* smalltext,
* navlink.


Using **tag names** and **ids** while writing your CSS should be avoided. 


####**Variants**

Variants can be used for** both component and element names**. 
Class names for variants are **prefixed with a dash**. 

For example: 
* class="button-primary -large",
* class="avatar -round",
* class="input-form -wide".

In the example below, I will break down the components into elements and show a real life usage of RSCSS.

![Group-10--1-](https://kolosek.com/content/images/2017/12/Group-10--1-.png)

The image shows a simple input field with a button and placeholder text.
First, we will **identify the component and break it down to elements**. 

![Group-11](https://kolosek.com/content/images/2017/12/Group-11.png)

The component includes the input field and submit button.

![Group-11.1--1-](https://kolosek.com/content/images/2017/12/Group-11.1--1-.png)

Now, it is time to write the CSS classes. I'll name the **component, element, and variable**. 

```
.signup-form {
   .inputfield {
     ...
   }
   .submitbtn {
     ...
     
     &.-inverted {
       ..
     }
   }
}
```

For naming the elements, I connected the two words (didn't use a dash or underscore). A variable was used for defining the style of the button. Therefore, both submitbtn and -inverted of the classes will have to be used on the button element.


##**[BEM](http://getbem.com/introduction/)**

BEM is a methodology that based on the Block - Element - Modifier system. Only class name selectors are used, ids are to be avoided.

####**Blocks**

**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**

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**

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, you can see an example of the BEM methodology.

![Group-18--1-](https://kolosek.com/content/images/2017/12/Group-18--1-.png)

The above image shows a block which has the class "**form**". It consists of an input field and a submit button. 

![Group-17](https://kolosek.com/content/images/2017/12/Group-17.png)

Now, 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, our 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. 

Be sure to check other detailed articles related to CSS properties such as this one: [CSS Positions](https://kolosek.com/css-position-relative-vs-position-absolute/), [SASS and LESS Nesting](https://kolosek.com/nesting-in-less-and-sass/), [CSS Columns](https://kolosek.com/css-columns/).

This article is originally published on [Kolosek Blog](https://kolosek.com/css-formatting/?utm_source=dvt).