Architecture Decision Records (ADRs)

← Home

SCSS/CSS nesting max levels

Introduction

Nesting styles allow for less code repetition and cleaner code that is easier to read and quicker to write. However, too many nested levels can make code more difficult to read and make selectors too specific to override.

Decision

Max 4 levels of nesting. This does not include pseudo-classes, pseudo-elements and media queries.

For example in Drupal node page it can be structured below:

Code block example:

  .node--type-blog.node--view-mode-featured {
    .node__content {
      margin-top: 1rem;
      margin-bottom: 1rem;
    }

    .field--name-field-image {
      border-right: 24px solid color(primary-3);
      box-sizing: border-box;
      background: $blue-gradient;
      height: 100%;

      img {
        max-blend-mode: multiply;
        filter: grayscale(100%);
      }

      @include breakpoint($m-break) {
        flex: 0 1 50%;
        max-width: 50%;
      }
    }
  } 
  

Context

It is convenient to keep endlessly nesting CSS selectors but too many levels makes code hard to read and makes selectors too specific and hard to override.

Consequences

Exceptions

Additional Resources

None

← See more ADRs