


Let's take a look at the building blocks of BEM.īlock: a chunk of HTML to which you want to scope stylesĮnter fullscreen mode Exit fullscreen mode You can even roll your own system! But the most important thing to remember is to use a system, stick to that system, and make it work for you. Other class naming conventions exist alongside BEM to help you scope styles when writing HTML and CSS - such as OOCSS and SMACSS.
HIDDEN FOLKS SALMA CODE
What's more, it encourages you to make your HTML and CSS descriptive and self-documenting - helping identify the purpose and intended function of the CSS classes in the code itself. Let's take a look at how we can harness the power of a system like BEM.īEM stands for block, element, modifier, and it's a super-handy system to help you scope CSS style properties to blocks of HTML. If you change the style properties of a utility class used in multiple components, it could affect the layout of your whole application - sometimes with very undesirable results! This type of system discourages the use of utility classes reused across multiple components because this is where you can run into the problems described at the beginning of the post. Style rules should be purposefully verbose and self-documenting, without relying on inheritance or default browser behaviour. In order to scope your styles in pure CSS, the aim is to declare your CSS classes specifically and solely for individual HTML components. Nice!īut what if you're just starting out, and you want to focus on building out pure CSS in a systematic way without getting bogged down with CSS-in-JS? This means you don't need to worry about classes in one component affecting the styling of another component - even if you use the same class name. CSS-in-JS solutions such as Styled Components or CSS modules that ship with front end frameworks have largely solved this problem by scoping styles to your component templates as standard. To scope your CSS means to encapsulate style rules in a systematic way so that they apply to one particular chunk of HTML only. It was frustrating! So what can you do to stop this from happening? Have you ever worked on an application with one huge CSS file and found that when you changed a style rule in one place, something unexpected happened somewhere else? I had this problem a lot in my early days of front end development.
