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

推荐订阅源

Spread Privacy
Spread Privacy
V
Visual Studio Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Recorded Future
Recorded Future
云风的 BLOG
云风的 BLOG
Microsoft Azure Blog
Microsoft Azure Blog
I
InfoQ
Apple Machine Learning Research
Apple Machine Learning Research
MyScale Blog
MyScale Blog
M
MIT News - Artificial intelligence
WordPress大学
WordPress大学
Recent Announcements
Recent Announcements
V
V2EX
The GitHub Blog
The GitHub Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Jina AI
Jina AI
小众软件
小众软件
aimingoo的专栏
aimingoo的专栏
V
Vulnerabilities – Threatpost
C
Check Point Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
AI
AI
宝玉的分享
宝玉的分享
P
Proofpoint News Feed
量子位
Attack and Defense Labs
Attack and Defense Labs
H
Hackread – Cybersecurity News, Data Breaches, AI and More
P
Privacy International News Feed
Google DeepMind News
Google DeepMind News
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
C
CERT Recently Published Vulnerability Notes
腾讯CDC
Latest news
Latest news
Google DeepMind News
Google DeepMind News
The Register - Security
The Register - Security
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
G
GRAHAM CLULEY
Blog — PlanetScale
Blog — PlanetScale
博客园_首页
美团技术团队
The Cloudflare Blog
T
Tenable Blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
J
Java Code Geeks
SecWiki News
SecWiki News
Webroot Blog
Webroot Blog
N
News | PayPal Newsroom
博客园 - 叶小钗
博客园 - Franky

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.