Most visitors now browse on phones, but the same page must also work on tablets and wide desktop screens. With responsive web design you write a single set of HTML and CSS, and the layout rearranges itself for whatever device opens it. The main tools are the viewport meta tag, flexible units, media queries and layout systems such as Flexbox and Grid that adjust by themselves. This lesson builds a card layout that changes from one to three columns.
In this lesson you will learn
- Why the viewport meta tag is essential
- The mobile-first approach
- Writing media queries with
min-width - Choosing breakpoints
- Fluid layouts with
auto-fit,minmax(),clamp()and flexible images
Mobile First With Media Queries
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Responsive cards</title>
<style>
body { font-family: sans-serif; margin: 8px; }
.grid { display: grid; grid-template-columns: 1fr; gap: 8px; } /* mobile first: 1 column */
.card { background: #e3f2fd; padding: 12px; border-radius: 8px; }
.note::after { content: "Layout: 1 column (narrow)"; }
@media (min-width: 480px) { /* tablets */
.grid { grid-template-columns: repeat(2, 1fr); }
.note::after { content: "Layout: 2 columns (480px+)"; }
}
@media (min-width: 720px) { /* desktops */
.grid { grid-template-columns: repeat(3, 1fr); }
.note::after { content: "Layout: 3 columns (720px+)"; }
}
img { max-width: 100%; height: auto; }
</style>
</head>
<body>
<p class="note"></p>
<div class="grid">
<div class="card">HTML</div><div class="card">CSS</div><div class="card">JavaScript</div>
<div class="card">SQL</div><div class="card">Python</div><div class="card">Java</div>
</div>
</body>
</html>
Result
<meta name="viewport" content="width=device-width, initial-scale=1">tells phones to use their real screen width. Without it, mobile browsers pretend to be about 980px wide and shrink the whole page.- Mobile first: the default CSS is for small screens (one column), and
@media (min-width: ...)rules add changes for larger screens. Small devices download and apply the simplest styles. - At 480px and wider the grid becomes two columns; at 720px three. The note at the top uses
::afterto show which rule is active. Resize your browser window to watch the layout change; on a wide screen the result frame shows three columns. img { max-width: 100%; height: auto; }stops images from overflowing their containers on narrow screens.
Layouts That Adapt Without Breakpoints
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Fluid sizes</title>
<style>
body { font-family: sans-serif; margin: 8px; }
h1 { font-size: clamp(1.2rem, 4vw, 2.2rem); margin: 0 0 8px; } /* grows with the screen, within limits */
.wrap { max-width: 600px; margin: 0 auto; background: #f1f8e9; padding: 12px; border-radius: 8px; }
.cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: 8px; }
.cards div { background: #c5e1a5; padding: 10px; border-radius: 6px; }
@media (prefers-reduced-motion: reduce) { * { animation: none !important; } }
</style>
</head>
<body>
<div class="wrap">
<h1>Fluid heading with clamp()</h1>
<div class="cards"><div>One</div><div>Two</div><div>Three</div><div>Four</div></div>
</div>
</body>
</html>
Result
grid-template-columns: repeat(auto-fit, minmax(140px, 1fr))fits as many 140px-or-wider columns as possible and shares the remaining space, so the grid reflows without any media query. See CSS Grid.clamp(1.2rem, 4vw, 2.2rem)sets a font size that grows with the viewport width (vw) but never goes below or above the limits.max-width: 600px; margin: 0 autokeeps text readable on huge screens and centres it.- Media queries can test more than width:
prefers-reduced-motionrespects users who turn off animations, andprefers-color-schemedetects dark mode.
Common Breakpoints
| Query | Typical devices |
|---|---|
| Default (no query) | Phones in portrait |
@media (min-width: 600px) | Large phones, small tablets |
@media (min-width: 900px) | Tablets in landscape, small laptops |
@media (min-width: 1200px) | Desktops |
These numbers are guides, not rules. Add a breakpoint where your content starts to look wrong, not where a particular device happens to be. Test using your browser’s device toolbar (F12, then the phone icon).
Common Mistakes
- Missing the viewport meta tag, so media queries seem not to work on phones.
- Fixed widths such as
width: 960pxthat force sideways scrolling; usemax-widthand percentages. - Desktop-first CSS that overrides many rules for small screens, which is harder to maintain.
- Tiny tap targets; buttons and links on mobile should be at least about 44px tall.
Practice Exercises
Exercise 1. Hide a sidebar on screens narrower than 700px.
Show answer
.sidebar { display: none; }
@media (min-width: 700px) {
.sidebar { display: block; }
}
Mobile first: hidden by default, shown on wider screens.
Exercise 2. Make a navigation menu stack vertically on phones and sit in a row from 600px.
Show answer
nav ul { display: flex; flex-direction: column; gap: 8px; }
@media (min-width: 600px) {
nav ul { flex-direction: row; }
}
Frequently Asked Questions
What is the difference between responsive and adaptive design?
Responsive design uses one flexible layout that adjusts continuously. Adaptive design switches between several fixed layouts at set sizes. Most modern sites are responsive, often with a few breakpoints.
What are container queries?
@container queries, supported in all major browsers since 2023, let a component change its layout based on the width of its own container rather than the whole screen, which is ideal for reusable cards and widgets.
Summary
- Always include the viewport meta tag.
- Write mobile-first CSS and add
min-widthmedia queries. - Use
auto-fit/minmax(),clamp()andmax-widthfor fluid layouts. - Choose breakpoints based on your content and test on real sizes.
