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.
The impact of tech stack choices on software ROI
October 30, 2024Choosing the right tech stack is key to optimizing software performance, scalability, and maintenance costs. Learn how we work side-by-side with our clients to make sure their tech stack choices line up with their business goals, budget, and long-term vision.
Read moreWeb app vs. mobile app: How to decide which is best for your business
March 26, 2024When considering whether to develop a web app or a mobile app for your business, there’s honestly no definitive answer. But this article will help you make an informed decision that aligns with your business goals and sets you up for success.
Read moreWhy I use NextJS
December 21, 2022Is NextJS right for your next project? In this post, David discusses three core functionalities that NextJS excels at, so that you can make a well-informed decision on your project’s major framework.
Read more