CSS Media Queries

CSS Media Queries allow you to apply different CSS styles based on the screen size, device type, or screen orientation. They are the foundation of Responsive Web Design.

1. Introduction to CSS Media Queries

A Media Query checks the characteristics of the user's device and applies CSS rules only when the specified condition is true.

Why Use Media Queries?
  • Create mobile-friendly websites.
  • Improve user experience.
  • Support different screen sizes.
  • Build responsive layouts.
  • Reduce the need for separate mobile websites.

Desktop

↓

Tablet

↓

Mobile
Media Queries make one website work beautifully on every device.

2. Media Query Syntax

Media Queries begin with the @media rule followed by a condition and the CSS styles to apply.


@media (max-width:768px){

    body{

        background:#f8f9fa;

    }

}
Part Description
@media Starts the Media Query.
(condition) Defines when styles are applied.
{ } Contains responsive CSS rules.
Media Query styles are applied only when the specified condition is true.

3. max-width Media Query

The max-width condition applies CSS rules when the screen width is less than or equal to the specified value.


@media (max-width:768px){

    h1{

        font-size:24px;

    }

}
Screen Width Media Query Runs?
500px ✔ Yes
768px ✔ Yes
900px ✖ No
max-width is commonly used for mobile-first responsive design.

4. min-width Media Query

The min-width condition applies CSS rules when the screen width is greater than or equal to the specified value.


@media (min-width:992px){

    body{

        background:#ffffff;

    }

}
Screen Width Media Query Runs?
800px ✖ No
992px ✔ Yes
1200px ✔ Yes
min-width is widely used in desktop-first designs.

5. Common Device Breakpoints

Developers commonly use breakpoints to target different screen sizes such as mobile phones, tablets, laptops, and desktop computers.

Device Typical Width
Mobile 0px – 767px
Tablet 768px – 991px
Laptop 992px – 1199px
Desktop 1200px and above

/* Mobile */

@media(max-width:767px){}

/* Tablet */

@media(min-width:768px){}

/* Desktop */

@media(min-width:1200px){}
Choosing appropriate breakpoints helps ensure a consistent user experience across all devices.

6. Combining Media Conditions

You can combine multiple media conditions using the and keyword. This allows CSS to apply styles only when all specified conditions are true.


@media (min-width:768px) and
(max-width:991px){

    body{

        background:#e9ecef;

    }

}
Condition Result
min-width:768px Screen width must be at least 768px.
max-width:991px Screen width must not exceed 991px.
Combining conditions helps target specific devices such as tablets.

7. Responsive Navigation with Media Queries

Media Queries are commonly used to make navigation menus responsive by changing their layout on smaller screens.


nav{

    display:flex;

    justify-content:space-between;

}

@media(max-width:768px){

    nav{

        flex-direction:column;

    }

}
Desktop Mobile
Horizontal Menu Vertical Menu
Responsive navigation improves usability on mobile devices.

8. Responsive Grid & Flexbox using Media Queries

Media Queries can change Flexbox and Grid layouts according to the screen size.

Flexbox Example

.container{

    display:flex;

}

@media(max-width:768px){

    .container{

        flex-direction:column;

    }

}
Grid Example

.gallery{

    display:grid;

    grid-template-columns:
    repeat(3,1fr);

}

@media(max-width:768px){

    .gallery{

        grid-template-columns:1fr;

    }

}
Combining Media Queries with Flexbox or Grid creates highly responsive layouts.

9. Practical Media Query Examples

Example 1: Hide Sidebar on Mobile

@media(max-width:768px){

    .sidebar{

        display:none;

    }

}

Example 2: Change Font Size

@media(max-width:600px){

    h1{

        font-size:24px;

    }

}

Example 3: Responsive Button

@media(max-width:576px){

    .btn{

        width:100%;

    }

}
Media Queries can control layouts, typography, spacing, visibility, and component behavior.

10. Media Query Comparison Table

Media Query Purpose
max-width Apply styles below a specified width.
min-width Apply styles above a specified width.
orientation Target portrait or landscape mode.
and Combine multiple conditions.
not Exclude matching conditions.
only Prevent older browsers from applying styles.

@media (orientation:landscape){

    body{

        background:#f8f9fa;

    }

}
Media Queries provide complete control over responsive layouts for different devices and screen orientations.

11. CSS Media Query Best Practices

Following best practices helps you write clean, efficient, and maintainable Media Queries for responsive websites.

  • Use a mobile-first approach whenever possible.
  • Keep breakpoints consistent throughout the project.
  • Use relative units like em and rem.
  • Avoid writing unnecessary Media Queries.
  • Group similar Media Queries together.
  • Test layouts on different devices and browsers.
  • Combine Flexbox or Grid with Media Queries.
  • Keep CSS organized for easy maintenance.
Well-organized Media Queries improve readability and website performance.

12. Common Media Query Mistakes

Mistake Correct Practice
Using too many breakpoints. Use only necessary breakpoints.
Ignoring mobile devices. Start with a mobile-first design.
Using fixed widths. Use responsive units.
Not testing on real devices. Test on phones, tablets, and desktops.
Writing duplicate Media Queries. Organize and reuse existing queries.

❌ Incorrect

.box{

    width:900px;

}

✔ Correct

.box{

    width:100%;

    max-width:900px;

}
Avoid unnecessary breakpoints and fixed-width layouts.

13. Browser Support

CSS Media Queries are supported by all modern web browsers, making them one of the most reliable responsive design features.

Browser Support
Google Chrome ✔ Supported
Mozilla Firefox ✔ Supported
Microsoft Edge ✔ Supported
Safari ✔ Supported
Opera ✔ Supported
Media Queries work reliably across all modern browsers.

14. CSS Media Query Interview Questions

  1. What is a CSS Media Query?
  2. Why are Media Queries used?
  3. What is the difference between max-width and min-width?
  4. What are breakpoints in Responsive Web Design?
  5. What is the mobile-first approach?
  6. How do you combine multiple Media Query conditions?
  7. What is the purpose of the orientation feature?
  8. Can Media Queries be used with Flexbox and Grid?
  9. How do Media Queries improve user experience?
  10. Which CSS rule begins every Media Query?
These interview questions are frequently asked in Front-End Developer interviews.

15. Lesson Summary

In this lesson, you learned:
  • ✔ Introduction to CSS Media Queries.
  • ✔ Media Query Syntax.
  • max-width and min-width.
  • ✔ Common Device Breakpoints.
  • ✔ Combining Media Conditions.
  • ✔ Responsive Navigation.
  • ✔ Responsive Flexbox and Grid.
  • ✔ Practical Examples.
  • ✔ Best Practices and Common Mistakes.
  • ✔ Browser Support and Interview Questions.
Congratulations! You have successfully completed the CSS Media Queries lesson. You can now create responsive layouts that adapt perfectly to different screen sizes.

Quick Quiz

Which Media Query is commonly used to apply styles for mobile devices?