Build real-world layouts with float, multicolumn, flexbox, CSS grid, and layering techniques.
Flexbox is a one-dimensional layout system. The container controls direction, alignment, and spacing of its children. Perfect for nav bars, card rows, and centering.
<!DOCTYPE html>
<html><head>
<style>
body { font-family: sans-serif; padding: 16px; display: flex; flex-direction: column; gap: 12px; }
.flex { display: flex; gap: 8px; border: 1px dashed #ccc; padding: 8px; border-radius: 6px; }
.item { background: #3b82f6; color: white; padding: 8px 14px; border-radius: 4px; font-size: 13px; }
.grow { flex: 1; }
.center { height: 72px; background: #f0f4ff; border-radius: 8px; display: flex; align-items: center; justify-content: center; }
.nav { justify-content: space-between; align-items: center; background: #1e293b; padding: 12px 16px; border-radius: 8px; }
.logo { color: white; font-weight: bold; }
.links { display: flex; gap: 16px; }
.links a { color: #94a3b8; text-decoration: none; font-size: 13px; }
</style>
</head><body>
<div class="flex">
<div class="item">A</div><div class="item">B</div><div class="item">C</div>
</div>
<div class="flex">
<div class="item grow">flex: 1</div>
<div class="item">fixed</div>
<div class="item grow">flex: 1</div>
</div>
<div class="center">Centered with align-items + justify-content</div>
<div class="flex nav">
<div class="logo">Logo</div>
<div class="links">
<a href="#">Home</a><a href="#">About</a><a href="#">Contact</a>
</div>
</div>
</body></html>Grid is two-dimensional — rows and columns at once. Use it for page layouts, card grids, and anything requiring precise 2D placement. grid-template-areas names zones by purpose.
<!DOCTYPE html>
<html><head>
<style>
body { font-family: sans-serif; padding: 16px; display: flex; flex-direction: column; gap: 16px; }
.item { background: #3b82f6; color: white; padding: 12px; border-radius: 4px; font-size: 13px; }
.grid-3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.layout {
display: grid;
grid-template-columns: 180px 1fr;
grid-template-rows: 40px 1fr 30px;
grid-template-areas:
"header header"
"sidebar main"
"footer footer";
gap: 8px; height: 180px;
}
.header { grid-area: header; background: #1e293b; }
.sidebar { grid-area: sidebar; background: #334155; }
.main { grid-area: main; background: #475569; }
.footer { grid-area: footer; background: #1e293b; }
</style>
</head><body>
<div class="grid-3">
<div class="item">1</div><div class="item">2</div><div class="item">3</div>
<div class="item">4</div><div class="item">5</div><div class="item">6</div>
</div>
<div class="layout">
<div class="item header">Header</div>
<div class="item sidebar">Sidebar</div>
<div class="item main">Main</div>
<div class="item footer">Footer</div>
</div>
</body></html>Layering puts elements visually on top of each other. Classic: position:absolute over a position:relative parent. Modern: grid with all children sharing grid-area: 1/1.
<!DOCTYPE html>
<html><head>
<style>
body { font-family: sans-serif; padding: 16px; display: flex; gap: 24px; flex-wrap: wrap; }
.card { position: relative; width: 200px; border-radius: 10px; overflow: hidden; }
.card img { display: block; width: 100%; }
.overlay { position: absolute; inset: 0; background: linear-gradient(to top, rgba(0,0,0,0.7), transparent); display: flex; align-items: flex-end; padding: 12px; }
.overlay span { color: white; font-size: 14px; font-weight: 600; }
.grid-layer { display: grid; width: 200px; border-radius: 10px; overflow: hidden; }
.grid-layer > * { grid-area: 1 / 1; }
.grid-layer img { width: 100%; display: block; }
.badge { place-self: start end; background: tomato; color: white; font-size: 11px; padding: 4px 8px; border-radius: 0 10px 0 8px; font-weight: bold; }
</style>
</head><body>
<div>
<p style="font-size:12px;color:#888;margin:0 0 6px">position: absolute overlay</p>
<div class="card">
<img src="https://picsum.photos/seed/layer1/400/250" />
<div class="overlay"><span>Caption overlay</span></div>
</div>
</div>
<div>
<p style="font-size:12px;color:#888;margin:0 0 6px">grid-area layering</p>
<div class="grid-layer">
<img src="https://picsum.photos/seed/layer2/400/250" />
<div class="badge">NEW</div>
</div>
</div>
</body></html>