client:media helps your website react to screen size changes. It makes your site interactive only when needed, saving resources.
client:media for responsive interactivity in Astro
<Component client:media="(media-query)"></Component>Replace (media-query) with a CSS media query like (max-width: 600px).
The component inside loads only if the media query matches the current screen.
MobileMenu component only on screens 600px wide or smaller.<MobileMenu client:media="(max-width: 600px)" />DesktopGallery component only on screens 1024px wide or larger.<DesktopGallery client:media="(min-width: 1024px)" />Sidebar component only on medium-sized screens between 768px and 1200px.<Sidebar client:media="(min-width: 768px) and (max-width: 1200px)" />This example shows a header that loads a mobile menu on small screens and a desktop menu on larger screens. Only one menu loads depending on the screen size, improving performance and user experience.
--- import MobileMenu from '../components/MobileMenu.astro'; import DesktopMenu from '../components/DesktopMenu.astro'; --- <header> <MobileMenu client:media="(max-width: 600px)" /> <DesktopMenu client:media="(min-width: 601px)" /> </header>
client:media uses standard CSS media queries, so you can reuse what you know from CSS.
It helps reduce unnecessary JavaScript loading on devices that don't need it.
Test your media queries in browser DevTools by resizing the window to see the effect.
client:media loads components only when a media query matches.
It improves performance by avoiding loading unneeded code on certain screen sizes.
Use it to make your site responsive and interactive in a smart way.
Practice
client:media in Astro components?Solution
Step 1: Understand what
client:mediadoesclient:medialoads components conditionally based on media queries, meaning it only activates when the screen matches certain conditions.Step 2: Compare options with this behavior
To load components only when a specific media query matches this behavior exactly. Options B, C, and D describe unrelated or incorrect behaviors.Final Answer:
To load components only when a specific media query matches -> Option DQuick Check:
client:media loads components conditionally = A [OK]
- Thinking client:media preloads all components
- Confusing client:media with CSS styling
- Assuming it disables JavaScript entirely
client:media?Solution
Step 1: Recall correct media query syntax
Media queries require parentheses around conditions, e.g.,(min-width: 768px).Step 2: Match syntax with options
<Component client:media="(min-width: 768px)" /> uses correct parentheses and syntax. <Component client:media="min-width: 768px" /> misses parentheses. <Component client:media="screen and min-width: 768px" /> has incorrect syntax (missing parentheses around the condition). <Component client:media="(max-width: 768px)" /> uses max-width, which is the opposite condition.Final Answer:
<Component client:media="(min-width: 768px)" /> -> Option AQuick Check:
Correct media query syntax = B [OK]
- Omitting parentheses around media queries
- Using max-width instead of min-width by mistake
- Adding unnecessary keywords like 'screen and'
<MyWidget client:media="(max-width: 600px)" />
What happens when the screen width is 500px?
Solution
Step 1: Understand the media query condition
The media query(max-width: 600px)matches screen widths 600px or less.Step 2: Check screen width against condition
At 500px, the condition is true, so the component loads and runs on the client.Final Answer:
The MyWidget component loads and runs on the client -> Option BQuick Check:
Screen 500px matches max-width 600px = D [OK]
- Thinking component loads but does not run JavaScript
- Assuming component loads only on server
- Confusing max-width with min-width behavior
<Sidebar client:media="min-width: 1024px" />
Solution
Step 1: Check media query syntax
The media query must be wrapped in parentheses, e.g.,(min-width: 1024px).Step 2: Verify other parts
Component name capitalization is fine. Using client:media is correct for media queries. min-width is valid depending on intent.Final Answer:
Missing parentheses around the media query -> Option AQuick Check:
Media queries need parentheses = A [OK]
- Omitting parentheses in media queries
- Confusing client:media with client:load
- Changing media query type without reason
client:media attribute correctly achieves this?Solution
Step 1: Understand the range condition
You want the component to load only when the screen width is at least 600px and at most 1200px.Step 2: Analyze each option's media query
<NavigationMenu client:media="(min-width: 600px) and (max-width: 1200px)" /> correctly usesandto combine(min-width: 600px)and(max-width: 1200px). Options A and B have reversed or impossible ranges. <NavigationMenu client:media="(min-width: 600px) or (max-width: 1200px)" /> usesor, which matches too broadly.Final Answer:
<NavigationMenu client:media="(min-width: 600px) and (max-width: 1200px)" /> -> Option CQuick Check:
Use 'and' to combine min and max width = C [OK]
- Reversing min-width and max-width order
- Using 'or' instead of 'and' for range
- Creating impossible media query ranges
Ready to prove it?
Pass the test and this pattern moves unknown
