![Advanced Tailwind: Container Queries](https://optimise2.assets-servd.host/gratis-creeper/staging/blog/banner_2023-07-28-190954_vhsa.jpg?w=1660&h=1188&auto=compress%2Cformat&fit=crop&dm=1708017435&s=9f71445482ad7a76809a5489993d1f90)
TailÂwind proÂvides a lot of valÂue for stanÂdardÂizÂing design tokens and stylesheets. I’ve been using it in every web based project for the last year, and being able to share comÂpoÂnents between projects and domains withÂout restrucÂturÂing style sheets is an incredÂiÂble boost to proÂducÂtivÂiÂty. Tailwind’s media queries, more specifÂiÂcalÂly, are one of my favorite feaÂtures. With such ease you can define stanÂdardÂized style behavÂiors for difÂferÂent screen sizes directÂly in the className
props:
<div className="sm:col-2 md:col-4 lg:col-8"/>
TailÂwind proÂvides a list of aliasÂes for these queries, which are by default mapped to stanÂdard screen sizes. We can always extend this to match our own numÂbers, of course.
So where’s the probÂlem? This post isn’t about media queries, but conÂtainÂer queries. I conÂsidÂer conÂtainÂer queries to be an advanced techÂnique for TailÂwind as it involves using pluÂgÂins, and fits a speÂcifÂic use-case. Before I explain what a conÂtainÂer query is, let’s look at the probÂlem we’re tryÂing to solve that media queries cannot.
ConÂsidÂer a webÂsite that has a user-conÂtrolled, resizÂable panÂel which is used for side-by-side views. This is often used for cross-refÂerÂencÂing webÂsites where you can see mulÂtiÂple, comÂpleteÂly difÂferÂent pieces of conÂtent at the same time. I put togethÂer a bare-bones examÂple using NexÂtJs to illusÂtrate this below:
I creÂatÂed the panÂels using the react-resizÂable-panÂels library, and am just disÂplayÂing the default NexÂtJs landÂing comÂpoÂnent in both panels:
The issue is that the default NexÂtJs landÂing page includes media queries to hanÂdle mobile views! Why aren’t they being respectÂed when the panÂels are resized? Why do they look dumb when the panÂels are small? Look at the default code below and spot the media queries. They’re defÂiÂniteÂly there, but nothing’s happening!
This is simÂply because TailÂwind media queries are based on the browsÂer window’s size. They will nevÂer change unless the browsÂer winÂdow is changed. They don’t know anyÂthing about the panÂel sizes. How do we get a media query to monÂiÂtor the size of its parÂent div
, instead of the browsÂer winÂdow? This is where conÂtainÂer queries come in, because this is exactÂly what they’re for.
InstalÂlaÂtion #
ConÂtainÂer queries are a TailÂwind pluÂgÂin, so they need to be installed first by folÂlowÂing these steps:
npm install @tailwindcss/container-queries
- Add this to your
tailwind.config.js
file:plugins: [ require('@tailwindcss/container-queries'), // ... ],
Usage #
EssenÂtialÂly, we mark our media query aliasÂes with a new conÂtainÂer flag @container
, or if you want a unique name in the case of more comÂplex designs, you can write @container/{name}
:
This tells any child comÂpoÂnents that if their media queries are also flagged, they will monÂiÂtor only the parent’s size. See the @lg:flex
flag in the div
? The @
will make the lg:
alias track the @container
.
So with this new tech, we can just do a find-all on media queries in this comÂpoÂnent, and add the @
markÂer. It will begin to look like this:
After runÂning this now, we’ll see that our conÂtent inside the panÂels are now respectÂing the parÂent comÂpoÂnent sizes, and we didn’t have to rewrite our media queries at all! Aside from just adding a @
to each one.
I think this is amazÂing if you have a simÂiÂlar use-case, and makes conÂtainÂer queries incredÂiÂbly easy to impleÂment. The next time you have to deal with resizÂable panÂels, or even just child comÂpoÂnents that need a slightÂly more advanced twist on their styling, take a look at Tailwind’s conÂtainÂer query plugin!
You can check out the live examÂple of conÂtainÂer queries runÂning with the panÂels here: https://advanced-tailwind.vercel.app/
And the source code is here: https://​github​.com/​D​a​v​e​A​l​d​o​n​/​a​d​v​a​n​c​e​d​-​t​a​i​lwind
Looking for more like this?
Sign up for our monthly newsletter to receive helpful articles, case studies, and stories from our team.
![Business Model Canvas: Helping business leaders create meaningful digital products](https://optimise2.assets-servd.host/gratis-creeper/staging/blog/business-model-canvas-banner.png?w=2134&h=2134&auto=compress%2Cformat&fit=crop&dm=1708017446&s=831e9976fdf920fbe07f923fb3521f4c)
Business Model Canvas: Helping business leaders create meaningful digital products
January 17, 2024When it comes to custom software, it’s easy to feel stuck between needing to deliver value quickly, while also ensuring meaningful, long-lasting results. Learn why the Business Model Canvas can help you identify the right digital product for your business needs.
Read more![What to know about the cost of custom app development](https://optimise2.assets-servd.host/gratis-creeper/staging/blog/Custom-app_web-thumbnail-1080x1080-px.png?w=1080&h=1080&auto=compress%2Cformat&fit=crop&dm=1708017468&s=6c2a9c525af4407f593d1e29638dee31)
What to know about the cost of custom app development
January 10, 2024We hear a lot of ideas for apps at MichiganLabs. People from large enterprises and small startups, located all over the world, call us to explore their mobile and web-based application ideas, and one of the first questions they ask is: How much is this app going to cost?
Read more![Product Strategy](https://optimise2.assets-servd.host/gratis-creeper/staging/blog/100_0U0A9231.jpg?w=696&h=320&q=75&auto=format&fit=crop&dm=1708017367&s=bbb2dc56ee989f50ad87c05cb00f9933)
Product Strategy
November 22, 2022A look at Product Strategy at MichiganLabs. Why we do it, what it is, what it is not, and how we approach it.
Read more