CSS Layouts in 2026: Flexbox, Grid, and Beyond

By the BoltQuickTools team | April 9, 2026 | Updated November 8, 2026 | 8 min read

CSS layout used to rely on floats, clearfix hacks, and inline-block workarounds. Flexbox brought proper one-dimensional layout, Grid added two-dimensional control, and container queries and subgrid have since filled in gaps that remained.

This article covers when to use each one, with code examples and common layout patterns you can adapt.

A Brief History of CSS Layouts

The history explains why certain patterns exist.

Flexbox Fundamentals

Flexbox operates on a single axis at a time. You choose whether items flow horizontally (row) or vertically (column), and then you control how they are distributed along that axis and how they align on the perpendicular axis.

The Two Axes

The main axis is the direction items flow. For flex-direction: row (the default), the main axis is horizontal. For flex-direction: column, it is vertical. The cross axis is perpendicular to the main axis.

.container {
  display: flex;
  flex-direction: row;        /* main axis: horizontal */
  justify-content: center;    /* align along main axis */
  align-items: center;        /* align along cross axis */
  gap: 16px;                  /* space between items */
}

justify-content and align-items

justify-content distributes space along the main axis. The most useful values:

align-items does the same along the cross axis. The default is stretch, which is why flex children fill the container height by default.

flex-grow, flex-shrink, and flex-basis

These three properties control how items share available space:

.item {
  flex-grow: 1;     /* grow to fill available space (proportional) */
  flex-shrink: 1;   /* shrink if container is too small */
  flex-basis: 200px; /* starting size before growing/shrinking */

  /* Shorthand */
  flex: 1 1 200px;
}

A common pattern: flex: 1 on all children (shorthand for flex: 1 1 0%) makes them share space equally. Setting flex: none on one child keeps it at its natural size while siblings fill the rest.

When to Use Flexbox

Flexbox suits one-dimensional arrangements. Some common uses:

Navigation Bars

.navbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px 32px;
}

.nav-links {
  display: flex;
  gap: 24px;
  list-style: none;
}

Centering (the Classic Problem)

/* Center anything, both axes, in three lines */
.center-me {
  display: flex;
  justify-content: center;
  align-items: center;
}

Card Rows with Equal Height

.card-row {
  display: flex;
  gap: 16px;
}

.card {
  flex: 1;
  /* All cards stretch to the same height automatically */
}

You can experiment with all of these patterns using our CSS Layout Generator, which lets you adjust Flexbox and Grid properties visually and export the CSS.

CSS Grid Fundamentals

While Flexbox handles one direction at a time, CSS Grid lets you control both rows and columns simultaneously. This makes it the right choice for page-level layouts and complex component structures.

Template Areas

Grid template areas let you define layout structure using a visual ASCII-art syntax:

.page {
  display: grid;
  grid-template-areas:
    "header  header"
    "sidebar content"
    "footer  footer";
  grid-template-columns: 250px 1fr;
  grid-template-rows: auto 1fr auto;
  min-height: 100vh;
}

.header  { grid-area: header; }
.sidebar { grid-area: sidebar; }
.content { grid-area: content; }
.footer  { grid-area: footer; }

fr Units, repeat(), and minmax()

The fr unit represents a fraction of available space. Combined with repeat() and minmax(), you can create flexible grids with very little code:

/* Three equal columns */
grid-template-columns: repeat(3, 1fr);

/* Columns that are at least 250px, expand to fill */
grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));

/* First column fixed, rest flexible */
grid-template-columns: 200px repeat(3, 1fr);

auto-fill vs auto-fit

These two keywords behave identically when there are enough items to fill the row. The difference appears when there are fewer items than columns:

/* Empty tracks are kept; items stay narrow */
grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));

/* Empty tracks collapse; items widen to fill the row */
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));

When to Use Grid

Page Layouts

Any time you are laying out a full page with header, sidebar, content, and footer, Grid is the natural choice. The template areas syntax makes the structure immediately readable.

Dashboards

.dashboard {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-template-rows: auto;
  gap: 16px;
}

.widget-large {
  grid-column: span 2;
  grid-row: span 2;
}

.widget-wide {
  grid-column: span 2;
}

Image Galleries

.gallery {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 8px;
}

.gallery img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  border-radius: 8px;
}

Flexbox vs Grid: A Decision Framework

A simple way to decide:

Most layouts use both: Grid for the page structure and card gallery, Flexbox for the navigation bar and the inside of each card.

Container Queries: Component-Level Responsiveness

Media queries respond to the viewport size. Container queries respond to the size of a parent element. This distinction is critical for reusable components.

Consider a card component. With media queries, it changes layout based on the browser window width. But what if that card appears in a narrow sidebar on a wide screen? Media queries would give it the "wide" layout even though it is in a narrow space. Container queries solve this.

/* Define the container */
.card-wrapper {
  container-type: inline-size;
  container-name: card;
}

/* Respond to the container's width, not the viewport */
@container card (min-width: 400px) {
  .card {
    display: flex;
    flex-direction: row;
    gap: 16px;
  }
  .card-image {
    width: 40%;
  }
}

@container card (max-width: 399px) {
  .card {
    display: flex;
    flex-direction: column;
  }
  .card-image {
    width: 100%;
  }
}

Container size queries have been available in every major browser since early 2023 (Chrome 105, Safari 16, Firefox 110). They are a good default for components that appear in layouts of different widths.

Subgrid: Aligned Nested Grids

Before subgrid, nested grid items could not align with their parent grid's tracks. If you had a card grid where each card contained a title, description, and button, the titles might be different heights, pushing the descriptions out of alignment.

Subgrid solves this by letting a child grid inherit track definitions from its parent:

.card-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 16px;
}

.card {
  display: grid;
  grid-row: span 3;
  grid-template-rows: subgrid;  /* inherit row tracks from parent */
  gap: 8px;
}

.card-title { /* aligns across all cards */ }
.card-desc  { /* aligns across all cards */ }
.card-button { align-self: end; }

Subgrid reached all major browsers in September 2023, when Chrome 117 added it after Firefox (2019) and Safari 16 (2022). It is useful for forms, cards, and other components where sibling content should align.

Common Layout Patterns

Holy Grail Layout

Header, footer, main content, and two sidebars. This used to require complex float gymnastics. With Grid, it is straightforward:

.holy-grail {
  display: grid;
  grid-template-areas:
    "header header header"
    "left   main   right"
    "footer footer footer";
  grid-template-columns: 200px 1fr 200px;
  grid-template-rows: auto 1fr auto;
  min-height: 100vh;
}

Sticky Footer

A footer that stays at the bottom of the viewport when content is short, but gets pushed down when content is tall:

body {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

main {
  flex: 1;  /* takes all available space, pushing footer down */
}

footer {
  /* stays at the bottom naturally */
}

Responsive Card Grid

.cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
  gap: 24px;
  padding: 24px;
}

This creates a grid where cards are at least 280px wide and automatically wrap to the next row. No media queries needed.

Sidebar Layout

.with-sidebar {
  display: grid;
  grid-template-columns: minmax(200px, 25%) 1fr;
  gap: 24px;
}

@media (max-width: 768px) {
  .with-sidebar {
    grid-template-columns: 1fr;
  }
}

Choosing What to Learn First

Flexbox and Grid cover most layouts, so learn those first. Add container queries for components used in different contexts, and subgrid when sibling components need to line up. The CSS Layout Generator lets you try property values visually. For finishing touches, the Box Shadow Generator and Gradient Generator output CSS you can paste in.