Many developers know only display: block and display: none.
Let's rediscover the full power of CSS layout.
display: flex
Before CSS, you need good HTML structure:
<header>…</header>
<nav>…</nav>
<main>…</main>
<article>…</article>
<section>…</section>
<footer>…</footer>
This makes code readable, accessible and structured.
CSS works better on semantic elements.
Grid is perfect for large page layouts
body {
display: grid;
grid-template-rows: auto 1fr auto;
grid-template-columns: 200px 1fr;
grid-template-areas:
"header header"
"sidebar main"
"footer footer";
min-height: 100vh;
}
header { grid-area: header; }
aside { grid-area: sidebar; }
main { grid-area: main; }
footer { grid-area: footer; }
Old tools, but still useful for specific cases
img {
float: left;
margin: 0 1rem 1rem 0;
}
.container {
display: flow-root;
}
No clearfix hack anymore. Clean and simple.
Flex is strong for smaller components
nav {
display: flex;
gap: 1rem;
}
Important: Don't use Flex for full page layouts.
Flex is for rows and columns, not for full grids.
Layout alone is not enough. We need ways to react to different sizes:
They work hand in hand with layout techniques
Classic and still needed - they react to screen width
.card-list {
display: grid;
grid-template-columns: 1fr;
}
@media (min-width: 768px) {
.card-list {
grid-template-columns: repeat(3, 1fr);
}
}
The real game changer - container size counts, not screen size
.card-list {
display: grid;
grid-template-columns: 1fr;
container-type: inline-size;
}
@container (min-width: 600px) {
.card-list {
grid-template-columns: repeat(3, 1fr);
}
}
Component adapts itself, regardless of container size!
"If Flex is your only tool, every layout looks like a Flex problem."
CSS will be strong and clear again!