惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
Tenable Blog
博客园_首页
Vercel News
Vercel News
WordPress大学
WordPress大学
美团技术团队
G
Google Developers Blog
大猫的无限游戏
大猫的无限游戏
小众软件
小众软件
Y
Y Combinator Blog
博客园 - 【当耐特】
量子位
酷 壳 – CoolShell
酷 壳 – CoolShell
The Cloudflare Blog
T
The Blog of Author Tim Ferriss
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google DeepMind News
Google DeepMind News
云风的 BLOG
云风的 BLOG
腾讯CDC
M
MIT News - Artificial intelligence
爱范儿
爱范儿
Recent Announcements
Recent Announcements
雷峰网
雷峰网
Last Week in AI
Last Week in AI
宝玉的分享
宝玉的分享
The Register - Security
The Register - Security
Jina AI
Jina AI
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Hugging Face - Blog
Hugging Face - Blog
P
Privacy & Cybersecurity Law Blog
Recorded Future
Recorded Future
Help Net Security
Help Net Security
N
News and Events Feed by Topic
博客园 - Franky
P
Proofpoint News Feed
L
LINUX DO - 热门话题
S
SegmentFault 最新的问题
The GitHub Blog
The GitHub Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
月光博客
月光博客
D
Docker
Google DeepMind News
Google DeepMind News
有赞技术团队
有赞技术团队
IT之家
IT之家
Security Latest
Security Latest
L
LangChain Blog
V
V2EX
阮一峰的网络日志
阮一峰的网络日志
J
Java Code Geeks

Max Stoiber's Essays

Save polish for where it matters How I get things done How I run gratitude circles How to present to executives Message me whenever How I manage my todos as a CEO How to run recurring virtual meetings efficiently How to have great taste How to be great at storytelling How we make brainstorming work How do you invent the future? Being unreasonably responsive has made my projects more successful Why I'm vigorous about giving feedback How to ship faster How to be better at making decisions How I tend to my digital garden David Cain: Do Quests, Not Goals Deliberate practice beats every other form of training, even via transfer learning How we foster deeper connections in our remote team Why I don't compliment people for their talent How can you slow down life? (which is perceptually half over by 23) 1:1s are for personal connection, not project updates Developer tools startups are playing on hard mode Developer tools are different than tools for any other profession You probably don't need GraphQL Why I Love Tailwind I am joining Gatsby Why I Write CSS in JavaScript Tech Choices I Regret at Spectrum Streaming Server-Side Rendering and Caching
Margin considered harmful
Max Stoiber · 2020-03-24 · via Max Stoiber's Essays

Mar 24, 2020·Updated Aug 11, 2024

77,558 views

We should ban margin from our components.

Hear me out.

The problems with using margin in a component

1. Margin breaks component encapsulation

A component should not affect anything outside it's own visual boundaries. Defining margin inside a component adds “invisible” space outside the component's visual boundaries.

2. Margin makes components less reusable

One specific margin around a component cannot be ideal for all instances of the component. Different layouts and contexts require different spacing.

3. Margin conflicts with how designers think

Defining margin in a component defines it globally for all instances of the component. But designers think about space in relation and context: they define how far a component should be from another component in a specific layout/context, not globally.

Use spacers instead

Instead of margin I have started using spacers (either -components or -class names), which move the responsibility of managing space to the parent-level.

For example, the Braid design system popularized the idea of a Stack component:

<Stack space={3}>
<Item />
<Item />
<Item />
</Stack>

TailwindCSS popularized space between and gap (for flexbox and grid) utility class names:

<div className="space-x-3">
<Item />
<Item />
<Item />
</div>

<div className="gap-3 flex">
<Item />
<Item />
<Item />
</div>

Under the hood, both spacer components and class names still use the margin CSS properties with a lobotomized owl selector or the gap property (for flexbox and grid layouts):

.space-x-3 > * + * {
margin-top: 0.75rem;
}

.gap-3 {
gap: 0.75rem;
}

The benefits of spacers

Moving the responsibility of managing space to the parent-level has benefits that aren't obvious.

1. Spacers force us to build more encapsulated, and thus reusable, components

Using spacers means none of your components define their own margins. Instead, you exclusively define how far a component should be from another component in a specific instance with a spacer.

2. Spacers bring us closer to how designers think

Spacers force you to define space in relation and context instead of globally for all instances of a component at once. Who else thinks about space in relation and context? Designers.

3. Bonus: Spacers keep our spacing consistent

Spacers can restrict spacing values to steps on a scale. (e.g., space-x-3 -> margin-left: 0.75rem) That way, all spacing automatically aligns to a grid and is consistent.

So: Use spacers. Ban margin.


I am not the first one to realize this. Thanks to @markdalgleish, @michaeltaranto and @mattcompiles at Seek, as well as my good friend @okonetchnikov for paving the way and prompting me to think about it.