DEV Community

Cover image for Arbitrary Styles With Tailwind JIT
Martins Onuoha
Martins Onuoha

Posted on

Arbitrary Styles With Tailwind JIT

This article was first posted on devjavu.space

Tailwind is a utility first CSS framework that gives developers full control over styling web components. Unlike component frameworks or libraries, Tailwind doesn’t restrict you to preexisting components that are tweakable via props and options, with Tailwind, you build from the ground up, using utility classes in their smallest form.

Sheesh! But I’d rather have someone else do the job, I like component libraries better. Well, there’s Headless UI — completely unstyled UI components that integrates well with Tailwind CSS (their words, not mine).

With the announcement of one of Tailwind's - Tailwind JIT, Tailwind got even better. Let’s see practically how to use this feauture.


With Tailwind, we would usually start out by setting up our themes and variants within the tailwind.config.js file. Something like this:

module.exports = {
  theme: {
    colors: {
      gray: '#CFD8DC',
      blue: '#2196F3',
      red: '#EF5350',
      pink: '#F48FB1',
    },
    fontFamily: {
      sans: ['Graphik', 'sans-serif'],
      serif: ['Merriweather', 'serif'],
    },
    extend: {
      spacing: {
        '128': '32rem',
        '144': '36rem',
      },
      borderRadius: {
        '4xl': '2rem',
      }
    }
  },
  variants: {
    extend: {
      borderColor: ['focus-visible'],
      opacity: ['disabled'],
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

The theme section is where we customise anything related to the visual design/design system of our site. Here we can specify our colour palette, font family, border-radius sizes, and the likes. Without customisation, Tailwind comes with specific values for all of these style properties, like colours for example.

Image description

Looking at this chart we know that we can customize 10 shades of the color “Red”, the problem, however, comes in when we need say one or two additional shades (insatiable humans, eh?). Of course, Tailwind’s configuration file can help us achieve this:

// tailwind.config.js
module.exports = {
  theme: {
    extend: {
      colors: {
        red: {
          350: '#E53935'
        },
      }
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

We shouldn’t have to do this if we only need these shades in one or two parts of our application, and they aren’t necessary for theming. This is where Arbitrary Styles with Tailwind JIT compiler come in. With Arbitrary Styles, you can specify the hyper-specific style you need on elements without needing to update it in your config file…Yes, Sam, you can now get rid of those inline style attributes 👀.


Let’s put this to a test. I’ll spin up a Vue project and install Tailwind, after that, we’d set up JIT mode. Note that you can do this with whatever Javascript frontend framework you work with. Tailwind is framework agnostic.

Psst! Here’s a Vue and no-framework setup for Tailwind:

vue create tailwind-ab-styles
Enter fullscreen mode Exit fullscreen mode

Navigate into the project directory:

cd tailwind-ab-styles
Enter fullscreen mode Exit fullscreen mode

Install and Set up Tailwind CSS. Here’s a quick guide to set up Tailwind CSS in your Vue project here:

How to Setup Tailwind CSS in Vue 3 | DevjavuHow to Setup Tailwind CSS in Vue 3 | Devjavu

A streamlined walkthrough if you hate docs so much.

favicon devjavu.space

After setting up Tailwind, you can enable JIT by updating your tailwind.config.js and changing the “mode” property to “JIT”.

module.exports = {
  mode: 'jit',  // update this line
  purge: ['./index.html', './src/**/*.{vue,js,ts,jsx,tsx}'],
  darkMode: false, // or 'media' or 'class'
  ...
};
Enter fullscreen mode Exit fullscreen mode

Let’s spin up the Vue server and see what this looks like.

yarn serve
Enter fullscreen mode Exit fullscreen mode

You might get a similar warning from the bundler about the JIT engine currently in preview when you start the server.

Image description


Using Arbitrary Styles

I’ve updated the App.vue file, to a simple markup with basic Tailwind classes.

<template>
  <div class="justify-center flex bg-yellow-300 items-center h-screen">
    <div class="text-4xl">
      Hello 👋🏼
    </div>
  </div>
</template>

<script>
export default {
  name: 'App',
};
</script>
Enter fullscreen mode Exit fullscreen mode

Result:

Image description

In the markup above notice, we have a text “Hello” with a div of class “text-4l” wrapped around it. Tailwind provides 10 “font-size” utility classes that we can use, and their values range from 0.75rem to 8rem. Say we wanted just this text to be size 9rem, with arbitrary styles we don’t have to configure another utility class for this case, instead, we do this:

<div class="text-[9rem]">
  Hello 👋🏼
</div>
Enter fullscreen mode Exit fullscreen mode

And there you have it, a huge text of size 9rem. This saves us the pain of littering our mark-up with inline styles.

Image description

Let’s add in a box below the “hello” text and set its width, height, and background color using Arbitrary values.

<div class="w-[300px] h-[200px] bg-[#FFECB3] rounded-[20px] ml-[40px]"></div>
Enter fullscreen mode Exit fullscreen mode

Image description

We can also use this for screen variants as well.

<div class="w-[300px] h-[200px] md:bg-[#FFECB3] rounded-[20px] ml-[40px] lg:ml-[-400px] lg:mb-[430px]"></div>
Enter fullscreen mode Exit fullscreen mode

Image description

This also works for grids and layouts as well, check out the documentation for advanced applications.

Limitation

At the time of writing, you can only @apply classes that are part of core, generated by plugins, or defined within a @layer rule. You can’t @apply arbitrary CSS classes that aren’t defined within a @layer rule. — docs

@apply is a directive used to inline an existing utility class into your custom CSS. Currently, this is the only known limitation with arbitrary values. Have fun tinkering with it.


Cheers ☕️

Top comments (0)