Start with the viewport tag
<meta name="viewport" content="width=device-width, initial-scale=1">
Without this line, phones pretend to be a wide desktop screen and shrink the page, making the text tiny. Put it in the <head> of every page.
Flexible widths
Avoid fixed pixel widths for large areas. Let content fill the space up to a comfortable maximum:
.container {
width: min(1100px, 100% - 32px);
margin-inline: auto; /* centre it */
}
On a big screen the container is 1100 pixels wide; on a phone it fills the screen with 16 pixels of space on each side.
Images that fit
img {
max-width: 100%;
height: auto;
}
Images now shrink to fit their container and keep their proportions. Also set width and height attributes in the HTML, so the browser reserves the right space and the page does not jump while images load.
Media queries
Design for small screens first, then add rules for wider ones:
.features {
display: grid;
gap: 16px;
grid-template-columns: 1fr; /* phones: one column */
}
@media (min-width: 700px) {
.features { grid-template-columns: 1fr 1fr; } /* tablets */
}
@media (min-width: 1000px) {
.features { grid-template-columns: repeat(3, 1fr); } /* desktops */
}
Choose breakpoints where your content starts to look wrong, not by specific phone models.
Text that scales
h1 {
font-size: clamp(1.8rem, 5vw, 3rem);
}
clamp(min, preferred, max) lets the heading grow with the screen width but never become smaller than 1.8rem or larger than 3rem. Keep body text at least 16 pixels, and make buttons and links at least about 44 pixels tall so they are easy to tap.
Try it yourself
In your browser's developer tools, turn on the device toolbar (Ctrl+Shift+M or Cmd+Shift+M) and resize a page you made in Unays Code. Fix anything that overflows sideways, then make a three-card layout that becomes one column on phones.
This tutorial was written with AI assistance and reviewed by Unays. The runnable code examples were tested before publishing. Spotted a mistake? Tell us at [email protected].