CSS Responsive Layout with Grid and Media Queries
Start with a single readable column, then add columns with CSS Grid and @media as the viewport widens. This example uses the same navigation, main content, and sidebar throughout. It switches to two columns at 768px and three at 1,100px.
Start with a narrow viewport
The viewport is the area where the browser lays out the document. Put the following meta tag in the HTML <head> to set the layout viewport to the device width on mobile browsers. Leave zoom available to the reader. CSS Viewport Module · Viewport meta element
<meta name="viewport" content="width=device-width, initial-scale=1">
The page uses width: calc(100% - 32px) to leave room on both sides and max-width: 1100px to cap its width. Use min-width: 0 on panels, overflow-wrap: anywhere for long strings, and max-width: 100%; height: auto; for images. Fixed widths inside the panels can still overflow after a media query reduces the column count. CSS Grid Layout · Track Sizing
Add columns with media queries
A mobile-first layout starts with narrow-screen styles and adds progressively wider min-width rules. Starting with a desktop layout is also possible with max-width overrides. At 1,100px both min-width queries in this example match. The later declarations win for the same selectors and properties. A breakpoint is the width at which the layout changes. Choose it based on when your content becomes difficult to read. Media Queries · width
Build the complete responsive example
Save the code as responsive-layout.html and open it in a browser. No external libraries are needed. The navigation, main content, and sidebar stay in the same HTML order. The queries change their grid areas and track widths.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Responsive layout exercise</title>
<style>
*, *::before, *::after { box-sizing: border-box; }
body {
margin: 0;
background: #eef2f6;
color: #243449;
font-family: system-ui, sans-serif;
line-height: 1.7;
}
.page {
width: calc(100% - 32px);
max-width: 1100px;
margin: 24px auto;
}
.panel {
min-width: 0;
padding: 20px;
border: 1px solid #b8c6d4;
background: white;
overflow-wrap: anywhere;
}
header.panel, footer.panel { background: #e8f0f9; }
h1, h2 { margin-top: 0; }
img { max-width: 100%; height: auto; }
.layout {
display: grid;
grid-template-columns: minmax(0, 1fr);
grid-template-areas: "menu" "main" "side";
gap: 20px;
margin-block: 20px;
}
.menu { grid-area: menu; }
.content { grid-area: main; }
.sidebar { grid-area: side; background: #fff4d9; }
.menu ul {
display: flex;
flex-wrap: wrap;
gap: 12px;
padding: 0;
margin: 0;
list-style: none;
}
a { color: #174e94; }
@media (min-width: 768px) {
.layout {
grid-template-columns: minmax(0, 1fr) 240px;
grid-template-areas: "menu menu" "main side";
}
}
@media (min-width: 1100px) {
.layout {
grid-template-columns: 160px minmax(0, 1fr) 240px;
grid-template-areas: "menu main side";
}
.menu ul { display: block; }
.menu li + li { margin-top: 12px; }
}
</style>
</head>
<body>
<div class="page">
<header class="panel">
<h1>Responsive layout</h1>
<p>Change the viewport width to find a readable layout.</p>
</header>
<div class="layout">
<nav class="panel menu" aria-label="On this page">
<ul>
<li><a href="#article">Read the main content</a></li>
<li><a href="#notice">Read the sidebar</a></li>
</ul>
</nav>
<main class="panel content" id="article">
<h2>Main content</h2>
<p>On narrow screens, read the navigation, content, and sidebar from top to bottom.</p>
<p>On wider screens, the same HTML occupies different columns.</p>
</main>
<aside class="panel sidebar" id="notice">
<h2>Sidebar</h2>
<p>On a small screen, this information moves below the main content.</p>
</aside>
</div>
<footer class="panel">Resize the window to compare layouts.</footer>
</div>
</body>
</html>
Check breakpoints and content widths
| Viewport width | Layout | Track widths |
|---|---|---|
| < 768px | Navigation, content, and sidebar stack vertically | One track uses the page width |
| 768px ≤ w < 1100px | Navigation above the main content and sidebar | 240px sidebar, remaining space for content |
| ≥ 1100px | Navigation, content, and sidebar share one row | 160px navigation and 240px sidebar, remaining space for content |
At a viewport width of 1,100px, the page is 1100 - 32 = 1068px wide. Subtract the 160px navigation track, the 240px sidebar, and 40px for the two gaps to get 628px for the main track. This is why the three-column layout begins before the page reaches its 1,100px maximum. The calculation assumes a scrollbar takes up no horizontal space.
In developer tools, compare 390px, 767px, 768px, 1,099px, and 1,100px. Check the column count and horizontal overflow just before and after each breakpoint, and follow both menu links to their sections. For an experiment, change only 768px to 900px, verify that the second column starts at 900px, then restore the original value.
Troubleshoot a broken layout on narrow screens
Check the viewport meta tag first. Look for large width or min-width values inside the panels. Let long addresses wrap and give code blocks their own horizontal scrolling if line breaks should be preserved. If a matching query does not change the columns, inspect which declaration supplies grid-template-columns. Content and links should remain reachable with longer translations and browser zoom.




