People visit websites on phones, tablets, laptops and large monitors. A responsive page adapts its layout to the size of the screen.
What is Responsiveness
A responsive web page changes its layout and sizes to suit the screen, so that it is easy to read and use on any device without zooming or scrolling sideways. Since most web traffic now comes from phones, responsive design is essential, and search engines rank mobile-friendly pages higher. Mobile-first design starts with the phone layout and adds features for larger screens.
Definition: Media query. A CSS rule that applies a set of styles only when a condition about the device is true, such as the screen width being below a given value.
The Viewport Meta Tag
By default, phones display pages as if on a wide desktop screen and shrink them. The tag <meta name="viewport" content="width=device-width, initial-scale=1.0"> tells the browser to use the real width of the device, which is the first step in making a page responsive.
Flexible Units and Images
Fixed sizes in pixels do not adapt. Relative units do: % (relative to the parent), em and rem (relative to font size), and vw and vh (percent of the viewport width and height). Images are made flexible with max-width: 100%; height: auto;, so they shrink to fit their container. Flexbox with flex-wrap: wrap and CSS Grid with auto-fit let items rearrange themselves.
Media Queries
A media query applies styles only when a condition holds: @media (max-width: 768px) { ... } applies to screens 768 pixels wide or less. Common breakpoints are about 576px (phones), 768px (tablets), 992px (laptops) and 1200px (desktops). In a media query, a page can stack columns vertically, enlarge buttons, hide side content or change the navbar into a vertical menu.
Table 2.3: Units for responsive design
| Unit | Relative to | Example |
|---|---|---|
| % | Parent element | width: 50% |
| em | Font size of the element | padding: 1.5em |
| rem | Font size of the root | font-size: 1.2rem |
| vw, vh | Viewport width, height | height: 100vh |
Example 2.3: A responsive two-column layout
Problem: Make a layout with a main area and a sidebar side by side on wide screens, stacked one below the other on screens 768px wide or less.
Solution:
- Use a container with display: flex and give main flex: 3 and aside flex: 1.
- In @media (max-width: 768px), set flex-direction: column on the container.
- Make images flexible with max-width: 100%.
Result: On a laptop the sidebar sits beside the content; on a phone it moves below.
.container { display: flex; gap: 16px; }
main { flex: 3; }
aside { flex: 1; background: #DEEAF6; }
img { max-width: 100%; height: auto; }
@media (max-width: 768px) {
.container { flex-direction: column; }
nav ul { flex-direction: column; }
body { font-size: 1.1rem; }
}
Common mistakes
- Leaving out the viewport meta tag, so that phones shrink the desktop layout.
- Giving fixed pixel widths to containers and images.
- Writing media queries before the general rules, so that the general rules override them.
Key takeaways
- Responsive pages adapt to every screen size.
- Start with the viewport meta tag.
- Use relative units, flexible images, Flexbox and Grid.
- Media queries apply styles at breakpoints.
Practice questions
- What is responsive web design?
- What does max-width: 100% do for an image?
- Write a media query for screens up to 600px wide.
Answers to the practice questions
- Designing pages whose layout and sizes adapt to the screen of the device.
- It stops the image from being wider than its container, so it shrinks on small screens.
- @media (max-width: 600px) { ... }.
Sources
- Official BCA syllabus Web Technologies, course code 25BCA402SEC02, Unit II
