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

Making Simple Tailwind Utilities

2m:24s

One of the most essential skills you need to learn here is to create simple Tailwind utilities, because:

  • Tailwind’s utilities are not just “utilities”
  • They’re more like mixins — powerful and useful tools that let you replicate code quickly

This is a little-known fact because Tailwind has packaged their “utilities” as almost a 1-1 mapping to a CSS value (like p is for padding).

We’re going to unpack this quite a bit and you’ll begin to see the powers behind Tailwind utilities. You’ll also see the gotchas and how to work around them.

Anatomy of a Utility

Writing a utility is similar to writing a CSS Selector. Except you define a Tailwind utility by using the @utility at-rule

@utility vertical {
/* CSS properties go here */
display: flex;
flex-flow: column;
gap: 1rem;
}

That’s it.

Once you have defined this utility, you can use it anywhere in your HTML directly — and the relevant CSS properties will be added to that element.

<div class="vertical">
<h1>Hello</h1>
<p>This is a vertical layout</p>
</div>

Hello

This is a vertical layout

Tailwind utilities can also take in CSS variables — this allows you to create very simple utilities with powerful effects.

@utility grid-simple {
display: grid;
grid-template-columns: repeat(var(--cols), minmax(auto, 1fr));
/* Other CSS properties */
}

Once I have defined grid-simple, I can use it like this:

<div class="grid-simple [--cols:2]">
<div class="bg-blue-300 p-2 text-center">Item 1</div>
<div class="bg-blue-300 p-2 text-center">Item 2</div>
</div>
Item 1
Item 2

In the example above, I no longer worry about preventing grid blowouts because it’s handled by grid-simple. And I can just change the --cols value at different breakpoints to get the layout I want.

Using Tailwind Utilities in CSS

You can use the Tailwind utilities you create in CSS with the @apply rule. The following is an extremely crude example with the utilities we mentioned above, but imagine this:

.grid-article {
@apply grid-simple;
--cols: 1;
@media (width >= 800px) {
--cols: 2;
}
}

Resize your browser to see this work

Item 1
Item 2

If you use Tailwind breakpoint variants, you don’t even have to write a @media query rule. Imagine the following.

.grid-article {
@apply vertical;
@apply md:grid-fr-auto;
}

This says: Use vertical on mobile and switch to grid-fr-auto at 768px.

  • Use a vertical layout on mobile
  • And switch to grid-fr-auto on tablet

Resize your browser to see this work

Content
Sidebar

This is super convenient!

I’ll talk more about these techniques in the Layouts section.