Responsive Web Design: A Practical Guide to MDN’s Core Concepts
People visit websites on phones, tablets, laptops, and large desktop screens. Responsive web design helps a site adapt to those different devices and display sizes. Instead of building a separate version of a page for every screen, developers use flexible layouts and CSS features to create one experience that adjusts to the available space.
MDN Web Docs is a useful resource for learning the web technologies behind responsive design, including CSS media queries, flexible layouts, and responsive images. This guide introduces those ideas and shows how they work together.
What Is Responsive Web Design?
Responsive web design is an approach to building websites that respond to the conditions in which they are viewed. Screen width is an important consideration, but it is not the only one. A responsive page should also remain usable when someone zooms in, changes text size, uses a touch screen, or navigates with a keyboard.
A well-designed responsive site generally has three qualities:
- Flexible layouts: Content can expand or contract to fit the available space.
- Adaptable media: Images and other media fit within their containers without distorting the layout.
- Media queries: CSS rules can adjust the design when the viewport or user’s preferences meet certain conditions.
Start with the Viewport
On mobile devices, browsers may use a virtual layout viewport that is wider than the physical screen. The viewport meta element tells the browser to use the device’s width as the page width, helping CSS layouts behave as expected.
<meta name="viewport" content="width=device-width, initial-scale=1">
Place this element in the document’s <head>. Without it, a mobile browser may render the page at a desktop-like width and scale it down, making text and controls difficult to use.
Build Flexible Layouts
Responsive layouts rely on elements that can adapt instead of being locked to fixed dimensions. CSS Grid and Flexbox are both designed to help arrange content across different amounts of space.
For example, CSS Grid can create columns that fit as many cards as the container allows:
.card-list {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(min(100%, 16rem), 1fr));
gap: 1rem;
}
This pattern lets cards form multiple columns when there is room and move into fewer columns as the container narrows. Flexible units such as percentages, fr, and rem can also help a layout adapt more smoothly than fixed pixel widths alone.
Use Media Queries Thoughtfully
CSS media queries let you apply styles conditionally. A common use is adjusting a layout when the viewport reaches a particular width:
.page-layout {
display: grid;
grid-template-columns: 1fr;
gap: 1.5rem;
}
@media (min-width: 48rem) {
.page-layout {
grid-template-columns: 2fr 1fr;
}
}
In this example, the page uses one column by default. When the viewport is wide enough, it changes to a two-column layout. The breakpoint should be chosen based on when the content needs a layout change, rather than on a specific device model.
Media queries can also respond to user preferences. For example, prefers-reduced-motion can be used to limit animations for people who have requested less motion in their operating-system settings.
Make Images and Media Adapt
Images can cause horizontal scrolling if they are wider than their containers. A simple rule helps keep them within the available space:
img,
video {
max-width: 100%;
height: auto;
}
For different image sizes or resolutions, the HTML <picture> element and the srcset and sizes attributes can offer the browser a choice of image sources. This can improve loading efficiency while providing suitable images for different displays.
Keep Text and Controls Usable
A responsive layout should not merely fit on a small screen; it should remain comfortable to use. Avoid making text so small that readers need to zoom, and make buttons and links easy to tap. Check that navigation works with a keyboard and that visible focus indicators are not removed without a suitable replacement.
Also test the page at increased zoom levels. A layout that works at its initial size may become difficult to navigate when text is enlarged or the viewport effectively becomes narrower.
Test Across Viewports
Browser developer tools can simulate different viewport sizes, but testing should not stop at a few preset device dimensions. Resize the browser gradually and look for problems such as:
- Horizontal scrolling that is not intentional
- Overlapping or clipped content
- Navigation that becomes difficult to operate
- Text lines that are too long or too cramped
- Images that load slowly or appear distorted
Testing on real devices can reveal touch, performance, and readability issues that a desktop simulation may not show. Automated checks can help, but they should complement—not replace—manual review.
Learn More with MDN Web Docs
MDN Web Docs offers reference material and guides for the technologies used in responsive web design. Useful topics include CSS media queries, CSS Grid, Flexbox, responsive images, and the viewport meta element. These resources explain the underlying features, while practical testing helps you decide how to apply them to a particular site.
The strongest responsive designs begin with clear content and a simple, flexible layout. Add breakpoints only when the design needs them, make media adapt to its container, and test the result with different screen sizes and user preferences. With these fundamentals, a website can provide a consistent and accessible experience across a wide range of devices.
5 Essential Responsive Web Design Tips from MDN
- Use a fluid layout that adapts to different screen sizes.
- Add the viewport meta tag for proper mobile scaling.
- Use CSS media queries to adjust layouts at breakpoints.
- Make images responsive with `max-width
- Test designs across devices and screen sizes.
Use a fluid layout that adapts to different screen sizes.
Use a fluid layout to let your content adjust naturally to different screen sizes instead of relying on fixed-width elements. CSS Grid, Flexbox, and relative units such as percentages, `rem`, and `fr` can help columns and components expand or shrink to fit the available space. This approach makes a page easier to use across phones, tablets, and desktops, while reducing the need for device-specific layouts. Test the design at different viewport widths to catch content that feels cramped, stretches too far, or causes horizontal scrolling.
Add the viewport meta tag for proper mobile scaling.
Add the viewport meta tag to help mobile browsers display your page at the device’s actual width instead of shrinking a desktop-sized layout to fit. Place <meta name="viewport" content="width=device-width, initial-scale=1"> inside the document’s <head>. This gives responsive CSS a reliable viewport to work with and helps keep text, images, and other content readable on small screens.
Use CSS media queries to adjust layouts at breakpoints.
Use CSS media queries to adjust your layout when the content needs more or less space—not just to match specific devices. For example, a single-column page can switch to two columns at a wider viewport, while navigation or text spacing can adapt as the screen changes. Test the design at different widths and choose breakpoints where the layout starts to feel cramped or unbalanced.
Make images responsive with `max-width
To make images responsive, set max-width: 100% so each image can shrink to fit its container without growing beyond its original size. Pair it with height: auto to preserve the image’s proportions as its width changes. This simple CSS rule helps prevent images from overflowing on narrow screens and keeps pages easier to view across devices.
Test designs across devices and screen sizes.
Test your responsive design across a range of devices and screen sizes, not just the dimensions of a few popular phones. Resize your browser gradually and check on real phones and tablets when possible. Look for horizontal scrolling, cramped text, overlapping elements, and controls that are hard to tap. MDN Web Docs emphasizes that testing helps reveal where a layout needs adjustment, so choose breakpoints based on when the content stops working well—not on a specific device model.
