Media Queries — Complete Guide
Media Queries — Complete Guide: free step-by-step lesson with examples, common mistakes, and interview tips — part of CSS Tutorial on Toolliyo Academy.
On this page
CSS Tutorial · Lesson 21 of 100
Media Queries
Foundations & Layout ✓ → Responsive & Motion → Architecture & Perf → Projects
Responsive & Motion · 2 — Adapt · ~13 min read · CSS — Responsive Design
1. Introduction
Today: Media Queries. Read the diagram, paste the CSS, then change one value and watch the UI update.
Media queries apply CSS when device conditions match — usually min-width or max-width breakpoints. @media (min-width: 768px) { ... } adds tablet-and-up rules.
2. Real-world story
Category page shows 2-up on phone, 4-up on laptop.
Outcome: Media queries align with merchandising breakpoints.
3. Why it matters
StyleVerse hides dense tables on mobile and shows card lists instead — media queries switch presentation at breakpoints.
4. Visual understanding
Read this diagram — the mental model for Media Queries.
HTML structure
│
CSS rules for: Media Queries
│
Painted UI (StyleVerse component)
5. Key concepts (easy words)
| Idea | Meaning |
|---|---|
| What | Media queries apply CSS when device conditions match — usually min-width or max-width breakpoints. @media (min-width: 768px) { ... } adds tablet-and-up rules. |
| Remember | @media wraps conditional CSS. min-width matches mobile-first pattern. Combine width and preference queries. |
| Responsive tip | Start mobile-first; enhance with min-width queries. |
6. How it works
- Product grid starts 2 columns on phone, 3 on tablet, 4 on desktop with increasing gap — classic e-commerce responsive pattern.
- @media wraps conditional CSS.
- min-width matches mobile-first pattern.
- Check the result in DevTools → Computed and the box model overlay.
7. Choose wisely
| Option | Notes |
|---|---|
| Do | Practice Media Queries on a small StyleVerse card |
| Avoid | Copying huge frameworks before learning core CSS |
8. Try this example
Paste into a <style> block (or use Run Example). Refresh to see StyleVerse UI changes.
.product-grid {
display: grid;
grid-template-columns: repeat(2, 1fr);
gap: 12px;
}
@media (min-width: 600px) {
.product-grid { grid-template-columns: repeat(3, 1fr); gap: 16px; }
}
@media (min-width: 1024px) {
.product-grid { grid-template-columns: repeat(4, 1fr); gap: 20px; }
}
Line walkthrough
| Code | What it means |
|---|---|
.product-grid { | Selector — picks which elements get these declarations. |
display: grid; | Declaration — property and value that change appearance. |
grid-template-columns: repeat(2, 1fr); | Declaration — property and value that change appearance. |
gap: 12px; | Declaration — property and value that change appearance. |
} | Ends the rule block. |
@media (min-width: 600px) { | Selector — picks which elements get these declarations. |
.product-grid { grid-template-columns: repeat(3, 1fr); gap: 16px; } | Selector — picks which elements get these declarations. |
} | Ends the rule block. |
@media (min-width: 1024px) { | Selector — picks which elements get these declarations. |
.product-grid { grid-template-columns: repeat(4, 1fr); gap: 20px; } | Selector — picks which elements get these declarations. |
Run Example »
Edit the code below and click Run to see the result in Toolliyo’s live editor.
9. Another real-world angle
10. Best practices checklist
- Prefer classes over ids for reusable StyleVerse components.
- Use design tokens (CSS variables) for color and spacing.
- Mobile-first media queries; test keyboard focus.
- Keep specificity low — avoid !important except rare overrides.
- Animate transform/opacity when you can; respect prefers-reduced-motion.
11. Common mistakes
- Dozens of arbitrary breakpoints — stick to design system tokens.
- Testing only one device width in Chrome.
12. Practice in the browser
- Build product-grid with eight card placeholders.
- Drag DevTools width across 599, 600, 1024px.
- Add prefers-color-scheme media query for dark.
- Use max-width queries only when mobile-first is not used.
Experiments
- Add @media (max-width: 599px) { .product-grid { grid-template-columns: 1fr; } }.
- Include prefers-reduced-motion in same file.
13. FAQ
Where should I put CSS for Media Queries?
Start in a <style> block or styles.css linked from HTML. Later use CSS Modules, Tailwind, or tokens in a design system.
Why is my rule not applying?
Check selector match, typos, specificity, and whether a more specific rule or inline style wins. DevTools Computed tells the truth.
Flexbox or Grid for this?
Flex for one direction (toolbars, card rows). Grid for two-dimensional page frames. Many UIs use both.
14. Interview questions
Explain Media Queries simply.
Media queries apply CSS when device conditions match — usually min-width or max-width breakpoints. @media (min-width: 768px) { ... } adds tablet-and-up rules.
How do you debug CSS?
Inspect element, toggle declarations, read Computed styles, outline the box model, and reduce specificity conflicts.
How does this show up in production UI?
Media queries align with merchandising breakpoints.
15. Remember
- @media wraps conditional CSS.
- min-width matches mobile-first pattern.
- Combine width and preference queries.
Interview prep for this lesson
Practice these questions aloud after reading—each links to a full structured answer.
Sign in to ask a question or upvote helpful answers.
No questions yet — be the first to ask!