If you work with CSS you may be familiar with the Lego analogy by Nicole Sullivan.
Follow this advice and, with time, you will end up with a collection of reusable components that you can compose to build complex UIs.
This concept is so simple that even my dad got it.
Reusability and composabilty are the key to success.
There are many articles and talks around the reusability topic, but I have rarely read about composability in CSS so I figured I’d write something about it.
Composability is a system design principle that deals with the inter-relationships of components.
A highly composable system provides recombinant components that can be selected and assembled in various combinations to satisfy specific user requirements.
Composability is about making the pieces play nicely together.
CSS Rules that affect composability compare to Lego studs: they let us assemble components in various combinations to satisfy specific user requirements.
Unlike Lego bricks though, UI components are not just glued together with studs. They are laid out and spaced in different ways to adapt to the context of the current view.
In this game margins play a big role.
Composability in CSS is ruled by
margin among other things.
Margins are often global and set to arbitrary standard values at the beginning of a project.
Take this code for example:
Here we have some global typography and default
margin-bottom for the
form component. A similar setup can be found in many popular or in-house CSS frameworks.
The code is well organized in separate files and it is reusable, but depending on the use-case it could be hard to compose the components without having to reset a couple of rules.
There are some things to keep in mind when building UI components or a pattern library:
- Context and default margins
- Margin direction
Context and default margins
When defining base styles or building a new component it is hard to know in advance where UI components are going to be used.
In the context of a header for example a default
margin-bottom on the form may have undesired effects and has to be reset.
I have seen and had to reset similar rules many times — design choices were made years ago by someone who didn’t or couldn’t predict my case of use, and now I have to deal with it :)
UI components instead should be self-contained. The component root should be free of any rule that may affect composability, specifically:
- layout rules like position (absolute or fixed), float, transform, etc
We can then use higher-order components or utilities to fit the component into a specific context.
The margin direction of child elements can also affect composability.
In 2012 Harry Roberts wrote about Single-direction margin declarations.
The basic premise is that you should try and define all your margins in one direction.
By choosing a single direction the effect of margins is more predictable and there are fewer side effects.
However child elements can still affect the surrounding components:
take an unordered list whose items have a
margin-bottom, any adjacent component/element or parent container will be affected by the
margin-bottom of the last item.
My suggestion is to always reset peripheral margins and to not set margins on the component root at all.
An improvement is to always use
margin-left and reset the
This is no different from resetting the
:last-child except for the fact that the
:first-child pseudo-class is supported by legacy browsers as well.
Finally we can avoid to reset rules by using the adjacent sibling selector