Responsive Design Lesson 23 of 33
CSS Media Queries — Different Styles for Phones and Laptops
Use media queries to change your layout at different screen sizes, choose sensible breakpoints, and support dark mode and printing.
Most Filipino students visit websites on a phone, but a teacher might project the same page on a wide screen in class. A media query lets you write CSS that applies only in certain conditions, most often "when the screen is at least this wide". This is how one website can have a one-column layout on phones and three columns on laptops.
The syntax
@media (min-width: 768px) {
/* these rules only apply when the screen is 768px wide or more */
.cards {
grid-template-columns: 1fr 1fr 1fr;
}
}
A media query is a block that starts with @media, followed by a condition in parentheses. Inside the curly braces, you write normal CSS rules. When the condition is true, the rules apply. When it's false, they're ignored.
<style>
.box { padding: 24px; color: white; font: bold 20px Arial, sans-serif; text-align: center; background: #b4372b; }
.box::after { content: "Narrow screen (under 500px)"; }
@media (min-width: 500px) {
.box { background: #0f5c4d; }
.box::after { content: "Wide screen (500px or more)"; }
}
</style>
<div class="box"></div>
min-width vs. max-width
(min-width: 768px)— "768px and wider"(max-width: 767px)— "767px and narrower"
You can combine conditions with and: @media (min-width: 600px) and (max-width: 900px) applies only between 600 and 900 pixels.
Mobile-first
There are two ways to organize responsive CSS:
- Desktop-first: write the laptop layout normally, then use
max-widthqueries to fix things for phones. - Mobile-first: write the phone layout normally (it's simpler, usually one column), then use
min-widthqueries to add columns for bigger screens.
Mobile-first is recommended. Phone layouts are simpler, so the base CSS stays short, and phones (which are often slower) don't have to process the desktop rules first only to undo them.
<style>
* { box-sizing: border-box; }
body { font-family: Arial, sans-serif; margin: 10px; }
/* Phone first: one column */
.cards { display: grid; gap: 10px; }
.card { background: #e3f1ec; padding: 16px; border-radius: 10px; }
/* Tablets: two columns */
@media (min-width: 500px) {
.cards { grid-template-columns: 1fr 1fr; }
}
/* Laptops: four columns */
@media (min-width: 800px) {
.cards { grid-template-columns: repeat(4, 1fr); }
}
</style>
<div class="cards">
<div class="card">🌾 Rice</div>
<div class="card">🐟 Fish</div>
<div class="card">🥬 Vegetables</div>
<div class="card">🥥 Coconut</div>
</div>
Choosing breakpoints
A breakpoint is the width where your layout changes. There are thousands of different phone and laptop sizes, so don't try to target specific devices. Instead:
- Start with the narrow layout. Make the window wider slowly.
- When the layout starts to look stretched or empty, that's your breakpoint.
As a starting point, many developers use widths around these:
| Breakpoint | Roughly |
|---|---|
| below 600px | Phones |
| 600px and up | Large phones, small tablets |
| 900px and up | Tablets in landscape, small laptops |
| 1200px and up | Laptops and desktop monitors |
Common responsive changes
What do people actually change inside media queries? Usually:
- The number of columns (grid-template-columns, flex-direction)
- Font sizes of big headings (smaller on phones)
- Padding and spacing (less on phones, more on big screens)
- Showing or hiding things, like a full menu on laptops vs. a "☰" button on phones
<style>
body { font-family: Arial, sans-serif; margin: 0; }
.nav { display: flex; flex-direction: column; background: #0f5c4d; }
.nav a { color: white; text-decoration: none; padding: 12px 16px; border-bottom: 1px solid rgba(255,255,255,0.15); }
@media (min-width: 600px) {
.nav { flex-direction: row; }
.nav a { border-bottom: 0; }
}
</style>
<nav class="nav">
<a href="#">Home</a><a href="#">Lessons</a><a href="#">Quizzes</a><a href="#">About</a>
</nav>
Other media features
Media queries can check more than width.
Dark mode
prefers-color-scheme detects whether the user's phone or computer is set to dark mode:
<style>
body { background: white; color: #1d2321; font-family: Arial, sans-serif; padding: 16px; }
@media (prefers-color-scheme: dark) {
body { background: #121615; color: #e6ebe8; }
}
</style>
<p>Switch your device between light and dark mode. This page follows it.</p>
Reduced motion
Some people get dizzy from animations. prefers-reduced-motion: reduce lets you turn animations off for them (you'll use this in the Animations lesson).
@media print styles apply when someone prints the page or saves it as PDF. Hide menus and ads, and use black text on white:
@media print {
nav, footer, .ad { display: none; }
body { color: black; background: white; }
}
Orientation and hover
(orientation: landscape) checks if the screen is wider than it is tall. (hover: hover) checks if the device has a mouse, which is useful because hover effects don't really exist on touch screens.
Quick Quiz
Check what you learned. Walang grade ito, practice lang. 🙂
By Sir Reyel · Last updated 2026-12-04