Consistent CSS spacing is hard, but it does not have to be
Consistent CSS spacing is hard, but it does not have to be
By Niels Matthijs
7 min read
Two simple guidelines is all it takes to prevent rampant spacing issues from turning your expertly crafted project into an endless game of whack-a-mole.
- Authors
- Name
- Niels Matthijs
- linkedinNiels Matthijs
- twitter@Onderhond
- Website
- websiteBlog
I've been around for a while, and it has been rewarding to witness CSS develop into a powerful styling tool. Many of the complexities we used to face are now being solved by single CSS properties (tip of the hat to box-shadow), while newly added capabilities give us powers far beyond our wildest dreams. We can whip out sub grids, container queries, and page transitions without breaking a sweat, so we can finally focus on the finer design details.
Not all of our past problems have been laid to rest though, which is what prompted me to write this article. One of the oldest and most common sets of CSS properties are still causing major headaches, and our projects suffer the consequences. Spacing is hard, and the repercussions (i.e. increased maintenance complexity and time/money spent on bugs) are grim.
Before I dig into two simple guidelines that will alleviate the most pressing spacing concerns, let's find out why CSS spacing is such a minefield.
Whack-a-mole problems
When CSS spacing goes wrong, it creates a whack-a-mole situation. For those who don't know, whack-a-mole is an old arcade game in which moles randomly rise up from holes in the ground. Players need to hit them with a hammer, doing so will cause the mole to disappear, while another one pops up in a different location.
In development terms: squashing one bug will invariably introduce a new one. The client reports an issue and the following code dive will leave a slightly confused developer pondering. He'll make a change and all will appear dandy, but unknowingly he created a new bug. When the new bug is discovered another ticket is created, resulting in increased frustration of all parties involved. And that cycle repeats itself. Ad infinitum. In other words: whack-a-mole.
There is no single reason why spacing is so vulnerable to this type of slip-up, but there are some obvious causes why we keep tripping up:
- Developers don't possess the needed design DNA to spot spacing issues.
- Spacing is everywhere, so the opportunities to mess up are plentiful.
- It's easy to mend spacing problems with crappy code, and people looking at the resulting layout will be none the wiser.
- Beware of the cascade. A trivial rule that adds 12px of vertical padding to a plain paragraph can ruin an entire project.
- There are several properties that create spacing, and a handful of context-specific hacks on top of that. Picking the right solution isn't always obvious.
- The setup of most projects is inadequate to assess layout impact, meaning developers are working in the dark (and just hoping for the best).
CSS is free-form in the sense that there are always various ways to solve a problem. No two solutions are ever truly equal though, so let's run through the most common spacing options we have at our disposal.
Using the right tool for the job
There are various ways to create space, each with its own strengths and weaknesses, but it generally comes down to these three:
- padding: creates space within an element. Works on pretty much everything, but top/bottom paddings are ineffective on inline elements.
- gap: creates consistent spacing between the direct children of an element. It only works with grid and flexbox.
- margin: creates space around elements. Works on pretty much everything, but top/bottom margins are ineffective on inline elements.
I am a big fan of gap, but its one big limitation is that it can't be overruled for specific children. It is perfect for creating a fixed grid, yet it is rigid, and changing one specific space is hacky. This severely limits its use.
Paddings are useful for creating space between an element's edge and its contents. They come in handy when an element has a clear visual definition (think backgrounds, borders, box-shadow, ...), when you want to expand the interactive area of an element (hover, click, ...), or when you want to reserve space for absolutely positioned elements.
Margin is the most versatile, and the go-to property when you need flexible space between elements. You should only switch out margin to a better-fitting property when it adds extra value. We are of course talking CSS and there will be specific use cases that require craftier solutions. As a starting point though, margin is your best bet.
Two Simple guidelines
It is very difficult to recover from a spacing setup mid-crumble, and usually, the only way out is to rework the entire deal from the ground up. So how can we shield ourselves from these types of reputation-damaging solutions? Well, if you stick to the following guidelines, you are certain to end up with far fewer problems than before.
1. A space is defined by a single CSS value
DRY and/or the single-responsibility principle, I'm not quite sure what fits best here, but the guideline should be clear enough. If you need to create space, make sure it is defined and can be changed from a single property value in your CSS. That means:
- no collapsing or adjoining margins
- no adjoining margins and paddings
- no adjoining paddings
- no borders in the same color as the background (or other, equally ugly hacks)
- ...
Just none of it. When you see this happening, stop yourself and take an extra minute to figure out a better solution (and if you can't find one, flag to your PM that the CSS needs a more profound looking at).
2. The parent controls the layout of its children
This guideline may sound a bit more abstract, but it's not that difficult to grasp. As we construct pages from reusable building blocks, our CSS needs to mimic that setup. That's why a component should never come with built-in spacing. There is no way to predict in which context a component will be used, so we really shouldn't be making any assumptions on the spacing it needs.
It's the parent element that decides the position of a component. That's the only one who knows how its children should be arranged. Layout systems like grid and flexbox allow us to do exactly that (gap goes on the parent element), if you're using margins then the child selector is your friend. And if you're using utility class-based frameworks, the burden shifts to the templating logic, which should make sure no excess spacing exists (for example by removing spacing classes on the first/last child).
It's a simple enough guideline, but it's also an easy one to sin against, not in the least because component visualization tools (StoryBook and the like) give few indications of a component's factual boundaries. One thing that helped me tremendously was to include crop marks (the red crosses in the image below). They are relatively unobtrusive, yet a quick glance will reveal if unwanted spacing exists.
Conclusion
And that's all there is to it really. As long as you make sure that components don't bear excess spacing and that a space can be changed from a single value in the CSS, there's not a lot that can go wrong. Working with a robust component visualization tool (rather than working directly on templates) will provide extra protection against mishaps, but let's focus on prevention first.
With all the tools we have, it's a little embarrassing to have to face clients about something as basic and prevalent as spacing. There really is no excuse to have this be a problem in 2023, but all too often I come across projects that hide a convoluted layout mess, so there is clearly a need to address this. With a little care and focus, I'm sure we can make this a problem of the past.
Upcoming events
Coven of Wisdom - Herentals - Winter `24 edition
Worstelen jij en je team met automated testing en performance? Kom naar onze meetup waar ervaren sprekers hun inzichten en ervaringen delen over het bouwen van robuuste en efficiënte applicaties. Schrijf je in voor een avond vol kennis, heerlijk eten en een mix van creativiteit en technologie! 🚀 18:00 – 🚪 Deuren open 18:15 – 🍕 Food & drinks 19:00 – 📢 Talk 1 20:00 – 🍹 Kleine pauze 20:15 – 📢 Talk 2 21:00 – 🙋♀️ Drinks 22:00 – 🍻 Tot de volgende keer? Tijdens deze meetup gaan we dieper in op automated testing en performance. Onze sprekers delen heel wat praktische inzichten en ervaringen. Ze vertellen je hoe je effectieve geautomatiseerde tests kunt schrijven en onderhouden, en hoe je de prestaties van je applicatie kunt optimaliseren. Houd onze updates in de gaten voor meer informatie over de sprekers en hun specifieke onderwerpen. Over iO Wij zijn iO: een groeiend team van experts die end-to-end-diensten aanbieden voor communicatie en digitale transformatie. We denken groot en werken lokaal. Aan strategie, creatie, content, marketing en technologie. In nauwe samenwerking met onze klanten om hun merken te versterken, hun digitale systemen te verbeteren en hun toekomstbestendige groei veilig te stellen. We helpen klanten niet alleen hun zakelijke doelen te bereiken. Samen verkennen en benutten we de eindeloze mogelijkheden die markten in constante verandering bieden. De springplank voor die visie is talent. Onze campus is onze broedplaats voor innovatie, die een omgeving creëert die talent de ruimte en stimulans geeft die het nodig heeft om te ontkiemen, te ontwikkelen en te floreren. Want werken aan de infinite opportunities van morgen, dat doen we vandaag.
| Coven of Wisdom Herentals
Go to page for Coven of Wisdom - Herentals - Winter `24 editionMastering Event-Driven Design
PLEASE RSVP SO THAT WE KNOW HOW MUCH FOOD WE WILL NEED Are you and your team struggling with event-driven microservices? Join us for a meetup with Mehmet Akif Tütüncü, a senior software engineer, who has given multiple great talks so far and Allard Buijze founder of CTO and founder of AxonIQ, who built the fundaments of the Axon Framework. RSVP for an evening of learning, delicious food, and the fusion of creativity and tech! 🚀 18:00 – 🚪 Doors open to the public 18:15 – 🍕 Let’s eat 19:00 – 📢 Getting Your Axe On Event Sourcing with Axon Framework 20:00 – 🍹 Small break 20:15 – 📢 Event-Driven Microservices - Beyond the Fairy Tale 21:00 – 🙋♀️ drinks 22:00 – 🍻 See you next time? Details: Getting Your Axe On - Event Sourcing with Axon Framework In this presentation, we will explore the basics of event-driven architecture using Axon Framework. We'll start by explaining key concepts such as Event Sourcing and Command Query Responsibility Segregation (CQRS), and how they can improve the scalability and maintainability of modern applications. You will learn what Axon Framework is, how it simplifies implementing these patterns, and see hands-on examples of setting up a project with Axon Framework and Spring Boot. Whether you are new to these concepts or looking to understand them more, this session will provide practical insights and tools to help you build resilient and efficient applications. Event-Driven Microservices - Beyond the Fairy Tale Our applications need to be faster, better, bigger, smarter, and more enjoyable to meet our demanding end-users needs. In recent years, the way we build, run, and operate our software has changed significantly. We use scalable platforms to deploy and manage our applications. Instead of big monolithic deployment applications, we now deploy small, functionally consistent components as microservices. Problem. Solved. Right? Unfortunately, for most of us, microservices, and especially their event-driven variants, do not deliver on the beautiful, fairy-tale-like promises that surround them.In this session, Allard will share a different take on microservices. We will see that not much has changed in how we build software, which is why so many “microservices projects” fail nowadays. What lessons can we learn from concepts like DDD, CQRS, and Event Sourcing to help manage the complexity of our systems? He will also show how message-driven communication allows us to focus on finding the boundaries of functionally cohesive components, which we can evolve into microservices should the need arise.
| Coven of Wisdom - Utrecht
Go to page for Mastering Event-Driven DesignThe Leadership Meetup
PLEASE RSVP SO THAT WE KNOW HOW MUCH FOOD WE WILL NEED What distinguishes a software developer from a software team lead? As a team leader, you are responsible for people, their performance, and motivation. Your output is the output of your team. Whether you are a front-end or back-end developer, or any other discipline that wants to grow into the role of a tech lead, RSVP for an evening of learning, delicious food, and the fusion of leadership and tech! 🚀 18:00 – 🚪 Doors open to the public 18:15 – 🍕 Let’s eat 19:00 – 📢 First round of Talks 19:45 – 🍹 Small break 20:00 – 📢 Second round of Talks 20:45 – 🙋♀️ drinks 21:00 – 🍻 See you next time? First Round of Talks: Pixel Perfect and Perfectly Insane: About That Time My Brain Just Switched Off Remy Parzinski, Design System Lead at Logius Learn from Remy how you can care for yourself because we all need to. Second Round of Talks: Becoming a LeadDev at your client; How to Fail at Large (or How to Do Slightly Better) Arno Koehler Engineering Manager @ iO What are the things that will help you become a lead engineer? Building Team Culture (Tales of trust and positivity) Michel Blankenstein Engineering Manager @ iO & Head of Technology @ Zorggenoot How do you create a culture at your company or team? RSVP now to secure your spot, and let's explore the fascinating world of design systems together!
| Coven of Wisdom - Amsterdam
Go to page for The Leadership Meetup