CSS Fixed-Width Layout with Grid: Two and Three Columns

A fixed-width CSS layout can use width: 960px and box-sizing: border-box on the outer wrapper, with CSS Grid for the columns. In the example, the page is centered when there is room and overflows horizontally when the viewport is narrower than 960px.

Set the page’s outer width to 960px

The fixed value is the page width. The page stays in document flow and grows taller with its content. margin-inline: auto divides extra horizontal space between the two sides, and border-box includes padding and borders in the specified 960px. The value is measured in CSS pixels. CSS Box Sizing · box-sizing

Build two columns with a right sidebar

Save the code as fixed-layout.html and open it in a browser. The example requires no libraries. Start with about 1,100 CSS pixels of viewport width, then resize the window and inspect the box model of .page and the tracks of .layout in developer tools.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Fixed-width two-column layout</title>
  <style>
    *, *::before, *::after { box-sizing: border-box; }
    body {
      margin: 0;
      color: #243449;
      background: #eef2f6;
      font-family: system-ui, sans-serif;
      line-height: 1.7;
    }
    .page {
      width: 960px;
      margin-block: 32px;
      margin-inline: auto;
      padding: 20px;
      border: 1px solid #b8c6d4;
      background: white;
    }
    .panel {
      min-width: 0;
      padding: 20px;
      border: 1px solid #b8c6d4;
      overflow-wrap: anywhere;
    }
    header, footer { background: #e8f0f9; }
    h1, h2 { margin-top: 0; }
    .layout {
      display: grid;
      grid-template-columns: minmax(0, 1fr) 220px;
      grid-template-areas: "main side";
      gap: 20px;
      margin-block: 20px;
    }
    .content { grid-area: main; }
    .sidebar { grid-area: side; background: #fff4d9; }
  </style>
</head>
<body>
  <div class="page">
    <header class="panel">
      <h1>Fixed-width layout example</h1>
      <p>The page has an outer width of 960px.</p>
    </header>
    <div class="layout">
      <main class="panel content">
        <h2>Main content</h2>
        <p>The main content and sidebar occupy different columns.</p>
        <p>Widening the window keeps the page and sidebar at the same width. More content makes the middle row taller and moves the footer down.</p>
      </main>
      <aside class="panel sidebar">
        <h2>Sidebar</h2>
        <ul><li>Announcements</li><li>Categories</li><li>Contact</li></ul>
      </aside>
    </div>
    <footer class="panel">The footer sits below both columns.</footer>
  </div>
</body>
</html>

The header and footer span the inner page width. The main panel on the left and the sidebar on the right share a grid row. With default alignment, both panels stretch to the row’s height. Adding paragraphs to the main panel makes the row taller and pushes the footer down.

Why the main column is 678px wide

Subtract 40px of horizontal padding and 2px of borders from the 960px page to get 918px for the grid. Subtract the 220px sidebar and 20px gap and the main track gets 678px. The panels use border-box too, so their own padding and borders fit within their tracks.

minmax(0, 1fr) gives the main track the remaining space and sets its minimum to zero. In the example, min-width: 0 lets the panel shrink to its track size, and overflow-wrap: anywhere lets long words and URLs wrap when necessary. For oversized images, consider max-width: 100%; height: auto;. CSS Grid Layout · Track Sizing

Move the sidebar to the left

Change the track sizes and the area order together in .layout to place the sidebar on the left. The HTML order stays the same, so default reading and keyboard navigation can differ from the visible order. Review the HTML order if people should reach sidebar links first. Restore the original two declarations to undo the change. CSS Grid Layout · Reordering and Accessibility

.layout {
  grid-template-columns: 220px minmax(0, 1fr);
  grid-template-areas: "side main";
}

Add a third column

To put a sidebar on each side, replace the middle block that starts with <div class="layout"> in the complete example with the following HTML. Keep the header and footer.

<div class="layout">
  <aside class="panel sidebar-left">
    <h2>Categories</h2><p>This is the left sidebar.</p>
  </aside>
  <main class="panel content">
    <h2>Main content</h2><p>The center track uses the remaining space.</p>
  </main>
  <aside class="panel sidebar">
    <h2>Information</h2><p>This is the right sidebar.</p>
  </aside>
</div>

Then replace the original .layout, .content, and .sidebar rules with the following CSS. Subtract 160px on the left, 200px on the right, and 40px for two gaps from 918px. That leaves 518px for the center. To return to two columns, restore the original HTML and CSS.

.layout {
  display: grid;
  grid-template-columns: 160px minmax(0, 1fr) 200px;
  grid-template-areas: "left main right";
  gap: 20px;
  margin-block: 20px;
}
.sidebar-left { grid-area: left; background: #fff4d9; }
.content { grid-area: main; }
.sidebar { grid-area: right; background: #fff4d9; }

Handle narrow screens and older float layouts

The fixed wrapper does not shrink below 960px, even when the viewport is narrower. Add the following CSS at the end of the original two-column example to make the wrapper responsive up to 960px. At 720px or below, the sidebar stacks under the main content. Choose a breakpoint where the columns become hard to read on your own site. Media Queries · width

.page { max-width: 100%; }

@media (max-width: 720px) {
  .layout {
    grid-template-columns: minmax(0, 1fr);
    grid-template-areas:
      "main"
      "side";
  }
}

When moving an older layout from float and clear to Grid, remove the old layout rules that target the same panels and footer. Grid places the columns in a shared row with the footer after it. Hiding overflow with overflow-x: hidden can leave controls out of reach. Allow the tracks to shrink or stack them instead.