Full Width [alt+shift+f] Shortcuts [alt+shift+k]
Sign Up [alt+shift+s] Log In [alt+shift+l]
65

The Rules of Margin Collapse

from Josh Comeau's blog [alt+shift+b] in programming

“Margin collapse” has a dastardly reputation, one of the trickier parts of CSS. Fortunately, it gets a lot easier once you learn a few rules! In this tutorial, we take a deep dive into the governing principles, and learn how to use them to our advantage.
7th Dec 2020

Stay updated

Get a weekly newsletter with the top 5 articles worth reading every week.

More from Josh Comeau's blog

Getting Started with Anchor Positioning

For decades, one of the most notoriously-challenging problems on the web has been sticking one element to another element, for things like tooltips and nested menus. The CSSWG has decided to provide a first-class solution to this problem, and it’s pretty friggin’ cool! In this tutorial, I’ll share the most useful parts I’ve found from this modern CSS feature.

6th Jul 2026 • 1 votes
CSS vs. JavaScript

There are a bunch of JavaScript animation libraries out there, and you might have wondered whether there’s a performance cost compared to traditional CSS transitions and keyframe animations. In this blog post, we’ll compare the same animation across several different strategies and see the differences firsthand. There’s some interesting nuance here!

26th May 2026 • 1 votes
Squash and Stretch

Have you ever heard of Disney’s 12 Basic Principles of Animation? In this tutorial, we’ll explore how we can use the very first principle to create SVG micro-interactions that feel way more natural and believable. It’s one of those small things that has a big impact.

13th Apr 2026 • 2 votes
Sneaky Header Blocker Trick

There is a lil’ UI detail on this blog. Most people don’t even notice it, but the ones who do often reach out, asking how on earth it works. It feels like it defies the rules of CSS! In this blog post, I’ll break down the surprisingly-straightforward implementation so you can start using this trick yourself!

23rd Mar 2026 • 1 votes
Sprites on the Web

In game development, it’s common to use spritesheets for animation, but this technique isn’t as widely used on the web. Which is a shame, because we can do some pretty cool stuff with sprites! In this post, we’ll share the niche CSS function you can use to leverage this technique, and explore some of the potential use cases.

23rd Feb 2026 • 1 votes

More in programming

We solved SQLite's single-writer limitation

Multiple concurrent writers. Multiple processes. Same SQLite. No modifications.

an hour ago • 1 votes
SumatraPDF new features: April 4, 2026

New in the SumatraPDF pre-release builds: Bake PDF File New Bake PDF File command (CmdPdfBake) in the Ctrl + k command palette turns interactive form fields and annotations into static page content and saves the result as a new PDF, which then opens. Show PDF Outline New Show PDF Outline command (CmdShowPdfOutline) in the command palette shows the document’s outline (table of contents) as text in a window. Tab Groups menu Saving and restoring tab groups now has its own Tab Groups submenu (in the Favorites menu) with Save Tab Group and Restore Tab Group. Both are also in the tab right-click menu. There is also a window for managing saved groups (#4402). Screenshots include open menus Take Screenshot also captures open menus as separate windows you can pick. Menu bar improvements with tabs The custom menu bar shown when tabs are in the title bar works more like a normal menu bar, and toggling the menu bar is more reliable (#5442, #549). More: changes from April 4, 2026 and the full changelog.

12 hours ago • 1 votes
If your team is happy, are you doing a good job?

Brilliant jerks, ZIRP-era managers, and how psychological safety lost the plot. Part 3 of my conversation with Dr. Cat Hicks.

18 hours ago • 1 votes
WTF is context engineering? (with real examples)

Say you're deploying an AI assistant that processes online order returns. For it to work, it would need access to your store's purchase policy, item…

2 days ago • 1 votes
Hosting without hyperscalers
3 days ago • 1 votes
📚 BoredReading

You seem to be enjoying this.

Join free to unlock everything.

Create free account

Already have an account? Sign in