CHAPTER 77

CSS Media Queries

Learn how media queries apply different CSS rules based on screen and device conditions.

01. What is CSS Media Queries?

Media queries allow CSS to respond to viewport conditions.
They are commonly used for responsive web design.
Styles can change at specific breakpoints.

02. Syntax

@media (max-width: 768px) { selector { property: value; } }

03. Example

CSS / HTML Example AIFA Coding Lab
<!DOCTYPE html>
<html>
<head>
<style>

.container {
    display: flex;
    gap: 20px;
}

.card {
    flex: 1;
    padding: 30px;
    background: #2563eb;
    color: white;
}

@media(max-width:700px) {

    .container {
        flex-direction: column;
    }

}

</style>
</head>
<body>

<div class="container">

    <div class="card">One</div>
    <div class="card">Two</div>

</div>

</body>
</html>

04. Important Points

✓ max-width targets screens below a specific width.
✓ min-width targets screens above a specific width.
✓ Breakpoints should be based on content rather than only device names.
✓ Mobile-first CSS can simplify responsive design.

05. Common Mistakes

⚠ Creating too many breakpoints.
⚠ Using breakpoints based only on specific device models.
⚠ Forgetting to test intermediate widths.

06. Real World Use

Media queries are essential for responsive websites, mobile navigation, responsive cards and adaptive typography.

Ready to Practice? 🚀

Open the AIFA CSS Editor and practice this chapter live.

Try It Yourself →
Practice Exercise

Create a Responsive Section

Create three cards in a row on desktop and stack them on screens below 700px.