Media queries have been round nearly so long as CSS itself — and with no flex, no grid, no responsive items, and no math capabilities, media queries have been essentially the most pragmatic alternative out there to make a considerably responsive web site.
Within the early 2010s, with the proliferation of cellular gadgets and the well timed publication of Ethan Marcotte’s traditional article “Responsive Internet Design”, media queries turned a lot wanted for crafting layouts that would morph throughout screens and gadgets. Even when the CSS Flexbox and Grid specs rolled out, media queries for resizing by no means left.
Whereas information on the precise utilization of media queries is elusive, the truth that they’ve grown over time with further options that go effectively past the viewport and into issues like person preferences continues to make them a bellwether ingredient for responsive design.
Immediately, there are extra choices and instruments in CSS for establishing layouts that permit web page parts to adapt to many alternative circumstances apart from the scale of the viewport. Some are extra broadly used — Flexbox and Grid for sure — but in addition issues like responsive size items and, most notably, container queries, an idea we are going to come again to in a bit.
However media queries are nonetheless typically the de facto instrument that builders attain for. Possibly it’s muscle reminiscence, inconsistent browser assist, or that we’re caught in our methods, however adoption of the fashionable approaches we’ve got for responsive interfaces appears gradual to take off.
To be clear, I’m all for media queries. They play a major function within the work we do above and past watching the viewport dimension to make higher and extra accessible person experiences primarily based on a person’s OS preferences, the kind of enter gadget they’re utilizing, and extra.
However ought to media queries proceed to be the gold normal for responsive layouts? As at all times, it relies upon, however
It’s simple that media queries have developed towards accessibility options, making house for different CSS options to take accountability for responsiveness.
The Drawback With Media Queries
Media queries appeared like a terrific resolution for many responsive-related issues, however as the online has grown in direction of greater and extra complicated layouts, the boundaries of media queries are extra prevalent than ever.
Drawback #1: They Are Viewport-Targeted
When writing media question breakpoints the place we would like the format to adapt, we solely have entry to the viewport’s properties, like width or orientation. Generally, all we’d like is to tweak a font dimension, and the viewport is our greatest bud for that, however most instances, context is necessary.
Elements on a web page share house with others and are positioned relative to one another in keeping with regular doc move. If all we’ve got entry to is the viewport width, realizing precisely the place to ascertain a specific breakpoint turns into a process of compromises the place some elements will reply effectively to the tailored format whereas others will want further changes at that particular breakpoint.
So, there we’re, resizing our browser and on the lookout for the right breakpoint the place our content material turns into too squished.
The next instance in all probability has the worst CSS you will note shortly, nevertheless it helps to know one of many issues with media queries.
That very same format in cellular merely doesn’t work. Tables have their very own set of responsive challenges as it’s, and whereas there’s no scarcity of options, we could possibly contemplate one other format utilizing fashionable strategies which can be method much less engineered.
We’re doing rather more than merely altering the width or peak of parts! Border colours, ingredient visibility, and flex instructions have to be modified, and it will probably solely be performed by a media question, proper? Properly, even in instances the place we’ve got to fully swap a format relying on the viewport dimension, we will higher obtain it with container queries.
Once more, Drawback #1 of media queries is that they solely contemplate the viewport dimension when making choices and are fully blind to a component’s surrounding context.
That might not be an enormous concern if all we’re speaking about is a collection of parts which can be allowed to take up the complete web page width as a result of the complete web page width could be very a lot associated to the viewport dimension, making media queries a superbly fantastic alternative for making changes.
See the Pen Responsive Playing cards Utilizing Media Queries [forked] by Monknow.
However say we need to show those self same parts as a part of a multi-column format the place they’re included in a slender column as an <apart> subsequent to a bigger column containing a <important> ingredient. Now we’re in bother.
A extra conventional resolution is to put in writing a collection of media queries relying on the place the ingredient is used and the place its content material breaks. However media queries fully miss the connection between the <important> and <apart> parts, which is an enormous deal because the dimension of 1 influences the scale of the opposite in keeping with regular doc move.
See the Pen Responsive Playing cards Utilizing Media Queries Inside Container [forked] by Monknow.
The .playing cards ingredient is within the context of the <apart> ingredient and is squished on account of being in a slender column. What can be nice is to vary the format of every .card part when the .playing cards ingredient that accommodates them reaches a sure dimension slightly than when the viewport is a sure dimension.
That’s the place container queries come into play, permitting us to conditionally apply kinds primarily based on a component’s dimension. We register a component as a “container,” which, in our present instance, is the unordered listing containing the collection of .card elements. We’re primarily giving the father or mother selector an excessive amount of energy to affect the present format.
.playing cards {
container-name: playing cards;
}
Container queries monitor a component by its dimension, and we have to inform the browser precisely find out how to interpret that dimension by giving .playing cards a container-type, which could be the container’s dimension (i.e., within the block and inline instructions) or its inline-size (i.e., the full size within the inline route). There’s a standard worth that removes sizing concerns however permits the ingredient to be queried by its kinds.
.playing cards {
container-name: playing cards;
container-type: inline-size;
}
We are able to simplify issues down a bit utilizing the container shorthand property.
.playing cards {
container: playing cards / inline-size;
}
Now, we will regulate the format of the .card elements when the .playing cards container is a sure inline dimension. Container queries use the identical syntax as media queries however use the @container at-rule as a substitute of @media.
.playing cards {
container: playing cards / inline-size;
}
@container playing cards (width < 700px) {
.playing cards li {
flex-flow: column;
}
}
Now, every .card is a versatile container that flows within the column route when the width of the .playing cards container is lower than 700px. Any wider than that, we’ve got the identical to put them out in a row route as a substitute.
See the Pen Responsive Playing cards Utilizing Container Queries [forked] by Monknow.
Model queries are a cousin to container queries within the sense that we will question the container’s kinds and conditionally apply model modifications to its kids, say altering a toddler ingredient’s coloration to white when the container’s background-color is about to a darkish coloration. We’re nonetheless within the early days, and assist for model queries and browser assist remains to be evolving.
I hope this offers you a way of how wonderful it’s that we’ve got this context-aware method of building responsive layouts. Containers are a very new thought in CSS (though we’ve used the time period synonymously with “father or mother ingredient” for ages) that’s novel and chic.
So, Are Media Queries Ineffective?
NO! Whereas media queries have been the go-to resolution for responsive design, their limitations are manifestly apparent now that we’ve got extra sturdy instruments in CSS which can be designed to unravel these limits.
That doesn’t make media queries out of date — merely a unique instrument that’s half of a bigger toolset for constructing responsive interfaces. Apart from, media queries nonetheless handle important accessibility issues because of their skill to acknowledge a person’s visible and movement preferences — amongst different settings — on the working system stage.
So, sure, hold utilizing media queries! However possibly attain for them sparingly since CSS has much more to supply us.
Subscribe to MarketingSolution.
Receive web development discounts & web design tutorials.
Now! Lets GROW Together!