Styling Components
ScandiPWA uses the BEM methodology and SCSS
Last updated
Was this helpful?
ScandiPWA uses the BEM methodology and SCSS
Last updated
Was this helpful?
To style components, ScandiPWA uses the methodology. BEM is based on assigning meaningful and unique class names to each HTML element we want to style while ensuring that we never need to use any CSS nesting in selectors.
We strive to follow this methodology in the core ScandiPWA theme, and we strongly encourage you to use it when overriding this theme as well.
Benefits of folowing our BEM guidelines:
The codebase is consistent
Styles are maintainable and can be re-used by composition with mixes
Styles can be overriden easily in child themes
All primary BEM classes are composed of these 2 parts:
<Block>
- in UpperCamelCase, the name of the component this element belongs to
-<Element>
(can be left out) - in UpperCamelCase, a meaningful name of this element (e.g. Container
, Button
, Divider
, Image
... whatever identifies the purpose of this element)
In addition to its primary <Block[-Element]>
class, a block or element may have any number of modifiers of the form:
<Block[-Element]>_<booleanModifier>
- for boolean modifiers such as isActive
, isVisible
, isBold
, etc. The presence of this modifier indicates that it is "true", and it's absence indicates that it is "false".
<Block[-Element]>_<modifierName>_<someValue>
- for boolean modifiers with values such as color_red
, type_primary
, size_thumbnail
BEM modifiers can be used to indicate state, available actions, or to distinguish between similar instances of the same element.
To add modifiers, pass an object with modifiers to the mods
prop. Boolean modifiers will be automatically detected and treated as such.
CSS variables are useful when:
You want to reuse the same value multiple times
You want to be able to override a value based on the context
You want to make it more clear what a value represents by naming it
CSS variables are always defined in :root
. That way, re-defining them anywhere else is an easy way to override them. Example:
Sometimes, you may want to allow other components to add additional style rules to a component. For example, the Image component needs to define some styles, but can't predict ahead of time the exact styling features that will be needed for Images in parent components.
The solution is to allow other components to add their own styles to the Image component. The BEM methodology allows this by "mixing" 2 BEM classes together. For example, in the CategoryDetails component, in addition to the regular Image
block, the CategoryDetails-Picture
class will be added. Since the element will now have both of these classes, the parent component can additionally style the element with new rules.
Formatting BEM classes manually with className
would get repetitive, so ScandiPWA uses to be able to use block
and elem
to specify the class. As an example, look at the render
method of ProductCard
:
We can take advantage of to reduce the repetitiveness of selecting BEM classes:
ScandiPWA defines certain breakpoints that enable you to write viewport width-specific styles. . To select a specific device, simply use the @include
directive: