☼
⌃
☰

CSS Layout

Table of Contents

How elements are arranged and positioned on a page. CSS has evolved from basic block/inline flow through floats to the modern Flexbox and Grid systems. The choice of layout method depends on whether the problem is one-dimensional (Flexbox) or two-dimensional (Grid).

Related: CSS Box Model CSS Core Concepts Cascading Style Sheets

Normal Flow

Default behaviour without layout overrides.

Type Behaviour Examples
Block Full width, starts on new line, stacks vertically div, p, h1, ul
Inline Width of content, flows horizontally span, a, strong, img
Inline-block Inline flow, but accepts width/height/margins Custom with display
CSS
.element { display: block; }
.element { display: inline; }
.element { display: inline-block; }

Positioning

CSS
position: static;    /* default — normal flow, top/left/etc. ignored */
position: relative;  /* stays in flow, offset from its original position */
position: absolute;  /* removed from flow, positioned relative to closest
                        non-static ancestor */
position: fixed;     /* relative to viewport — stays put on scroll */
position: sticky;    /* relative until scroll threshold, then fixed */
CSS
/* Typical use: tooltip or badge over a parent */
.parent  { position: relative; }
.tooltip { position: absolute; top: 0; right: 0; }

/* Sticky header */
header { position: sticky; top: 0; z-index: 100; }

z-index controls stacking order — higher value = on top. Only applies to positioned elements (not static).

Flexbox — One-Dimensional

Best for: navigation bars, card rows, form alignment, any single-axis distribution of items.

CSS
/* Container */
.container {
  display: flex;
  flex-direction: row;           /* row | column | row-reverse | column-reverse */
  justify-content: space-between; /* main axis: flex-start | center | space-between | space-around | space-evenly */
  align-items: center;           /* cross axis: flex-start | flex-end | center | stretch | baseline */
  flex-wrap: wrap;               /* nowrap | wrap */
  gap: 16px;                     /* space between items */
}

/* Items */
.item {
  flex: 1;           /* shorthand for flex-grow flex-shrink flex-basis */
  flex-grow: 1;      /* take up remaining space */
  flex-shrink: 0;    /* don't shrink below flex-basis */
  flex-basis: 200px; /* starting size */
  order: 2;          /* visual order override */
  align-self: flex-start; /* override align-items for this item */
}

Grid — Two-Dimensional

Best for: page-level layout, dashboards, any arrangement with both rows and columns.

CSS
/* Container */
.grid {
  display: grid;
  grid-template-columns: 1fr 2fr 1fr;         /* 3 columns */
  grid-template-columns: repeat(3, 1fr);       /* equivalent */
  grid-template-columns: 200px auto 1fr;       /* mixed units */
  grid-template-rows: auto 1fr auto;           /* header, main, footer */
  gap: 24px 16px;                              /* row-gap column-gap */
}

/* Placing items */
.header  { grid-column: 1 / -1; }   /* span all columns */
.sidebar { grid-column: 1; grid-row: 2; }
.main    { grid-column: 2 / 4; grid-row: 2; }

/* Named areas */
.grid {
  grid-template-areas:
    "header header header"
    "sidebar main   main"
    "footer footer footer";
}
.header  { grid-area: header; }
.sidebar { grid-area: sidebar; }

Floats

Legacy — historically used for multi-column layouts. Superseded by Flexbox and Grid. Retain for text-wrapping around images only.

CSS
img { float: left; margin: 0 16px 16px 0; }
.clearfix::after { content: ""; display: block; clear: both; }

Choosing a Layout Method

Situation Use
Single row or column of items Flexbox
Full page structure with rows and columns Grid
Fine-tuned element placement, overlays Positioning
Text wrapping around an image Float
Default document reading order Normal flow

Tips

  • Flexbox and Grid can nest — Grid for the overall page structure, Flexbox for individual components within grid cells
  • gap works in both Flexbox and Grid — prefer it over margins between items for cleaner spacing
  • fr units in Grid divide the remaining space after fixed widths are allocated — 1fr 2fr gives one third and two thirds
  • align-items in Flexbox aligns on the cross axis; in Grid it aligns content inside cells — same property name, slightly different meaning