<aside> ✅ ScandiPWA follows a strict SCSS style guide for maintainability and consistency

</aside>

ScandiPWA follows the BEM methodology to write styles. In addition, there are some guidelines to help you write more maintainable stylesheets.

We strongly recommend you use Stylelint to check your code style. This article was written to help you understand the code style rules we enforce and write better code.

Avoid Non-BEM Selectors

<aside> 🚨 Avoid using selectors that aren't Block-Element-Modifier classes:

// component/CheckoutOrderSummary/CheckoutOrderSummary.style.scss (fragment)

.CheckoutOrderSummary {
    &-CartItemDescription {
        margin-top: 5px;

        p {
            font-size: 1.1rem;
            line-height: 1.5;
        }
    }
}

Potential issues:

<aside> ✅ Instead, it is recommended to assign a new BEM class to the element you want to style. Then, you can select it properly:

.CheckoutOrderSummary {
    &-CartItemDescription {
        margin-top: 5px;
    }

    &-CartItemDescriptionContent {
        font-size: 1.1rem;
        line-height: 1.5;
    }
}

</aside>

Minimize Nesting

In some cases, you might need to style one Block differently depending on whether it is a child of another block.

<aside> ⚠️ Avoid nesting selectors - don't select a block nested inside another block:

.Button {
    margin: 5px;
}

// this is discouraged
.ContactForm .Button {
    margin: 10px;
}

This is discouraged for several reasons:

<aside> ✅

</aside>

<aside> ➡️ In this case, another possible solution would be to use a Modifier:

.Button {
    margin: 5px;

    &_margin_large {
        margin: 10px;
    }
}

Now, the button could take a prop to specify the margin size and add it as a modifier to the Button Block. Then, the ContactForm would have to pass a value to this prop indicating that the button should have a larger margin.

</aside>

Prefer Mobile-First Styling

Mobile-first styling means that you are encouraged to start with mobile styles. When mobile styles are complete, you can use the @include desktop directive and other breakpoints to adjust the styling for larger viewports.

Avoid Magic Numbers

You should avoid hard-coding numbers when their reasoning is not obvious. Make values re-usable and understandable by using CSS variables.

<aside> 🚨 Avoid hard-coded values:

.NotificationList {
    position: fixed;
    top: 110px;
}

Issues:

<aside> ✅ Instead, use variables, and combine them with calc if necessary:

.NotificationList {
    position: fixed;
    top: calc(var(--header-height) + var(--breadcrumbs-height) + 20px);
}

Advantages:

Use Round Numbers

Unless you need to produce a pixel-perfect design, we recommend using values rounded to the nearest 5px, avoid unnecessary decimal digits, and consider how much precision browsers can actually display.

<aside> ✅

Instead, consider using a variable to customize your styling:

// Button.style.scss

:root {
    --button-margin: 5px;
}

.Button {
    // by default, a value of 5px will be inherited from :root
    margin: var(--button-margin);
}
// ContactForm.style.scss

.ContactForm {
    // we can set the variable lower in the hierarchy to override it
    --button-margin: 10px;
}

Now, each component is responsible for its own styles only, and the specificity is still low.

</aside>