Essential CSS
JavaScript Roadmap
Learn JavaScript
Mastering Web Typography
Practical Astro
Understanding Async JS
Unorthodox Tailwind
API Masterclass
Build and Deploy
The Beginner Freelancer
Real World Node
Profile Billing Logout
Unorthodox Tailwind
Lesson Plan
  1. Introduction
    1. Introduction
    2. What Tailwind brings to the table
    3. Tailwind: The Good and The Bad
    4. The First and Most Important Step
    5. Simple Unorthodox System
    6. The System I Use
  2. Tailwind Fundamentals
    1. Introduction
    2. Installing Tailwind
    3. Using Tailwind utilities
    4. Understanding Tailwind’s utility system
    5. Understanding Tailwind values
    6. Understanding Tailwind variants
  3. Unorthodox Fundamentals
    1. Introduction
    2. Making Simple Tailwind Utilities
    3. Components vs Utilities
    4. Specificity, Cascade, and CSS Layers
    5. Debugging Tailwind Styles
    6. Composable Utility System
    7. Create lots of components
  4. Layouts System
    1. Tailwind is great for layouts
    2. Four Types of Layouts
    3. Building Shell Layouts
    4. Building Macro Layouts
    5. Better Responsive Variants
    6. A collection of maximum widths
    7. A note on grid wrappers
    8. Working with sections
    9. Building Micro Layouts
    10. Positioning Layouts
  5. Typography System
    1. Introduction
    2. A system for using multiple typefaces
    3. Tailwind's font-size system
    4. Finding your font-size system
    5. My font-size system
    6. A simple responsive typography system
    7. The main body
    8. Vertical rhythm
  6. Theming System
    1. Introduction
    2. Tailwind's colour system
    3. The pigment system
    4. Tailwind's gradient system
    5. A friendlier gradient system
    6. Handling SVG fill and stroke
    7. Improving Tailwind's colour palette
    8. A simple way to theme
  7. Useful Tools
    1. Introduction
    2. Making functional utilities
    3. Custom variants
    4. Scaffolding tools
    5. Extending Tailwind
  8. Effects System
    1. Introduction
    2. Vertical text
    3. Strikethrough text
    4. Text stroke
    5. Highlighting text
    6. Nicer shadows
    7. Transitioning shadows
    8. Drop shadows
    9. Dark mode tricks
    10. Gradient borders
    11. More effects to explore
  9. Putting Everything Together
    1. The Unorthodox Tailwind process
    2. The 4 systems combined
    3. Create components liberally
    4. Advanced tips
    5. My CSS Structure
    6. A Cheatsheet
    7. Consolidating your thoughts
    8. Parting words

Custom variants

2m:3s

Earlier, we mentioned that Tailwind is really bad with theming because you have to style hover, focus, and active states individually.

<div class="hover:bg-blue-500 focus:bg-blue-500 active:bg-blue-500"></div>

The good news is Tailwind allows you to create variants with custom variants, so you no longer have to write so many individual variants by yourself. You can combine them into a bigger one.

So if we want to apply the same utility to :hover, :focus and :active, we can combine them into something like a custom-variant. Let’s call it haf (hover, active, focus) variant.

@custom-variant haf {
&:hover,
&:focus,
&:active {
@slot;
}
}

Then you can write this instead of repeating the smaller variants:

<div class="haf:bg-blue-500">...</div>

If you want to use the variant in CSS, you can use it with @variant.

.selector {
@variant haf {
/* Your rules here... */
}
}

That said, I still think pigment is way better for theming because we tend to have to style active states differently from hover and focus states — plus there are other states not mentioned here.

But these custom variants are still very useful for interactions.

The Interact Variant

One of the common things we have to do as developers is to be able to tell whether an element should be active or not.

Most of the time, an element is active when:

  • A user interacts with it
  • It is in some sort of a selected state.

We have a lot of selectors that allow us to check for these states, like hover, focus, focus-visible, active, [aria-current], [aria-selected="true"], etc. So what I did was to consolidate all of them into a custom variant called interact.

/* Tailwind */
@custom-variant interact {
&:hover,
&:focus-visible,
&:active,
&[aria-current],
&[aria-selected='true'],
&[aria-pressed='true'],
&[aria-expanded='true'] {
@slot;
}
}

I named it interact because I usually activate this when a user interacts with the element, but you can also call it is-active or any other variant you want.

Interact-within Variant

The second variant I find useful is something I call interact-within.

The basic idea here is to select an element when a child element within is being interacted with. It might also have certain states to check for on itself.

Here’s the code for it.

@custom-variant interact-within {
&:hover,
&:focus-within,
&:has(
.selected,
.checked,
[aria-current],
[aria-selected='true'],
[aria-pressed='true'],
[aria-expanded='true']
) {
@slot;
}
}

So far, these are the only two useful custom variants I’ve found.