Easy Way to Write Maintainable CSS

CSS might be messy, CSS might be hard to maintain, CSS might be unscalable. But it doesn’t have to be! After all it’s just a tool and how it’s used depends mostly on a developer.

Do you know that feeling when styles for a navigation in a header broke some links in a footer? Or when you couldn’t change a font color, because other styles were more important? I do, and – if you are a frontend developer – you probably too. Most of the problems we have to deal with come from features of CSS – inheritance, globality and specificity. These mean that styles of a parent element will apply to every child in it, every style is available on every part of a page and some selectors have higher priority than others. These features aren’t bad, they are desired, but sometimes they are a source of huge pain for a developer.

CSS methodologies are solutions to these problems. They provide a way to encapsulate our styles and make CSS files easier to maintain and understand. They make front-end code easier to read and understand, easier to work with and easier to scale. CSS methodologies help other developers work with codebase and use the same language. But they aren’t some magical tools that will automatically fix our code, they are simply sets of rules to which developers should apply.

Overview of some CSS methodologies

Here are some of the most popular CSS methodologies. All of them encourage the reuse of CSS styles and creating modular components.


Object-Oriented CSS

Main principles:

  1. Separate structure and skin
    To define repeating visual features (like background and border styles) as separate “skins” that you can mix-and-match with your various objects to achieve a large amount of visual variety without much code.
  2. Separate container and content
    Rarely use location-dependent styles. An object should look the same no matter where you put it.

Example of OOCSS:


Scalable and Modular Architecture for CSS

At the very core of SMACSS is categorization. By categorizing CSS rules, we begin to see patterns and can define better practices around each of these patterns.

  1. Base Rules
    Base rules are applied to elements using elements selectors. They don’t use IDs or classes selectors. You can declare some default typography styles here.
  2. Layout Rules
    Layout styles are styles applied to major components that hold minor components (modules) inside them. This is a good place to keep your header or footer styles.
  3. Module Rules
    These are the smaller components of the page like navigations, forms, buttons etc. Modules are reusable blocks of code and can be moved without breaking the styling.
  4. State Rules
    They override other styles depending on a state like disabled buttons or active navigation items.
  5. Theme Rules
    Theme rules change the default styling. You may need them for example for a Christmas theme for your website.

Example of SMACCS:


Block Element Modifier

My personal favourite. Why? BEM is simple and quick to learn. You can show it to new developers and they will understand it in the matter of minutes. BEM doesn’t have many rules and is very flexible.

BEM methodology consists of three parts – Blocks, Elements and Modifiers. Styles use only class selectors, no tags or IDs. The structure is flat and you shouldn’t nest selectors.

It’s a separate entity that is meaningful on it’s own. Any DOM node can be a block.

A part of a block that has no standalone meaning and is semantically tied to its block. An element cannot exist outside a block and it’s not dependent on other elements.

They are flags on block or elements and can be used to change the styling. Nesting is allowed for modifiers if you want to change appearance of multiple elements within a block;

Here’s more complex example of BEM. It makes use of SASS’s selector concatenation.

It will compile to this code:

CSS Methodologies in Magento

Magento 1

Magento 1 doesn’t apply to any CSS methodology. Overwriting everything just to use it would be very time consuming and very unproductive. There is nothing we can do about it. For the existing code we stick to the old, ugly and unordered CSS. But if we write new features we can use some naming convention.

Magento 2

According to this poll BEM is the most used CSS methodology by developers. Some time ago Magento Team mentioned about using BEM on their Dev Blog. It’s probably just a wishful thinking and is very unlikely to happen, but… SNOW.DOG created the Alpaca theme for Magento which uses BEM methodology – it’s definitely something worth checking out – https://github.com/SnowdogApps/magento2-alpaca-theme

Unfortunately for the third-party modules we are dependent on good will of their developers. And of course we can’t expect from everyone to follow the same naming convention. But let’s just hope that more companies will see the advantages of using CSS methodologies.


Konrad Jaguszewski

Full Stack Developer

I'm a self-taught web developer. Since childhood I was amazed by computers. I love creating and learning new stuff, be that programming, cooking, playing guitar or doing crafts.