HTML Responsive
Responsive HTML is markup written so a page works well on any screen, from a small phone to a wide monitor. The layout adapts with CSS, but several decisions in the HTML itself make that possible: the viewport meta tag, images that can scale or swap, and content structured so it can stack in a single column. Without them, CSS media queries alone cannot rescue a page on mobile.
The viewport meta tag comes first
Phones render pages on a virtual canvas, often around 980 pixels wide, and then shrink it to fit the screen. Text becomes tiny and users have to pinch and zoom. This line in the <head> tells the browser to use the device's real width instead:
<meta name="viewport" content="width=device-width, initial-scale=1">
Do not add maximum-scale=1 or user-scalable=no. Blocking zoom makes the page harder to use for people with low vision.
Images that fit any width
An image wider than its container will push the page sideways and cause horizontal scrolling. The basic fix is CSS, img { max-width: 100%; height: auto; }, which lets images shrink but never grow past their natural size. HTML offers more: you can let the browser pick the best file for the screen.
<!-- Same image at different sizes; the browser picks one -->
<img
src="photo-800.jpg"
srcset="photo-400.jpg 400w, photo-800.jpg 800w, photo-1600.jpg 1600w"
sizes="(max-width: 600px) 100vw, 50vw"
alt="Students working at laptops in a classroom"
width="800" height="533"
loading="lazy">
<!-- Different crops for different screens -->
<picture>
<source media="(min-width: 900px)" srcset="banner-wide.jpg">
<img src="banner-square.jpg" alt="Laptop on a desk with code on screen">
</picture>
srcset lists available files and their real widths. sizes tells the browser how wide the image will display: full viewport width on small screens, half the width elsewhere. The browser then downloads the smallest file that looks sharp, saving data on phones. Use <picture> when you need a different crop, not just a different size, such as a tall banner on mobile.
The width and height attributes let the browser reserve the right amount of space before the image loads, which stops content jumping around.
Structure that can reflow
Write the HTML in the order it should be read on a phone, since a single column follows source order. Group content into semantic sections such as <header>, <nav>, <main>, <article>, <aside> and <footer>. CSS can then place them side by side on wide screens with flexbox, grid and media queries, while the same markup stacks naturally on narrow ones.
Content that refuses to shrink
A few kinds of content commonly break mobile layouts:
- Wide tables. Wrap them in a container with
overflow-x: autoso the table scrolls inside its box instead of the whole page scrolling. - Embedded videos and iframes with fixed
widthattributes. Give themwidth: 100%and anaspect-ratioin CSS. - Long words and URLs that cannot wrap.
overflow-wrap: anywherelets them break. - Fixed pixel widths on containers, such as
width: 1200px. Prefermax-widthwith percentage or flexible widths.
The mistake most people make
Testing only by dragging a desktop browser window narrower. That helps, but it misses touch targets that are too small, hover-only menus that phones cannot open, and the virtual keyboard covering form fields. Use the device toolbar in developer tools, which simulates phone widths and touch, and check the page on a real phone when you can. Look especially for a horizontal scrollbar; one oversized element is enough to cause it.
Checklist for responsive markup
Add the viewport tag, give images srcset, sizes and dimensions, use <picture> for different crops, order content for a single column, and contain wide tables and embeds. With that HTML in place, CSS media queries can do the rest of the layout work.
Ready to master real-world web development?
Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.