#1 India's Top IT Training Institute
×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
Mentors

Mentors

Meet the industry experts who train and guide you through your course.

Read More →
Free Resources

Free Resources

Free tutorials, interview questions, tools and calculators to help you learn.

Explore →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →
CSS3 Fundamentals

CSS3 Media Queries

Media queries let you apply CSS only when certain conditions are true, most often the width of the viewport. They are the backbone of responsive design: the same HTML can show one column on a phone, two on a tablet and three on a laptop, simply because different rules switch on at different screen sizes.

Anatomy of a media query

A media query starts with @media, optionally names a media type such as screen or print, and then lists one or more conditions in brackets. Rules inside the block only apply while the conditions match. When the window is resized, the browser re-checks them instantly.

/* Mobile-first: base styles for small screens */
.cards {
  display: grid;
  grid-template-columns: 1fr;
  gap: 16px;
}

/* Tablets and up */
@media (min-width: 600px) {
  .cards {
    grid-template-columns: repeat(2, 1fr);
  }
}

/* Laptops and up */
@media (min-width: 992px) {
  .cards {
    grid-template-columns: repeat(3, 1fr);
  }
}

/* Hide navigation when printing */
@media print {
  nav, .ads {
    display: none;
  }
}

Mobile first or desktop first

There are two ways to write responsive CSS. Mobile first means writing base styles for small screens and using min-width queries to add layout as space grows, like the example above. Desktop first starts with the large layout and uses max-width queries to simplify it for smaller screens.

Mobile first tends to produce leaner CSS, since phones download the simple layout and only larger screens apply the extra rules. It also forces you to decide what content really matters, because a narrow screen has no room for clutter.

Useful features beyond width

  • orientation: landscape or portrait reacts to how the device is held.
  • prefers-color-scheme: dark lets you offer a dark theme that follows the user's system setting.
  • prefers-reduced-motion: reduce lets you turn off animations for users who find motion uncomfortable.
  • hover: hover detects devices with a real hover capability, so touch screens do not rely on hover menus.

You can combine conditions with and, for example @media (min-width: 600px) and (max-width: 991px) to target only a middle range. Modern browsers also understand the range syntax @media (600px <= width < 992px), which reads more naturally.

The viewport tag you must not forget

The most common reason media queries "do not work" on a real phone has nothing to do with the CSS. Without <meta name="viewport" content="width=device-width, initial-scale=1"> in the HTML head, mobile browsers pretend the screen is about 980 pixels wide and zoom out. Your min-width: 600px query then matches on a phone, and the page shows the desktop layout shrunk to a tiny size.

Another pitfall is query order. Two queries with the same specificity follow the normal cascade, so a later rule wins. In mobile-first code, place smaller breakpoints before larger ones; otherwise a min-width: 600px rule written after a min-width: 992px rule will override it on big screens.

Choosing breakpoints

Rather than copying a list of device widths, resize your page slowly and add a breakpoint wherever the design starts to look cramped or stretched. Content-driven breakpoints keep working as new devices appear.

Takeaway: include the viewport meta tag, write mobile-first base styles, add min-width queries in increasing order, and pick breakpoints where your own layout needs them.

Ready to master real-world web development?

Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.

Explore Course