CSS Layout
3 main ways to build layouts: Flexbox, Grid, Position.
Flexbox — 1 dimension
Great for navbars, list items, card rows, and centering content.
css.row { display: flex; align-items: center; /* cross axis */ justify-content: space-between; /* main axis */ gap: 1rem; flex-wrap: wrap; } .item-grow { flex: 1; } /* take the remaining space */ .item-fixed { flex: 0 0 200px; } /* fixed 200px */
Grid — 2 dimensions
Great for dashboards, galleries, and complex page layouts.
css/* Auto responsive grid — no media query needed */ .grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); gap: 1rem; } /* Holy grail */ .app { display: grid; grid-template: "header header" auto "side main" 1fr "footer footer" auto / 240px 1fr; min-height: 100dvh; } .header { grid-area: header; } .side { grid-area: side; } .main { grid-area: main; } .footer { grid-area: footer; }
Position
css/* Tooltip / dropdown */ .parent { position: relative; } .tooltip { position: absolute; inset-inline-start: 0; top: calc(100% + 4px); } /* Sticky header */ .header { position: sticky; top: 0; z-index: 10; } /* Modal overlay */ .overlay { position: fixed; inset: 0; background: rgba(0,0,0,.5); }
When to use which?
Flex → 1 axis, content-driven (navbar, button group, list)
Grid → 2 axes, layout-driven (dashboard, gallery, full-page)
Position → element floating above the flow (modal, tooltip, badge, sticky)