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

Specificity, Cascade, and CSS Layers

2m:50s

Adam Wathan has (very cleverly) built Tailwind with CSS Cascade Layers, making it extremely powerful for organizing styles by priority.

This is the default order:

@layer theme, base, components, utilities;
@import 'tailwindcss/theme.css' layer(theme);
@import 'tailwindcss/utilities.css' layer(utilities);

The core of Tailwind is its utilities. This means you have two choices:

  1. The default way
  2. The unorthodox way

The default way

The default choice is to follow the standard recommendation: place components first, and Tailwind utilities last.

So, if you’re building components, you need to manually wrap your components with a @layer directive. Then, override your component styles with Tailwind, putting Tailwind as the “most important layer”.

@layer components {
.card {
border: 2px solid black;
padding: 1rlh;
}
}
<div class="card border-blue-500">...</div>
Card with blue border

But like I said, this is tedious. I prefer the unorthodox way.

The unorthodox way

The unorthodox way is to write your styles in an unnamed layer — or any layer after utilities, really — so your CSS naturally overrides Tailwind utilities.

Of these two, I prefer the unnamed layer option:

/* Define the layers as usual */
@layer theme, base, components, utilities;
/* Write your CSS normally here */
.component {
/* ... */
}

This allows me to do stuff like:

  • Writing less code (by omitting unnecessary layer declarations)
  • Write complex stuff that’s hard or impossible to do in Tailwind (like theming and animations) in CSS — and not worry about Tailwind overriding them

Being trained in old-school CSS, I’m also very good with specificity management techniques like ITCSS so I’m not worried about specificity wars.

The simple ITCSS tip is:

  1. Use 1 class whenever possible
  2. Nest classes only if you want to ensure that single-class selectors don’t override them

Pretty simple, really.

Now, if you follow my reasoning so far, you would notice that my Tailwind utilities have a weaker specificity than the styles written in my CSS:

.card {
border: 2px solid black;
padding: 1rlh;
}
<div class="card border-blue-500">...</div>
Card with black border

This is intended — I want CSS to override Tailwind. Remember the Simple Unorthodox System?

  1. Write Tailwind
  2. Shift things into CSS when Tailwind gets frustrating

In doing this, I allow myself to use the Simple Unorthodox System. Then, if I need to override a value in the HTML with Tailwind, I just !important the utility to give it the strength it needs.

<!-- !important the border-color utility -->
<div class="card !border-blue-500">...</div>
Card with blue border

This sounds plain wrong

Well…

  1. It’s unorthodox, so it’s gonna sound wrong
  2. Remember there are no best practices? Feel free to experiment with this and see how it works for you.
  3. The !important keyword has traditionally been used to override classes so we’re still using it for what it’s meant to do.

Plus, we’re also leveraging the !important feature in CSS Layers to make the Tailwind utility more important than any !important in the unnamed layer — it falls within the principles of what !important is supposed to do.

So this unorthodox way is a perfectly valid way to use the powers of CSS Layers and Tailwind utilities.

Besides, the !important is so explicit (and used so little) that it makes sense for one-off quick-and-dirty adjustments without creating a brand new selector.