CSS3 Responsive
Responsive design means one page that adapts its layout to the screen it is viewed on, from a narrow phone to a wide monitor. Instead of building separate mobile and desktop sites, you use flexible widths, relative units and media queries so content rearranges itself. It is the foundation that frameworks like Bootstrap are built on, so it is worth understanding before using any grid system.
Start with the viewport meta tag
Mobile browsers pretend to be a wide desktop screen by default and then shrink the page down. Your media queries never trigger, and text becomes tiny. One line in the <head> fixes this by telling the browser to use the device's real width:
<meta name="viewport" content="width=device-width, initial-scale=1">
Without this tag, nothing else in this lesson works properly on phones.
Build fluid, then add breakpoints
A responsive layout rests on a few habits:
- Percentages and max-width instead of fixed widths. A container with
width: 100%; max-width: 1100pxfills small screens and stops growing on large ones. - Flexible media. Images and videos get
max-width: 100%andheight: autoso they never overflow their box. - Relative units for text.
remfollows the user's browser font setting, andclamp()lets a heading scale between a minimum and maximum size. - Layouts that wrap. Flexbox with
flex-wrap: wrapor CSS Grid withauto-fitlets columns drop to new rows when space runs out.
Mobile first means writing the base CSS for small screens and using min-width media queries to add columns as space grows. The small-screen styles stay simple, and larger screens only override what they need.
.container {
width: 100%;
max-width: 1100px;
margin: 0 auto;
padding: 0 16px;
}
.cards {
display: grid;
gap: 16px;
grid-template-columns: 1fr; /* one column on phones */
}
@media (min-width: 600px) {
.cards { grid-template-columns: repeat(2, 1fr); }
}
@media (min-width: 960px) {
.cards { grid-template-columns: repeat(3, 1fr); }
}
h1 { font-size: clamp(1.75rem, 4vw, 3rem); }
img { max-width: 100%; height: auto; }
Choosing breakpoints
Do not pick breakpoints from a list of phone models. Devices change constantly. Instead, slowly widen your browser window and add a breakpoint wherever the design starts to look stretched or cramped. Your content decides the breakpoints. Bootstrap ships with its own set of breakpoints, which you will meet in the next lesson, but the idea is the same.
The usual bug: horizontal scrolling on phones
If a page scrolls sideways on a phone, some element is wider than the screen. Common causes are a fixed width: 800px, a long URL or word that cannot wrap, a table with many columns, or an element with width: 100% plus padding when box-sizing is still content-box. Set box-sizing: border-box on all elements, use overflow-wrap: anywhere for long strings, and wrap wide tables in a container with overflow-x: auto. Browser developer tools have a device toolbar that lets you test at around 390px wide to catch these early.
Takeaway
Add the viewport tag, think in flexible widths, size media to its container, and layer min-width media queries on top of a mobile base. Once these habits feel natural, a framework grid is just a shortcut for things you already understand.
Ready to master real-world web development?
Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.