What are CSS Media Queries?

CSS Media Queries are rules that allow developers to apply CSS styles based on device characteristics such as screen width, height, orientation, or resolution. They are the backbone of responsive web design, ensuring websites look great on mobiles, tablets, laptops, and desktops.


Why are Media Queries Helpful?

  • ✅ Make websites mobile-friendly.
  • ✅ Improve user experience on different devices.
  • ✅ Follow Google’s mobile-first indexing SEO rules.
  • ✅ Save time by writing adaptive styles instead of building separate mobile sites.

Basic Syntax of a Media Query


    @media (condition) {
        /* CSS styles go here */
    }
      

Example: Applying styles only when the screen width is less than 768px:


    @media (max-width: 768px) {
        body {
            background-color: lightblue;
        }
    }
      

Result: On mobile or smaller screens, the background becomes light blue.


Real-Time Examples of Media Queries

1. Mobile First Design


    /* Base (Mobile) */
    body {
        font-size: 14px;
    }

    /* Tablet */
    @media (min-width: 768px) {
        body { 
            font-size: 16px; 
        }
    }

    /* Desktop */
    @media (min-width: 1024px) {
        body { 
            font-size: 18px; 
        }
    }
    

Result: Font size scales up as screen width increases.

2. Responsive Navigation


    /* Hide menu on mobile */
    @media (max-width: 600px) {
        nav ul {
            display: none;
        }
    }

    /* Show menu on larger screens */
    @media (min-width: 601px) {
        nav ul {
            display: flex;
        }
    }
  

Result: Collapsed menu on mobile, full navigation on desktop.

Popular Media Query Breakpoints

Device Breakpoint
Mobile max-width: 600px
Tablet min-width: 601px and max-width: 1024px
Laptop/Desktop min-width: 1025px

✅ Top Media Query Interview Questions ?

  1. What are CSS Media Queries and why are they used?
  2. Explain the difference between min-width and max-width.
  3. What is the difference between Mobile-first and Desktop-first approaches?
  4. How do you target both orientation and resolution using media queries?
  5. Can we combine multiple conditions in a single media query? Give an example.
  6. What’s the difference between @media screen and @media all?
  7. How can you apply different styles for high-resolution (Retina) displays?
  8. Write a media query that applies styles only for devices between 768px and 1024px.
  9. What are the limitations of media queries?
  10. do media queries differ from using CSS frameworks like Bootstrap for responsiveness?

Conclusion

CSS Media Queries are a powerful feature in responsive web design that help developers create websites that adapt seamlessly to different devices, screen sizes, and orientations. Whether it’s a desktop, tablet, or mobile phone, media queries ensure that users enjoy an optimal browsing experience without needing separate websites for each device.

In real-time projects, they save time, reduce code duplication, and improve user engagement by delivering a consistent design. By mastering CSS Media Queries, you’re not just coding styles—you’re designing for flexibility, accessibility, and performance, which are the foundations of modern web development.

👉 In short, CSS Media Queries = Better UX + Responsive Design + Future-Ready Websites.