What Happens When You Type a URL and Press Enter
DNS lookups, TCP and TLS handshakes, HTTP requests, servers, and the browser's rendering pipeline. The whole journey of a web page, explained so it finally clicks.
Read →Flexbox is for one direction, Grid is for two. That rule covers most cases, but not all. Practical layouts, the properties you'll use most, and how to combine both without fighting CSS.
Modern CSS layout comes down to two tools: Flexbox and Grid. Both are fully supported in every modern browser, both are powerful, and beginners often use one where the other would be half the code. Here's how to choose.
A second way to think about it: Flexbox works content-out (items decide their size, and the container distributes space), while Grid works layout-in (you define the structure, and items slot into it).
Use Flexbox for navigation bars, button groups, centering, card internals and any row of items that should wrap or share space.
.navbar {
display: flex;
align-items: center; /* vertical alignment in a row */
justify-content: space-between; /* spread items along the row */
gap: 1rem;
}
The properties you'll use constantly:
| Property | What it does |
|---|---|
flex-direction |
row (default) or column |
justify-content |
Alignment along the main axis |
align-items |
Alignment along the cross axis |
gap |
Space between items |
flex-wrap |
Allow items to wrap onto new lines |
flex: 1 |
"Take up the remaining space" |
The famous centering problem is now two lines:
.center {
display: flex;
place-items: center; /* or justify-content + align-items: center */
}
A very common pattern, "sidebar fixed, content flexible":
.layout { display: flex; }
.sidebar { flex: 0 0 260px; } /* don't grow, don't shrink, 260px */
.content { flex: 1; min-width: 0; } /* fill the rest */
That min-width: 0 prevents long content (like code blocks) from overflowing. It's one of the most useful lines in CSS.
Use Grid when you care about rows and columns lining up: page structures, dashboards, card galleries, forms with labels and inputs aligned.
.page {
display: grid;
grid-template-columns: 260px 1fr;
grid-template-rows: auto 1fr auto;
grid-template-areas:
"header header"
"sidebar main"
"footer footer";
min-height: 100vh;
}
.header { grid-area: header; }
.sidebar { grid-area: sidebar; }
.main { grid-area: main; }
.footer { grid-area: footer; }
Named areas make the layout readable at a glance, and rearranging it for mobile is just a new grid-template-areas inside a media query.
.cards {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
gap: 1.5rem;
}
Cards are at least 260px wide and as many fit per row as the screen allows. It goes from one column on phones to four or more on wide screens with zero breakpoints. Few lines of CSS are this useful. (auto-fit is a close cousin: it stretches items when there are fewer than fit a row.)
| Situation | Use |
|---|---|
| Navbar, toolbar, button row | Flexbox |
| Centering one thing | Either (Flexbox or place-items on Grid) |
| Card internals (icon, text, button) | Flexbox |
| Gallery of equal cards | Grid |
| Whole page layout | Grid |
| Form with aligned labels and inputs | Grid |
| Items of unknown count that should wrap | Flexbox with flex-wrap, or Grid with auto-fill |
| Aligning content across separate cards | Grid with subgrid |
You don't pick one for a whole project. A typical page uses Grid for the overall layout and the card gallery, and Flexbox inside each card and in the header. Nesting them is normal.
Subgrid is worth knowing too: a nested grid can inherit its parent's tracks, so titles and buttons line up across a row of cards even when their content lengths differ. It's supported in all modern browsers now.
gap in both Flexbox and Grid; it's cleaner and avoids extra space on the edges.min-height when you need a floor.min-width: 0 to flex or grid children that contain long text or code.Both feed directly into good responsive design: mobile-first responsive web design.
Use Flexbox for one-dimensional layouts like rows of items, and Grid for two-dimensional layouts like page structures and galleries. Most real projects use both together.
Neither is better. They solve different problems. Grid excels at structured, aligned layouts; Flexbox excels at distributing space among items in a line.
Yes, and you should. A common pattern is Grid for the page and gallery layout, with Flexbox inside individual components.
DNS lookups, TCP and TLS handshakes, HTTP requests, servers, and the browser's rendering pipeline. The whole journey of a web page, explained so it finally clicks.
Read →There are dozens of HTTP status codes, but about fifteen cover nearly everything. What each one means, when to return it, and the mix-ups (401 vs 403, 301 vs 302) that confuse everyone.
Read →A, AAAA, CNAME, MX, TXT and NS records in plain English, why DNS changes seem slow, and the troubleshooting commands that tell you what is really going on.
Read →