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.
What the smartest executive leaders are doing right now
July 25, 2024We are consistently seeing three things right now in terms of how executives are tackling the technological challenges they are facing.
Read moreSimplifying the 4 forces of AI
April 9, 2024Artificial Intelligence (AI) is becoming more prevalent, but less understood. Through examples of organizations leading the charge in each of these areas, I hope to empower you to make better decisions for your enterprise and career.
Read more