Loading Page Content...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
Think about your school's sports day trials:
min-width: 150cm) are eligible for the Senior High Jump." (Applies to 150cm, 160cm, 180cm and above).max-width: 12yr) compete in the Sub-Junior relay race." (Applies to 12, 11, 10 and below).130cm <= height <= 150cm) play on the Middle School badminton court."+-------------------------------------------------------------------------+ | THE MEDIA QUERY TIMELINE SPECTRUM | | | | 0px 640px 1024px 1440px | | |-------------------|-------------------|-------------------|----> | | | | A. max-width: 640px | | <===================] (Applies to mobile phones ONLY) | | | | B. min-width: 1024px | | [============================================> | | (Applies to Laptops, Desktops, and 4K Screens) | | | | C. Range: (640px <= width <= 1024px) | | [===================] (Tablets ONLY) | +-------------------------------------------------------------------------+
Media queries are the brain of responsive design. They allow your CSS stylesheet to inspect the user's screen resolution, device orientation, and even print settings before deciding which style rules to activate.
In this tutorial, you will master the logic of min-width, max-width, modern Level 4 range syntax (width >= 768px), and logical operators.
min-width vs max-widthA media query wraps a block of CSS rules inside a conditional @media statement:
min-width (Additive / Mobile-First): You write simple styles for small screens first, then add richer multi-column enhancements as the screen gets wider.max-width (Subtractive / Desktop-First): You write complex styles for widescreen desktops first, then remove or override features as the screen shrinks down to mobile.For years, developers complained that writing @media (min-width: 768px) and (max-width: 1024px) was clunky and repetitive.
All modern browsers now support the clean, mathematical Range Syntax:
This mathematical syntax is 100% supported in Chrome, Firefox, Safari, and Edge.
and, not, and the Comma (Or)You can chain multiple conditions together using Boolean logic:
Have you ever tried printing a school project article directly from Wikipedia or a blog, only for the printer to waste 10 pages printing black navigation menus, banner advertisements, and search bars?
You can write specialized print stylesheets using @media print:
| Bad Practice (Don't) | Good Practice (Do) | Why? |
|---|---|---|
Forgetting the viewport meta tag in HTML <head> | Always include <meta name="viewport" content="width=device-width, initial-scale=1.0"> | Without this meta tag, mobile smartphones ignore media queries and zoom out to 980px desktop view! |
Writing overlapping boundaries: max-width: 768px and min-width: 768px | Use max-width: 767.98px and min-width: 768px (or modern <= 767px and >= 768px) | Prevents conflicting styles when a device screen width is exactly 768px. |
| Hardcoding media queries for specific phone brands (iPhone 14 query) | Base breakpoints on your content layout needs, not device names | Thousands of Android and Apple phones exist with different pixel ratios. |
Overusing !important inside media queries | Structure your stylesheet cascade logically | Media queries placed lower in the stylesheet naturally override earlier rules. |
min-width: 768px: Styles apply from 768px upward (Mobile-First enhancement).max-width: 767px: Styles apply from 767px downward (Desktop-First constraint).@media (width >= 768px) and @media (600px <= width <= 900px) provide clean mathematical notation.and requires all conditions to match; comma , acts as or.@media print to hide navigation bars, buttons, and dark backgrounds when printing documents.<head> for CSS media queries to function properly on mobile devices?@media (min-width: 1024px)?OR operator between conditions?@media print stylesheet block?Create a responsive and printable School Student Identity Card:
width >= 768px), display the card horizontally with the student photo on the left and credentials on the right.width < 768px), stack the photo on top of the text details.@media print stylesheet that removes the dark background, hides the "Print ID Card" button, and centers a crisp, black-and-white card for physical printing!Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Test your understanding of this lesson with 5 questions. Each question has one correct answer.