@media: a page that fits a phone
Section 5: Pro CSS: shadows and movementLesson 5.4 · ⏱ 9 min
A pro page fits a big screen and a phone. @media (max-width: 500px) means: “if the screen is 500 pixels wide or less, use these rules”.
HTML
<div class="row">
<div class="box">🤖 Robots</div>
<div class="box">🌐 Web</div>
<div class="box">🐍 Python</div>
</div>
CSS
body { font-family: Tahoma, sans-serif; }
.row { display: flex; gap: 12px; padding: 12px; }
.box {
flex: 1;
padding: 20px;
text-align: center;
font-size: 20px;
border-radius: 14px;
background: #ffe9a8;
}
/* a narrow screen (a phone): one under the other */
@media (max-width: 500px) {
.row { flex-direction: column; }
.box { background: #c8f0d0; }
}
🖥️ The page in Robo Web:

🖥️ What happens on the stage?
This picture is on a narrow screen: the boxes are on top of each other and their colour changed. On a wide screen they are side by side.
💡 Tip
Drag the line between the code and the page in the lab to make the page narrower, and watch the boxes move from a row to a column.
📌 Another example: a grid that changes with the width
Four columns on a big screen, and two on a phone.
HTML
<div class="grid">
<div>1</div><div>2</div><div>3</div><div>4</div>
</div>
CSS
body { font-family: Tahoma, sans-serif; }
.grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 10px; }
.grid div { padding: 24px; text-align: center; font-size: 24px; border-radius: 12px; background: #D6E6FF; }
@media (max-width: 500px) {
.grid { grid-template-columns: repeat(2, 1fr); }
}
🖥️ The page in Robo Web:

✍️ Practise
Open the lab, paste the example, and drag the line left and right.
✍️ Practise here in the lesson✖ Close the labor open the lab in a new page ↗
🎯 Challenge yourself
Easy
On a narrow screen the title gets smaller.
👀 Show the solution🙈 Hide the solution
HTML
<h1>A big title</h1>
CSS
h1 { font-size: 50px; }
@media (max-width: 500px) {
h1 { font-size: 28px; }
}
🖥️ The page in Robo Web:

Result: the title got smaller.
Medium
On a narrow screen the background becomes orange.
👀 Show the solution🙈 Hide the solution
HTML
<p>Change the preview width</p>
CSS
body { background: white; }
@media (max-width: 500px) {
body { background: orange; }
}
🖥️ The page in Robo Web:

Result: orange on a phone.
Hard
A row of links that becomes a column on a phone.
👀 Show the solution🙈 Hide the solution
HTML
<nav class="menu">
<a href="#">Home</a>
<a href="#">Lessons</a>
<a href="#">Contact</a>
</nav>
CSS
body { font-family: Tahoma, sans-serif; }
.menu { display: flex; gap: 16px; padding: 10px; background: #1B2350; }
.menu a { color: white; }
@media (max-width: 500px) {
.menu { flex-direction: column; }
}
🖥️ The page in Robo Web:

Result: the links are under each other.

