DEV Community

Xiaohan Du
Xiaohan Du

Posted on

Improve SCSS structure

Alt Text

A bad example

An example SCSS which is structured as:

.widget {
    background-color: red;
    .widget-body {
        background-color: black;
        .widget-link-block {
            background-color: yellow;
            .widget-link-block-menu {
                background-color: green;
            }
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Which compiles to CSS:

.widget {
    background-color: red;
}

.widget .widget-body {
    background-color: black;
}

.widget .widget-body .widget-link-block {
    background-color: yellow;
}

.widget .widget-body .widget-link-block .widget-link-block-menu {
    background-color: green;
}
Enter fullscreen mode Exit fullscreen mode

It has 2 disadvantages:

  1. we are repeating keywords like 'widget'
  2. the compiled CSS relies on HTML structure, i.e. it only works on the following HTML:
<div class='widget'>
  <div class='widget-body'>
    <div class='widget-link-block'>
      <div class='widget-link-block-menu'>

      </div>
    </div>
  </div>
</div>
Enter fullscreen mode Exit fullscreen mode

Violating the hierarchy would cause them stop working:

<div class='widget-body'>
<div>
Enter fullscreen mode Exit fullscreen mode

does not work

<div class='widget'>
  <div class='widget-body'>
    <div class='widget-link-block-menu'>
      <div class='widget-link-block'>

      </div>
    </div>
  </div>
</div>
Enter fullscreen mode Exit fullscreen mode

does not work

Suggested method

Use SCSS properly and Follow BEM

The above SCSS example can be modified to:

.widget {
    background-color: red;
    &-body {
        background-color: black;
    }
    &__link {
        &__block {
            background-color: yellow;
            &__menu {
                background-color: green;
            }
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

which compiles to CSS:

.widget {
    background-color: red;
}

.widget-body {
    background-color: black;
}

.widget__link__block {
    background-color: yellow;
}

.widget__link__block__menu {
    background-color: green;
}
Enter fullscreen mode Exit fullscreen mode

As you can see, the compiled CSS is more compact, less complex. When write SCSS, we no longer repeat writing '.widget'. More importantly, the compiled CSS no longer relies on HTML structure, it can be used like this:

<div class='widget'>
  <div class='widget-body'>
    <div class='widget__link__block'>
      <div class='widget__link__block__menu'>

      </div>
    </div>
  </div>
</div>
Enter fullscreen mode Exit fullscreen mode

or in different order:

<div class='widget'>
  <div class='widget-body'>
    <div class='widget__link__block__menu'>
      <div class='widget__link__block'>

      </div>
    </div>
  </div>
</div>
Enter fullscreen mode Exit fullscreen mode

or individually:

<div class='widget-body'>
</div>
Enter fullscreen mode Exit fullscreen mode

Top comments (0)