Introduction to Media Queries
Learn the @media syntax to apply different CSS rules at different screen widths.
What are media queries?
Relative units alone can't solve every responsive problem. Sometimes you need to make a dramatic layout change at a certain screen size — switch from a sidebar layout to a stacked layout, hide a navigation menu and show a hamburger icon, or change from a 3-column grid to a single column. Media queries let you write CSS rules that only apply when certain conditions are met — most commonly, when the browser window is a certain width. Think of them as 'if statements' for CSS: 'If the screen is at least 768px wide, use this layout. Otherwise, use that layout.'
When you visit Instagram on your phone, you see a single-column feed. On a desktop, you see the feed plus a sidebar with suggestions. Instagram doesn't serve a different website — it uses media queries to show different layouts based on the screen width. Every major website uses media queries: Google rearranges its search results page, YouTube switches from a grid to a stacked layout, and Amazon reorganizes its product pages.