The Day CSS Layout Stopped Feeling Like Magic
Flexbox for one axis, Grid for two — and the mental model that unblocked me.
I fought CSS for years by guessing — tweak margin, refresh, swear, repeat. The turning point wasn’t a new framework; it was accepting that layout has two jobs: line things up in a row/column, or place them in two dimensions.
Flexbox: one dimension at a time
Row of chips? Navbar? Footer with spaced items? Flex.
Mental model: “I have a main axis and a cross axis; children can grow/shrink with rules.”
.toolbar {
display: flex;
align-items: center;
gap: 0.75rem;
}
Grid: when you mean “this box goes there”
Dashboards, card galleries, “sidebar + content” without float hacks — Grid.
.layout {
display: grid;
grid-template-columns: 240px 1fr;
min-height: 100vh;
}
What I stopped doing
Absolutely positioning everything “just for this one screen.” It always came back for revenge on mobile.
Debugging trick I still use
outline: 1px solid hotpink on suspicious containers. Never border — it changes size and lies to you.
gap saved my flex sanity
Before universal gap, I used negative margins and hacky selectors to space chips. Now display: flex; gap: 0.5rem is the default. If you’re on a codebase that still “can’t use gap,” check which browsers you actually support — you might be carrying 2016 constraints.
Grid areas for dashboards
When I have a fixed dashboard skeleton, naming areas beats counting columns in my head:
.dashboard {
display: grid;
grid-template-areas:
"nav nav"
"side main";
grid-template-columns: 220px 1fr;
}
.sidebar { grid-area: side; }
Readable diffs when you move a panel — that’s worth the extra syntax.
When I still reach for position: sticky
Table headers and section navs. Sticky isn’t evil; abusing sticky for whole-page layout is. Pair it with a sensible scroll parent or you’ll chase “why isn’t it sticking” for an hour.
CSS isn’t random; it’s ruthlessly logical once you name which problem you’re solving. Flex or Grid — pick the dimension count and stop arm-wrestling float.