More than half of all web visits come from phones, and the same page might be viewed on a 360-pixel phone, a tablet, a laptop and a 4K monitor. Responsive web design means building one page that adapts to all of them: text stays readable without zooming, images shrink to fit, nothing scrolls sideways, and buttons are big enough to tap.
Most of the adapting is done with CSS, which the CSS course covers in lessons on media queries and responsive layouts. But HTML lays the foundation. Without the viewport meta tag, phones pretend to be desktop screens and shrink everything. Without width and height attributes and srcset, images either overflow or download far too large. Without the right input types, phone users get the wrong keyboard.
In this lesson you'll set up the viewport, make images, videos and tables fit any width, build forms and navigation that work well on touch screens, and test everything with your browser's device mode. Google also uses mobile-first indexing, so a page that works well on phones is the version that counts for search.
The viewport meta tag
<meta name="viewport" content="width=device-width, initial-scale=1">- Every responsive page needs this line in the
<head>. width=device-widthtells the browser the page should be as wide as the screen, rather than a pretend 980-pixel desktop.initial-scale=1starts at normal zoom.- Never add
user-scalable=noormaximum-scale=1: blocking zoom makes pages hard to read for people with low vision.
Fluid media and tables
- Images:
img { max-width: 100%; height: auto; }shrinks images to fit their container while keeping their shape. Addsrcsetandsizesso phones download smaller files, as shown in the responsive images lesson. - Video and embeds:
width: 100%plusaspect-ratio: 16 / 9. - Tables: wide tables can't shrink below their content. Wrap them in a container with
overflow-x: autoso the table scrolls inside its box instead of widening the whole page, or reorganise them into cards on small screens. - Long words and URLs:
overflow-wrap: anywherestops them breaking the layout.
HTML Responsive Design Syntax
Responsive essentials
<meta name="viewport" content="width=device-width, initial-scale=1">
<img src="photo-800.jpg" srcset="photo-400.jpg 400w, photo-800.jpg 800w" sizes="(max-width: 600px) 100vw, 600px" alt="..." width="800" height="533">
<div class="table-wrap"><table>...</table></div>
<input type="tel" autocomplete="tel">
<input inputmode="numeric" autocomplete="one-time-code">img, video { max-width: 100%; height: auto; }
.table-wrap { overflow-x: auto; }HTML Responsive Design Summary
- Add
<meta name="viewport" content="width=device-width, initial-scale=1">to every page. - Make images and video fluid with
max-width: 100%andaspect-ratio. - Let wide tables scroll inside a wrapper instead of widening the page.
- Use the right input types, inputmode and autocomplete for mobile keyboards.
- Build mobile first and test in device mode and on real phones.
HTML Responsive Design Examples
Each example runs in the page. Edit the code and press Run to see what changes.
Fluid images and video
<style>
img, iframe { max-width: 100%; }
img { height: auto; display: block; border-radius: 8px; }
.video { width: 100%; aspect-ratio: 16 / 9; border: 0; }
</style>
<img src="https://picsum.photos/seed/rwd/900/500" alt="City skyline at dusk" width="900" height="500">
<iframe class="video" src="https://www.youtube-nocookie.com/embed/UB1O30fR-EE" title="Video: HTML basics" loading="lazy"></iframe>Line-by-line explanation
- Lines 2 to 3The 900-pixel image shrinks to fit any narrower screen, keeping its proportions.
- Line 4The video fills the width and stays 16:9.
- Line 6Width and height still help the browser reserve the right space.
A table that scrolls on small screens
<style>
.table-wrap { overflow-x: auto; border: 1px solid #e5e7eb; border-radius: 8px; }
table { border-collapse: collapse; min-width: 560px; width: 100%; }
th, td { padding: 8px 12px; border-bottom: 1px solid #e5e7eb; text-align: left; white-space: nowrap; }
</style>
<div class="table-wrap" tabindex="0" role="region" aria-label="Plan comparison">
<table>
<tr><th>Plan</th><th>Storage</th><th>Users</th><th>Support</th><th>Price</th></tr>
<tr><td>Starter</td><td>10 GB</td><td>1</td><td>Email</td><td>£4/month</td></tr>
<tr><td>Team</td><td>100 GB</td><td>10</td><td>Chat</td><td>£19/month</td></tr>
</table>
</div>Line-by-line explanation
- Line 2The wrapper scrolls sideways when the table is wider than the screen.
- Line 3The table keeps a sensible minimum width instead of squashing.
- Line 6
tabindex,roleandaria-labellet keyboard users focus and scroll the region.
A mobile-friendly form and menu
<style>
input, button { font-size: 16px; padding: 12px; width: 100%; box-sizing: border-box; margin-bottom: 10px; }
summary { padding: 12px; cursor: pointer; font-weight: bold; }
nav a { display: block; padding: 12px; }
</style>
<details>
<summary>Menu</summary>
<nav><a href="#">Home</a><a href="#">Shop</a><a href="#">Contact</a></nav>
</details>
<form>
<label for="ph">Phone</label>
<input id="ph" type="tel" autocomplete="tel">
<label for="pc">Postcode</label>
<input id="pc" autocomplete="postal-code">
<label for="code">Verification code</label>
<input id="code" inputmode="numeric" autocomplete="one-time-code">
<button type="submit">Continue</button>
</form>Line-by-line explanation
- Line 216px text stops iPhones zooming in on focus, and full-width fields are easy to tap.
- Line 4Large padding makes each menu link a comfortable tap target.
- Lines 6 to 9A collapsible menu with no JavaScript, using details and summary.
- Line 12
type="tel"shows a phone keypad. - Line 16
inputmode="numeric"shows a number keypad, andone-time-codelets phones fill in codes from text messages.
Common HTML Responsive Design Mistakes
No viewport meta tag
<head><title>Shop</title></head><head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Shop</title>
</head>Without it, phones show a tiny desktop-sized page.
Disabling zoom
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no"><meta name="viewport" content="width=device-width, initial-scale=1">People who need larger text rely on pinch-to-zoom.
Fixed widths
<img src="banner.jpg" alt="Sale" style="width: 1000px">
<table style="width: 1000px">...</table><img src="banner.jpg" alt="Sale" style="max-width: 100%; height: auto">
<div style="overflow-x: auto"><table>...</table></div>Fixed widths force sideways scrolling on phones.
HTML Responsive Design in Real Projects
Responsive design in practice
Every major site, from news publishers to banks, serves one responsive page rather than a separate mobile site. Search engines index the mobile version first, so problems on phones affect rankings. Design systems define breakpoints and touch target sizes, and teams check new pages at common widths such as 360, 768 and 1280 pixels before release. Codevra's own pages are tested at 360 pixels wide.
HTML Responsive Design Exercises
Answer each one and check it instantly. If you get stuck, open the hint before the answer.
Complete the viewport tag.
<meta name="viewport" content="width=, initial-scale=1">
What happens on a phone without the viewport meta tag?
How do you stop a wide table from widening the whole page?
Which input shows a phone number keypad?
This head will look tiny on phones and blocks zooming, and the image overflows. Fix the viewport so it allows zoom, and make the image fluid.
<meta name="viewport" content="width=980, user-scalable=no">
<img class="hero" src="https://picsum.photos/seed/fix/1200/400" alt="Banner" style="width: 1200px">- Viewport uses device-width
- Zoom isn't blocked
- Image is fluid
HTML Responsive Design Quiz
5 questions to check what you learned.
What is responsive web design?
- A separate site for phones
- A fast website
- A site that responds to clicks
- One page that adapts its layout to any screen size
Why shouldn't you use user-scalable=no?
- It stops people zooming, which many need to read comfortably
- It's slower
- It breaks images
- Search engines ban it
What does max-width: 100% do to an image?
- Makes it fill the screen
- Crops it
- Lets it shrink to fit its container but never grow beyond its natural size
- Hides it on mobile
What does mobile-first mean?
- Phones load the page first
- Write the small-screen layout as the default, then add styles for larger screens
- Only phones are supported
- Desktop styles come first
Which autocomplete value helps phones fill in codes from text messages?
- sms
- otp-code
- numeric
- one-time-code
HTML Responsive Design Challenge
A mobile-ready page start
Create:
- A viewport meta tag with
width=device-width, initial-scale=1 - An image with width and height attributes and inline
max-width: 100% - A
<div class="scroll">with inlineoverflow-x: autothat wraps a<table>
- Correct viewport tag
- Fluid image with dimensions
- Table inside a scrolling wrapper