What Are CSS Media Queries?
CSS media queries allow you to apply CSS rules conditionally based on characteristics of the environment in which a page is being displayed. They are one of the most important tools for building responsive websites.
The most common use is checking the available viewport width and changing the layout when the current design no longer fits comfortably. However, media queries are not limited to screen width. They can also respond to characteristics such as orientation, hover capability, pointer type, display mode, resolution, color scheme, reduced-motion preferences, and other user-environment features. ([developer.mozilla.org](https://developer.mozilla.org/en-US/docs/Web/CSS/Guides/Media_queries/Using))
Media queries therefore let CSS answer questions such as: Is the viewport narrow? Is the page being printed? Does the user prefer dark mode? Can the primary input device hover? and Is the viewport in portrait orientation?
What Is the Basic Syntax of a CSS Media Query?
The basic @media syntax looks like this:
@media (max-width: 768px) {
.navigation {
display: none;
}
}
The CSS inside the block is applied only when the media query evaluates to true. A media query can contain an optional media type and one or more media feature expressions connected with logical operators. ([developer.mozilla.org](https://developer.mozilla.org/en-US/docs/Web/CSS/Guides/Media_queries/Using))
What Parts Make Up a Media Query?
@media
The at-rule used to conditionally apply CSS.
Media Type
Broad output category such as screen or print.
Media Feature
A characteristic such as width, height, orientation, or hover capability.
CSS Rules
The declarations applied when the condition matches.
@media screen and (max-width: 768px) {
.layout {
grid-template-columns: 1fr;
}
}
What Are CSS Media Types?
Media types provide a broad classification of the output environment.
Common media types include all,
screen, and
print. ([developer.mozilla.org](https://developer.mozilla.org/en-US/docs/Web/CSS/Guides/Media_queries/Using))
Applies to all media types. It is the default when no media type is specified.
Targets screen-based output such as a normal browser display.
Targets paged or print output.
In many responsive layouts, you do not need to explicitly write
screen because leaving the type out means the
query applies to all media types for which its conditions are true.
How Do Media Queries Work with Viewport Width?
Viewport width is one of the most common media features used in responsive web design.
</pre>
<p class="mt-5">
The first query applies at widths of 768 pixels or less. The second applies at widths
of 769 pixels or more.
</p>
<p class="mt-4">
Width-based breakpoints should be chosen according to where the design needs to change,
rather than because a particular device is commonly associated with a certain number.
</p>
</section>
<!-- Range Syntax -->
<section>
<h2 class="text-2xl md:text-3xl font-bold text-white mb-4">
Can You Write Media Query Ranges Without min-width and max-width?
</h2>
<p class="mb-4">
Yes. Modern media query syntax supports range comparisons such as
<code class="text-blue-300">width >= 768px</code> and
<code class="text-blue-300">width < 1200px</code>.
</p>
<pre class="bg-slate-950 border border-slate-700 rounded-xl p-5 overflow-x-auto text-sm"><code>@media (width >= 768px) and (width < 1200px) {
.layout {
grid-template-columns: 1fr 1fr;
}
}
This range syntax can make more complex conditions easier to read because the comparison
is written directly as a range. Traditional
min-width and
max-width syntax remains common and useful as well.
([developer.mozilla.org](https://developer.mozilla.org/en-US/docs/Web/CSS/Guides/Media_queries/Using))
How Do Media Queries Support Mobile-First CSS?
In a mobile-first approach, the base stylesheet describes the compact layout and media queries progressively enhance the interface as more space becomes available.
</pre>
<p class="mt-5">
This style of responsive CSS starts with a layout that works in a narrow space and
adds columns when the container has enough room.
</p>
</section>
<!-- Breakpoints -->
<section>
<h2 class="text-2xl md:text-3xl font-bold text-white mb-4">
How Should You Choose Responsive Breakpoints?
</h2>
<p class="mb-4">
A breakpoint should usually be based on the behavior of the content rather than the
name of a device.
</p>
<div class="grid md:grid-cols-3 gap-5 mt-6">
<div class="rounded-xl border border-slate-700 bg-slate-900 p-5">
<div class="font-mono text-blue-300 mb-2">
Content Fits
</div>
<p class="text-sm">
Keep the existing layout when the current arrangement is still comfortable.
</p>
</div>
<div class="rounded-xl border border-slate-700 bg-slate-900 p-5">
<div class="font-mono text-blue-300 mb-2">
Layout Breaks
</div>
<p class="text-sm">
Introduce a breakpoint when elements become crowded, overflow, or become difficult to use.
</p>
</div>
<div class="rounded-xl border border-slate-700 bg-slate-900 p-5">
<div class="font-mono text-blue-300 mb-2">
Layout Changes
</div>
<p class="text-sm">
Reorganize the component instead of simply shrinking every dimension.
</p>
</div>
</div>
<p class="mt-5">
Responsive breakpoints are design decisions. There is no universal set of breakpoint
values that every website must use.
</p>
</section>
<!-- Orientation -->
<section>
<h2 class="text-2xl md:text-3xl font-bold text-white mb-4">
How Does the orientation Media Feature Work?
</h2>
<p class="mb-4">
The <code class="text-blue-300">orientation</code> media feature checks the orientation
of the viewport and supports <code class="text-blue-300">portrait</code> and
<code class="text-blue-300">landscape</code>. ([developer.mozilla.org](https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/At-rules/%40media/orientation))
</p>
<pre class="bg-slate-950 border border-slate-700 rounded-xl p-5 overflow-x-auto text-sm"><code:@media (orientation: landscape) {
.hero {
min-height: 60vh;
}
}
@media (orientation: portrait) {
.hero {
min-height: 45vh;
}
}
Orientation describes the viewport rather than simply identifying the physical device. For example, browser UI such as a software keyboard can change the viewport dimensions, which can affect the reported orientation. ([developer.mozilla.org](https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/At-rules/%40media/orientation))
How Do You Use Media Queries for Print Styles?
Media queries are useful for making pages more suitable for printing.
</pre>
<p class="mt-5">
A print stylesheet can remove navigation and interactive controls while allowing the
actual content to use the printable page more effectively.
</p>
<p class="mt-4">
You can also target print orientation:
</p>
<pre class="mt-4 bg-slate-950 border border-slate-700 rounded-xl p-5 overflow-x-auto text-sm"><code:@media print and (orientation: landscape) {
.report {
font-size: 10pt;
}
}
What Logical Operators Can Be Used in Media Queries?
Media queries can be combined with logical operators to create more precise conditions.
Common logical operators include and,
not, and
only. Multiple comma-separated queries act as alternatives.
([developer.mozilla.org](https://developer.mozilla.org/en-US/docs/Web/CSS/Guides/Media_queries/Using))
All conditions in that media query must match.
Inverts the meaning of a media query.
Restricts the media query to matching the specified media condition.
How Does the and Operator Work?
Use and when multiple conditions must be true.
= 768px) and (orientation: landscape) {
.dashboard {
grid-template-columns: 240px 1fr;
}
}
The rules apply only when the output is a screen, the viewport is at least 768 pixels wide, and the viewport is in landscape orientation.
How Do You Express OR Logic in Media Queries?
Separate multiple media queries with commas when any one of them should match.
= 900px), (orientation: landscape) {
.hero {
padding: 4rem;
}
}
The rule applies when either the viewport is at least 900 pixels wide or the viewport is in landscape orientation. A comma-separated media query list matches when at least one of its individual queries matches. ([developer.mozilla.org](https://developer.mozilla.org/en-US/docs/Web/CSS/Guides/Media_queries/Using))
Can Media Queries Detect Hover and Pointer Capabilities?
Yes. Media features such as hover,
any-hover,
pointer, and
any-pointer can be used to adapt interactions
to different input capabilities. ([developer.mozilla.org](https://developer.mozilla.org/en-US/docs/Web/CSS/Guides/Media_queries/Using))
</pre>
<p class="mt-5">
This can be preferable to assuming that every user has a mouse or that every interface
can rely on hover interactions.
</p>
<p class="mt-4">
You can also inspect pointer precision:
</p>
<pre class="mt-4 bg-slate-950 border border-slate-700 rounded-xl p-5 overflow-x-auto text-sm"><code:@media (pointer: coarse) {
.button {
padding: 0.9rem 1rem;
}
}
How Does prefers-color-scheme Work?
The prefers-color-scheme media feature allows CSS
to respond to whether the user has requested a light or dark color scheme. ([developer.mozilla.org](https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/At-rules/%40media/prefers-color-scheme))
</pre>
<p class="mt-5">
This allows a page to adapt to the user's system or user-agent color preference without
requiring JavaScript just to detect the preference.
</p>
</section>
<!-- Reduced Motion -->
<section>
<h2 class="text-2xl md:text-3xl font-bold text-white mb-4">
How Can Media Queries Respect Reduced-Motion Preferences?
</h2>
<p class="mb-4">
The <code class="text-blue-300">prefers-reduced-motion</code> feature can be used
to reduce or disable motion when the user has requested less animation.
</p>
<pre class="bg-slate-950 border border-slate-700 rounded-xl p-5 overflow-x-auto text-sm"><code:@media (prefers-reduced-motion: reduce) {
*,
*::before,
*::after {
animation-duration: 0.01ms;
animation-iteration-count: 1;
transition-duration: 0.01ms;
scroll-behavior: auto;
}
}
Reduced-motion support is an accessibility consideration. It should be applied carefully so that important feedback remains understandable even when decorative movement is removed.
Can Media Queries Detect Screen Resolution?
The resolution feature can test output pixel density.
For example:
</pre>
<p class="mt-5">
Resolution queries are useful in specialized cases, but ordinary responsive layout
decisions usually depend more directly on available space than on pixel density. ([developer.mozilla.org](https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/At-rules/%40media/resolution))
</p>
</section>
<!-- Height -->
<section>
<h2 class="text-2xl md:text-3xl font-bold text-white mb-4">
Can Media Queries Respond to Viewport Height?
</h2>
<p class="mb-4">
Yes. The <code class="text-blue-300">height</code> feature can be used in the same way
as width.
</p>
<pre class="bg-slate-950 border border-slate-700 rounded-xl p-5 overflow-x-auto text-sm"><code:@media (max-height: 600px) {
.hero {
padding-block: 2rem;
}
}
Height queries can be helpful for interfaces that need to adapt when vertical space is limited, such as dialogs, dashboards, navigation panels, or large hero sections.
How Does aspect-ratio Work in a Media Query?
The aspect-ratio feature allows CSS to respond to
the relationship between viewport width and height.
16 / 9) {
.hero-content {
max-width: 900px;
}
}
This can be useful when the shape of the available viewport matters more than a single width value.
Can You Combine Width, Orientation and Preferences?
Yes. Complex interfaces can combine multiple media features when the design genuinely needs environment-specific behavior.
</pre>
<p class="mt-5">
Avoid creating complicated queries simply because the syntax allows it. Every condition
should have a clear purpose in the interface.
</p>
</section>
<!-- Container Query Distinction -->
<section>
<h2 class="text-2xl md:text-3xl font-bold text-white mb-4">
Are Media Queries the Same as Container Queries?
</h2>
<p class="mb-4">
No. The two techniques solve related but different problems.
</p>
<div class="grid md:grid-cols-2 gap-6">
<div class="rounded-xl border border-slate-700 bg-slate-900 p-6">
<h3 class="text-lg font-semibold text-white mb-3">
Media Queries
</h3>
<p>
Respond to the user environment, commonly the viewport and its features.
</p>
<pre class="mt-4 bg-slate-950 border border-slate-700 rounded-xl p-4 overflow-x-auto text-sm"><code>@media (width >= 768px) {
.component {
...
}
}
Container Queries
Respond to the size or features of a component's containing context.
= 500px) {
.component {
...
}
}
Media queries are often appropriate for page-level responsive behavior, while container queries can be valuable for reusable components whose layout depends on the space assigned by their parent.
Can Media Queries Be Used Outside @media?
Yes. Media queries can also be used in HTML attributes such as
media on elements like
<link>,
<style>, and
<source>, and they can be used with responsive
image-related features. ([developer.mozilla.org](https://developer.mozilla.org/en-US/docs/Web/CSS/Guides/Media_queries/Using))
</pre>
<p class="mt-5">
This tells the browser that the linked stylesheet is intended for print media.
</p>
</section>
<!-- JavaScript -->
<section>
<h2 class="text-2xl md:text-3xl font-bold text-white mb-4">
Can JavaScript Use CSS Media Queries?
</h2>
<p class="mb-4">
JavaScript can test media query state with
<code class="text-blue-300">window.matchMedia()</code>.
</p>
<pre class="bg-slate-950 border border-slate-700 rounded-xl p-5 overflow-x-auto text-sm"><code:const query = window.matchMedia('(width <= 768px)');
console.log(query.matches);
You can also listen for changes:
{
console.log('Matches:', event.matches);
});
Use CSS media queries for visual styling whenever possible. JavaScript media-query detection is useful when application behavior genuinely needs to respond to an environment change.
How Do You Build a Responsive Navigation with Media Queries?
<header class="header">
<a href="/" class="logo">IgKnight</a>
<nav class="desktop-nav">
<a href="/docs">Documentation</a>
<a href="/practice">Practice</a>
<a href="/compiler">Compiler</a>
</nav>
<button class="menu-button">
Menu
</button>
</header>
</pre>
<p class="mt-5">
This is a common responsive pattern: the base layout supports the larger arrangement,
and the media query changes visibility when the available width becomes constrained.
</p>
</section>
<!-- Responsive Dashboard -->
<section>
<h2 class="text-2xl md:text-3xl font-bold text-white mb-4">
How Do You Make a Dashboard Responsive with Media Queries?
</h2>
<p class="mb-4">
Dashboards often need a sidebar on larger screens and a simpler one-column structure
on smaller screens.
</p>
<pre class="bg-slate-950 border border-slate-700 rounded-xl p-5 overflow-x-auto text-sm"><code:.dashboard {
display: grid;
grid-template-columns:
240px minmax(0, 1fr);
gap: 2rem;
}
@media (max-width: 768px) {
.dashboard {
grid-template-columns: 1fr;
}
.sidebar {
display: none;
}
}
The important point is that the media query changes the layout structure when the two-column version is no longer suitable.
How Do Media Queries Improve Responsive Forms?
</pre>
<p class="mt-5">
A two-column form can save vertical space on larger screens, while a single-column form
can be easier to use and scan on smaller screens.
</p>
</section>
<!-- Too Many Breakpoints -->
<section>
<h2 class="text-2xl md:text-3xl font-bold text-white mb-4">
Should You Add a Media Query for Every Screen Width?
</h2>
<p class="mb-4">
No. Excessive media queries can make a stylesheet difficult to understand and maintain.
</p>
<p class="mb-4">
Before adding another breakpoint, ask whether the layout can already adapt using:
</p>
<div class="grid md:grid-cols-2 gap-4">
<div class="rounded-xl border border-slate-700 bg-slate-900 p-5">
<code class="text-blue-300">flex-wrap</code>
<p class="text-sm text-slate-400 mt-2">
Let Flexbox move items onto new lines.
</p>
</div>
<div class="rounded-xl border border-slate-700 bg-slate-900 p-5">
<code class="text-blue-300">minmax()</code>
<p class="text-sm text-slate-400 mt-2">
Make Grid tracks flexible within defined limits.
</p>
</div>
<div class="rounded-xl border border-slate-700 bg-slate-900 p-5">
<code class="text-blue-300">clamp()</code>
<p class="text-sm text-slate-400 mt-2">
Make typography and spacing change fluidly.
</p>
</div>
<div class="rounded-xl border border-slate-700 bg-slate-900 p-5">
<code class="text-blue-300">auto-fit</code>
<p class="text-sm text-slate-400 mt-2">
Allow repeated Grid tracks to adapt automatically.
</p>
</div>
</div>
<p class="mt-5">
Media queries are most valuable when the design actually needs a structural or behavioral
change that flexible layout techniques cannot express clearly.
</p>
</section>
<!-- Common Mistakes -->
<section>
<h2 class="text-2xl md:text-3xl font-bold text-white mb-4">
What Are Common CSS Media Query Mistakes?
</h2>
<div class="space-y-5">
<div class="rounded-xl border border-red-900/50 bg-red-950/20 p-5">
<h3 class="text-lg font-semibold text-white mb-2">
Using Device Names Instead of Layout Conditions
</h3>
<p>
CSS does not need to know that a visitor is using a specific phone model.
The more useful question is whether the current layout has enough available space.
</p>
</div>
<div class="rounded-xl border border-red-900/50 bg-red-950/20 p-5">
<h3 class="text-lg font-semibold text-white mb-2">
Adding Too Many Breakpoints
</h3>
<p>
A large collection of overlapping breakpoints often indicates that the underlying
layout needs more flexible sizing.
</p>
</div>
<div class="rounded-xl border border-red-900/50 bg-red-950/20 p-5">
<h3 class="text-lg font-semibold text-white mb-2">
Forgetting the Base Layout
</h3>
<p>
Media queries only override or add rules. A responsive design still needs a sensible
base stylesheet.
</p>
</div>
<div class="rounded-xl border border-red-900/50 bg-red-950/20 p-5">
<h3 class="text-lg font-semibold text-white mb-2">
Using max-width Everywhere
</h3>
<p>
Mobile-first styles often work naturally with min-width queries, but neither approach
is universally required. Choose the model that keeps your CSS understandable.
</p>
</div>
<div class="rounded-xl border border-red-900/50 bg-red-950/20 p-5">
<h3 class="text-lg font-semibold text-white mb-2">
Ignoring Intermediate Widths
</h3>
<p>
A layout may look correct at a desktop and mobile preset but break between those values.
Resize continuously instead of checking only a few fixed sizes.
</p>
</div>
<div class="rounded-xl border border-red-900/50 bg-red-950/20 p-5">
<h3 class="text-lg font-semibold text-white mb-2">
Hiding Important Content
</h3>
<p>
A mobile layout should preserve important information and functionality whenever
practical. Reorganize content rather than automatically removing it.
</p>
</div>
</div>
</section>
<!-- Debugging -->
<section>
<h2 class="text-2xl md:text-3xl font-bold text-white mb-4">
How Do You Debug CSS Media Queries?
</h2>
<div class="space-y-4">
<div class="rounded-xl border border-slate-700 bg-slate-900 p-5">
<h3 class="font-semibold text-white mb-2">
1. Check the current viewport size
</h3>
<p>
Confirm that the environment actually satisfies the media condition.
</p>
</div>
<div class="rounded-xl border border-slate-700 bg-slate-900 p-5">
<h3 class="font-semibold text-white mb-2">
2. Inspect the media query
</h3>
<p>
Browser DevTools show whether the media query currently matches and which declarations are active.
</p>
</div>
<div class="rounded-xl border border-slate-700 bg-slate-900 p-5">
<h3 class="font-semibold text-white mb-2">
3. Check the cascade
</h3>
<p>
A matching media query does not automatically win. Specificity, cascade layers,
source order, and other rules can still affect the final style.
</p>
</div>
<div class="rounded-xl border border-slate-700 bg-slate-900 p-5">
<h3 class="font-semibold text-white mb-2">
4. Test intermediate widths
</h3>
<p>
Look for the point where the current layout begins to break rather than checking only device presets.
</p>
</div>
<div class="rounded-xl border border-slate-700 bg-slate-900 p-5">
<h3 class="font-semibold text-white mb-2">
5. Check ancestor constraints
</h3>
<p>
A component can still look broken even when its media query matches if parent sizing or overflow creates the real problem.
</p>
</div>
</div>
</section>
<!-- Accessibility -->
<section>
<h2 class="text-2xl md:text-3xl font-bold text-white mb-4">
How Can Media Queries Improve Accessibility?
</h2>
<p class="mb-4">
Media queries can respond to user preferences and input capabilities, not only viewport dimensions.
This makes them useful for accessible interfaces.
</p>
<pre class="bg-slate-950 border border-slate-700 rounded-xl p-5 overflow-x-auto text-sm"><code:@media (prefers-reduced-motion: reduce) {
.animated-panel {
animation: none;
transition: none;
}
}
They can also be useful for dark and light color preferences, contrast-related behavior, forced-colors environments, hover support, pointer capabilities, and other conditions supported by the media query system. ([developer.mozilla.org](https://developer.mozilla.org/en-US/docs/Web/CSS/Guides/Media_queries))
Can HTML Use Media Queries to Choose Different Images?
Yes. HTML media conditions can be used with responsive image techniques such as
the <picture> element and its
<source media> conditions.
<picture>
<source
media="(max-width: 700px)"
srcset="mobile-banner.jpg"
>
<img
src="desktop-banner.jpg"
alt="Learning platform banner"
>
</picture>
This lets the browser choose an appropriate image source according to the conditions specified in the markup.
What Is a Complete CSS Media Query Example?
The following example combines mobile-first layout, navigation changes, responsive cards, orientation handling, and reduced-motion preferences.
<main class="page">
<header class="header">
<a href="/" class="logo">IgKnight</a>
<nav class="navigation">
<a href="/docs">Documentation</a>
<a href="/practice">Practice</a>
<a href="/compiler">Compiler</a>
</nav>
<button class="menu-button">
Menu
</button>
</header>
<section class="hero">
<h1>Learn Web Development</h1>
<p>
Practical documentation and developer tools.
</p>
</section>
<section class="cards">
<article class="card">Documentation</article>
<article class="card">Practice</article>
<article class="card">Compiler</article>
</section>
</main>
Notice that the media queries are used only where the layout genuinely changes.
Flexible Grid sizing and clamp() already handle
some of the fluid behavior without additional breakpoints.
What Are the Best Practices for CSS Media Queries?
Start with a flexible base layout
Use normal flow, Flexbox, Grid, and relative sizing before adding many media queries.
Base breakpoints on content
Change the layout where the design needs to change, not because a specific device has a particular name.
Prefer mobile-first when it keeps the CSS simpler
Start with the compact layout and progressively add features for larger available space.
Use preference queries thoughtfully
Respect settings such as reduced motion and preferred color scheme when they affect the experience.
Keep media queries readable
Group related responsive rules and avoid deeply complicated conditions without a clear reason.
Test real widths
Resize the viewport and test intermediate sizes, not only a few predefined device dimensions.
CSS Media Query Features at a Glance
| Feature | Example | Typical Purpose |
|---|---|---|
| width | width >= 768px | Responsive layout changes. |
| height | max-height: 600px | Limited vertical space. |
| orientation | orientation: landscape | Viewport orientation changes. |
| hover | hover: hover | Whether primary input can hover. |
| pointer | pointer: coarse | Pointer precision/capability. |
| prefers-color-scheme | prefers-color-scheme: dark | User color preference. |
| prefers-reduced-motion | prefers-reduced-motion: reduce | Reduced-motion preference. |
| resolution | min-resolution: 2dppx | Output pixel density. |
| aspect-ratio | aspect-ratio > 16 / 9 | Viewport shape. |
Key Takeaways
- Media queries conditionally apply CSS based on the current environment.
- The @media at-rule is the most common way to write CSS media queries.
- width and height are common features for responsive layouts.
- min-width and max-width remain common, while modern range syntax can express direct comparisons such as width >= 768px.
- orientation can distinguish portrait and landscape viewport states.
- Media queries can respond to hover and pointer capabilities, not only screen dimensions.
- Preference queries such as prefers-color-scheme and prefers-reduced-motion can improve accessibility and personalization.
- Commas create OR-style alternatives, while and combines conditions that must all match.
- Responsive breakpoints should be based on content and layout behavior, not only device names.
- Use flexible CSS techniques such as Flexbox, Grid, minmax(), auto-fit, and clamp() before creating large numbers of breakpoints.
- Media queries and container queries solve related but different responsive problems.