Using CSS Variables
There are three ways to use CSS variables: in CSS, in the style
attribute, and via Tailwind.
Choosing between each method
You’ll often find yourself using CSS variables with the style
attribute because it’s convenient and usually sufficient.
For example, to build a 2-column grid, you don’t need to fish out and modify your CSS file. You can simply use the --cols
variable in the style
attribute.
To create a responsive grid, you need to change the --cols
variable at different breakpoints. Since you can’t use media queries in the style
attribute, you’ll need to choose between CSS or Tailwind.
Using CSS
Creating a responsive grid in CSS is straightforward. Simply write a media query and change the value of the --cols
variable.
The downside to this approach is that it requires more code, so we only recommend it if the Tailwind method becomes complex or unwieldy for you.
Using Tailwind
Tailwind allows you to use CSS variables with arbitrary values. To use a CSS variable, enclose your variable and property pair with []
. Then, write your CSS variable as you would in CSS, but without spaces.
Here’s an example of how this works in practice.
Here’s the best part.
Tailwind gives you the ability to change CSS at different breakpoints with their responsive variant, so you can change layouts on the fly.
Their variants take the following format:
To create a responsive grid, you can use built-in variants like md
(for 768px
) and lg
(for 1024px
) like this:
For the discerning frontend developer, you know you shouldn’t use device breakpoints — especially now that container queries have full browser support!
So we’ve added a range of media and container breakpoints (from 50px
to 2000px
) in our layout preset for you to choose from. Enjoy creating responsive layouts ❤️!