Beyond CSS Media Queries<\/h1>\nJuan Diego Rodr\u00edguez<\/address>\n 2024-05-16T15:00:00+00:00
\n 2025-03-19T12:04:52+00:00
\n <\/header>\n
Media queries have been around almost as long as CSS itself \u2014 and with no flex, no grid, no responsive units, and no math functions, media queries were the most pragmatic choice<\/strong> available to make a somewhat responsive website.<\/p>\nIn the early 2010s, with the proliferation of mobile devices and the timely publication of Ethan Marcotte\u2019s classic article \u201cResponsive Web Design\u201d<\/a>, media queries became much needed for crafting layouts that could morph across screens and devices. Even when the CSS Flexbox and Grid specifications rolled out, media queries for resizing never left.<\/p>\nWhile data on the actual usage of media queries is elusive<\/a>, the fact that they have grown over time<\/em> with additional features that go well beyond the viewport and into things like user preferences continues to make them a bellwether ingredient for responsive design<\/strong>.<\/p>\nToday, there are more options and tools in CSS for establishing layouts that allow page elements to adapt to many different conditions besides the size of the viewport. Some are more widely used \u2014 Flexbox and Grid for certain \u2014 but also things like responsive length units and, most notably, container queries<\/a>, a concept we will come back to in a bit.<\/p>\nBut media queries are still often the de facto<\/em> tool that developers reach for. Maybe it\u2019s muscle memory, inconsistent browser support, or that we\u2019re stuck in our ways, but adoption of the modern approaches we have for responsive interfaces seems slow to take off.<\/p>\nTo be clear, I am all for media queries<\/strong>. They play a significant role in the work we do above and beyond watching the viewport size to make better and more accessible user experiences based on a user\u2019s OS preferences, the type of input device they\u2019re using, and more.<\/p>\nBut should media queries continue to be the gold standard for responsive layouts? As always, it depends, but<\/p>\n
\n\n <\/p>\nIt is undeniable that media queries have evolved toward accessibility solutions, making space for other CSS features to take responsibility for responsiveness.<\/p>\n
<\/a>\n <\/p>\n
\n\n \u201c<\/span><\/div>\n<\/p><\/div>\n<\/blockquote>\nThe Problem With Media Queries<\/h2>\n
Media queries seemed like a great solution for most responsive-related problems, but as the web has grown towards bigger and more complex layouts, the limits of media queries are more prevalent than ever<\/strong>.<\/p>\nProblem #1: They Are Viewport-Focused<\/h3>\n
When writing media query breakpoints where we want the layout to adapt, we only have access to the viewport\u2019s properties, like width<\/code> or orientation<\/code>. Sometimes, all we need is to tweak a font size, and the viewport is our best bud for that, but most times, context is important<\/strong>.<\/p>\nComponents on a page share space with others and are positioned relative<\/em> to each other according to normal document flow. If all we have access to is the viewport width, knowing exactly where to establish a particular breakpoint becomes a task of compromises where some components will respond well to the adapted layout while others will need additional adjustments at that specific breakpoint.<\/p>\nSo, there we are, resizing our browser and looking for the correct breakpoint where our content becomes too squished.<\/p>\n
The following example probably has the worst CSS you will see in a while, but it helps to understand one of the problems with media queries.<\/p>\n\nSee the Pen [Old Media Queries [forked]](https:\/\/codepen.io\/smashingmag\/pen\/xxNwbob) by Monknow<\/a>.<\/p>See the Pen Old Media Queries [forked]<\/a> by Monknow<\/a>.<\/figcaption><\/figure>\nIt\u2019s a pretty embarrassing example, but why exactly is it bad?<\/p>\n
If we try to convert the CSS verbatim, it would say, When the page width is smaller than 600px<\/code>, these elements will grow and collapse.<\/em> This statement is completely agnostic to the element\u2019s contents or its siblings. What happens if there are more siblings or just one? Or what happens if the element is inside a smaller container? The media query simply lacks the information we need to account for these things, which leads us to a second problem with media queries.<\/p>\nProblem #2: They Are Difficult To Manage<\/h3>\n
Nowadays everything is a component. Like nomads, components wander from place to place, sharing space with other components and bringing along their ever-changing content. Again, media queries are completely unaware of the context surrounding a component, and the burden lies on the developer to find that sweet spot for each and every case.<\/p>\n
\n 2025-03-19T12:04:52+00:00
\n <\/header>\n
In the early 2010s, with the proliferation of mobile devices and the timely publication of Ethan Marcotte\u2019s classic article \u201cResponsive Web Design\u201d<\/a>, media queries became much needed for crafting layouts that could morph across screens and devices. Even when the CSS Flexbox and Grid specifications rolled out, media queries for resizing never left.<\/p>\n While data on the actual usage of media queries is elusive<\/a>, the fact that they have grown over time<\/em> with additional features that go well beyond the viewport and into things like user preferences continues to make them a bellwether ingredient for responsive design<\/strong>.<\/p>\n Today, there are more options and tools in CSS for establishing layouts that allow page elements to adapt to many different conditions besides the size of the viewport. Some are more widely used \u2014 Flexbox and Grid for certain \u2014 but also things like responsive length units and, most notably, container queries<\/a>, a concept we will come back to in a bit.<\/p>\n But media queries are still often the de facto<\/em> tool that developers reach for. Maybe it\u2019s muscle memory, inconsistent browser support, or that we\u2019re stuck in our ways, but adoption of the modern approaches we have for responsive interfaces seems slow to take off.<\/p>\n To be clear, I am all for media queries<\/strong>. They play a significant role in the work we do above and beyond watching the viewport size to make better and more accessible user experiences based on a user\u2019s OS preferences, the type of input device they\u2019re using, and more.<\/p>\n But should media queries continue to be the gold standard for responsive layouts? As always, it depends, but<\/p>\n \n <\/p>\n It is undeniable that media queries have evolved toward accessibility solutions, making space for other CSS features to take responsibility for responsiveness.<\/p>\n <\/a>\n <\/p>\n Media queries seemed like a great solution for most responsive-related problems, but as the web has grown towards bigger and more complex layouts, the limits of media queries are more prevalent than ever<\/strong>.<\/p>\n When writing media query breakpoints where we want the layout to adapt, we only have access to the viewport\u2019s properties, like Components on a page share space with others and are positioned relative<\/em> to each other according to normal document flow. If all we have access to is the viewport width, knowing exactly where to establish a particular breakpoint becomes a task of compromises where some components will respond well to the adapted layout while others will need additional adjustments at that specific breakpoint.<\/p>\n So, there we are, resizing our browser and looking for the correct breakpoint where our content becomes too squished.<\/p>\n The following example probably has the worst CSS you will see in a while, but it helps to understand one of the problems with media queries.<\/p>\n See the Pen [Old Media Queries [forked]](https:\/\/codepen.io\/smashingmag\/pen\/xxNwbob) by Monknow<\/a>.<\/p> It\u2019s a pretty embarrassing example, but why exactly is it bad?<\/p>\n If we try to convert the CSS verbatim, it would say, When the page width is smaller than Nowadays everything is a component. Like nomads, components wander from place to place, sharing space with other components and bringing along their ever-changing content. Again, media queries are completely unaware of the context surrounding a component, and the burden lies on the developer to find that sweet spot for each and every case.<\/p>\n\n
The Problem With Media Queries<\/h2>\n
Problem #1: They Are Viewport-Focused<\/h3>\n
width<\/code> or
orientation<\/code>. Sometimes, all we need is to tweak a font size, and the viewport is our best bud for that, but most times, context is important<\/strong>.<\/p>\n
600px<\/code>, these elements will grow and collapse.<\/em> This statement is completely agnostic to the element\u2019s contents or its siblings. What happens if there are more siblings or just one? Or what happens if the element is inside a smaller container? The media query simply lacks the information we need to account for these things, which leads us to a second problem with media queries.<\/p>\n
Problem #2: They Are Difficult To Manage<\/h3>\n