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.