Min-Width and Max-Width Media Queries: Modern Range Syntax and Logic
Min-Width and Max-Width Media Queries: Modern Range Syntax and Logic
Think about your school's sports day trials:
- "Students with height of at least 150 cm (
min-width: 150cm) are eligible for the Senior High Jump." (Applies to 150cm, 160cm, 180cm and above). - "Students up to a maximum age of 12 years (
max-width: 12yr) compete in the Sub-Junior relay race." (Applies to 12, 11, 10 and below). - "Students between 130 cm and 150 cm (
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.
1. The Classic Syntax: min-width vs max-width
A media query wraps a block of CSS rules inside a conditional @media statement:
The Difference in Philosophy
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.
2. Modern CSS Media Queries Level 4: The Range Syntax
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.
3. Logical Operators: and, not, and the Comma (Or)
You can chain multiple conditions together using Boolean logic:
4. Media Types: Screen vs Print
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:
5. Common Mistakes to Avoid (Do's and Don'ts)
| 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. |
6. Quick Revision Summary (Cheat Sheet)
min-width: 768px: Styles apply from 768px upward (Mobile-First enhancement).max-width: 767px: Styles apply from 767px downward (Desktop-First constraint).- Level 4 Range Syntax:
@media (width >= 768px)and@media (600px <= width <= 900px)provide clean mathematical notation. - Logical Operators:
andrequires all conditions to match; comma,acts asor. - Print Stylesheets: Use
@media printto hide navigation bars, buttons, and dark backgrounds when printing documents.
Multiple Choice Questions
1. Which HTML tag is strictly required inside <head> for CSS media queries to function properly on mobile devices?
A. <meta name="robots" content="index"> B. <meta name="viewport" content="width=device-width, initial-scale=1.0"> C. <meta charset="UTF-8"> D. <link rel="responsive" href="style.css"> Answer: B Explanation: The viewport meta tag tells mobile browsers to match screen dimensions to CSS pixels at a 1:1 scale rather than simulating a 980px desktop screen.
2. Under modern CSS Media Queries Level 4, what is the shorthand equivalent of @media (min-width: 1024px)?
A. @media (width >= 1024px) B. @media (width == 1024px) C. @media (width =< 1024px) D. @media (screen: 1024px) Answer: A Explanation: Media Queries Level 4 allows standard comparison operators; width >= 1024px is identical to min-width: 1024px.
3. If you want styles to apply ONLY to screen sizes between 600px and 900px inclusive, which query is correct?
A. @media (min-width: 600px) or (max-width: 900px) B. @media (600px <= width <= 900px) C. @media (width: 600px to 900px) D. @media (min-width: 900px) and (max-width: 600px) Answer: B Explanation: Modern range syntax permits chaining minimum and maximum boundaries: (600px <= width <= 900px).
4. In a media query, which character acts as the logical OR operator between conditions?
A. Ampersand (&) B. Pipe (|) C. Comma (,) D. Plus (+) Answer: C Explanation: In CSS media queries, a comma-separated list of media queries acts as a logical OR (if either query evaluates to true, the block applies).
5. What is the primary purpose of writing an @media print stylesheet block?
A. To print text automatically onto the user's screen B. To format the webpage specifically for physical paper printing, hiding ads, menus, and dark backgrounds C. To accelerate page rendering by 200% D. To download a PDF copy to the desktop Answer: B Explanation: @media print targets the printed paper output, stripping away interactive navigation menus and background colors to conserve ink and ensure document readability.
7. Hands-on Practice Challenge: The School Student Identity Card
Create a responsive and printable School Student Identity Card:
- 1On desktop screens (
width >= 768px), display the card horizontally with the student photo on the left and credentials on the right. - 2On mobile screens (
width < 768px), stack the photo on top of the text details. - 3Include an
@media printstylesheet that removes the dark background, hides the "Print ID Card" button, and centers a crisp, black-and-white card for physical printing!
Mobile-First vs Desktop-First Approach: Clean Architecture and Overriding Rules
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Related Lessons
Practice Quiz
Test your understanding of this lesson with 5 questions. Each question has one correct answer.