SCSS Code Style
ScandiPWA follows a strict SCSS style guide for maintainability and consistency
Last updated
Was this helpful?
ScandiPWA follows a strict SCSS style guide for maintainability and consistency
Last updated
Was this helpful?
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 using selectors that aren't Block-Element-Modifier classes:
Potential issues:
As code becomes more complex, it can become unclear what role the p
plays in the app. A well-named BEM class would describe its purpose better
Unnecessary coupling: JavaScript code is now forced to use a <p>
element. If you want to rewrite the component to use a <div>
instead, you would have to update the styles.
Unnecessary : Having an additional selector element will increase the specificity and make the styles harder to .
Instead, it is recommended to assign a new BEM class to the element you want to style. Then, you can select it properly:
In some cases, you might need to style one Block differently depending on whether it is a child of another block.
Avoid nesting selectors - don't select a block nested inside another block:
This is discouraged for several reasons:
Button
styles now contain selectors from another component. This causes coupling, which will result in issues if the .ContactForm
block is renamed
The selector has higher specificity, making it harder to override.
Instead, consider using a variable to customize your styling:
Now, each component is responsible for its own styles only, and the specificity is still low.
You should avoid hard-coding numbers when their reasoning is not obvious. Make values re-usable and understandable by using CSS variables.
Avoid hard-coded values:
Issues:
Unclear where the value 110 is coming from
The layout can break if some values are changed
Instead, use variables, and combine them with calc
if necessary:
Advantages:
Intentions are more clear
Variable values can be safely changed to adjust the layout
Each value is re-usable.
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.
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 to adjust the styling for larger viewports.