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

Tailwind is great for layouts

7m:18s

When I talk about layouts, I’m referring to how you place items on a page. The CSS properties that are widely used here include:

  • display — often grid or flex nowadays
  • margin
  • padding
  • width
  • height
  • position
  • top, left, bottom, right

I include border-width as a minor item in this list as well.

At this point, there’s only one thing I’d like to say.

Tailwind is really great for making layouts.

There are many reasons why:

  1. Layouts are dependent on the HTML structure
  2. There’s no need to name layouts
  3. Just-in-time layout requirements
  4. Responsive variants can be created on the fly

Layout styles are highly dependent on the HTML structure

When we shift layouts into CSS, we lose the HTML structure. And it takes mental effort to reestablish them.

Imagine you have this two-column grid in CSS:

<div class="grid">
<div class="grid-item"></div>
<div class="grid-item"></div>
</div>
.grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 1rem;
> :first-child {
grid-column: span 2;
}
> :last-child {
grid-column: span 1;
}
}

Now cover the HTML structure and just read the CSS. As you do that, notice you need to exert effort to imagine the HTML structure that this applies to.

Now imagine the same, but built with Tailwind utilities:

<div class="grid grid-cols-3">
<div class="col-span-2"></div>
<div class="col-span-1"></div>
</div>

You might almost begin to see the layout manifest in your eyes without seeing the actual output. It’s pretty clear: 3 column grid, first item spans 2 columns while the second one spans 1 column.

But grid-cols-3 and col-span-2 are kinda weird and foreign-looking because we’re trying to parse Tailwind’s method of writing CSS.

This obviously isn’t the best way — and that’s why I stress so much on creating utilities.

Use your utilities over Tailwind’s utilities

If you do this, you can define your own syntax and make the layout crystal clear to you.

For example, I defined the number of columns for the grid and the number of --columns an item should --span with CSS variables. This layout is extremely clear to me.

<div class="grid-simple [--cols:3]">
<div class="[--span:2]">...</div>
<div class="[--span:1]">...</div>
</div>
Content
Sidebar

It’s the same 3-column layout.

But this is much easier to see — and know — what’s going on. Perhaps better than both the CSS and Tailwind version.

Why not use 2fr 1fr?

If you’re new to CSS grid, you may ask this question: “Why not use 2fr and 1fr for a 3-column grid?”

.grid {
display: grid;
grid-template-columns: 2fr 1fr;
}

The answer is simple. Because it doesn’t work.

This is because fr is calculated based on the available space that remains after subtracting away the grid’s gutters (or gap).

Since 2fr 1fr only contains 2 columns, the output from 2fr 1fr will be different from a standard 3-column grid.

Three column grid
One
Two
Three
With Grid Simple
Content
Sidebar
Using 2fr 1fr
Content
Sidebar

Alright. Let’s continue with the reasons that make Tailwind great for building layouts.

There’s no need to name layouts

Tailwind removes the need for you to name layouts. I rarely come up with better names than:

  • Number + Columns like two-columns
  • Semantic names like content-sidebar

But these names don’t do the layout justice — you can’t really tell what’s going on even if you use two-columns, because two-columns can mean a variety of things:

  1. Two equal columns
  2. Two columns with 1fr auto
  3. Two columns with auto 1fr
  4. Two columns that span a total of 7 “columns” and the first object takes up 4 columns while the second takes up 3…

You can already see me tripping up when I try to explain that last one there…

Instead of forcing ourselves to name the layout, we can let the numbers do the talking — then the whole structure becomes very clear.

<div class="grid-simple [--cols:7]">
<div class="[--span:4]">...</div>
<div class="[--span:3]">...</div>
</div>

The variables paint a picture.

7 column layout
4 Columns
3 Columns
1
2
3
4
5
6
7

Layout requirements can change depending on context

One of the hardest things to do within Vanilla CSS is to create layouts that have different spacing in different contexts.

Consider the example below where we have two-column layouts.

  1. The bigger one with wider gaps
  2. The smaller one with smaller gaps

The difference in gap sizes is subtle, but it is used to show that the items are of separate groups.

In this case:

  1. The wider grid has a wider gap
  2. The smaller grid has a smaller gap

So, if you are building with CSS only, you typically need to name a modifier, which means you need to name the classes.

.two-col-wider {
/* ... */
gap: 1rem;
}
.two-col-narrow {
/* ... */
gap: 0.5rem;
}

Whereas for Tailwind, you can just add the gap property, and that changes the layout on the fly.

<div class="grid-simple [--cols:2] gap-6">
<div class="grid-simple [--cols:2] gap-2">Left 4 blue rectangles</div>
<div class="grid-simple [--cols:2] gap-2">Right 4 blue rectangles</div>
</div>

A real example

So let’s move from theory into an actual real-world scenario where I use this concept in my site design.

Two layouts with different grids

Here you can see:

  1. There is a larger gap between the newsletter box and the quotes,
  2. And a smaller gap between the form items within the newsletter itself.

Another real example

Let’s say you’re writing a header.

Your subscription has been confirmed

Conventional typography wisdom says that you should vary the maximum width of the content based on how you want it to be displayed.

In this case, I believe that it is much nicer to add a max-width property to this heading so that it reads in a nicer way. It also prevents a word from being an orphan.

Your subscription has been confirmed

Without Tailwind, you’ll need an inline style for something of this nature.

<h2 class="h2" style="max-width: 12em;">
Your subscription has been confirmed
</h2>

With Tailwind, you can specify the max-width in a terser way:

<h2 class="h2 max-w-[12em]">
Your subscription has been confirmed
</h2>

Responsive variants can be created on the fly

Tailwind has the ability to create responsive layouts without leaving the HTML.

I think this is a superpower that has not been found in any other CSS framework, and this is the feature that makes layouts incredibly easy!

With CSS grid, if you want to define a responsive layout, you might need media queries.

.two-column {
display: grid;
grid-template-columns: 1fr;
/* other properties */
@media (width >= 768px) {
grid-template-columns: 1fr 1fr;
}
}

With Tailwind, you do not need the media query. You just need to specify the breakpoint with a responsive variant. (Which has this syntax variant:utility).

<div class="grid-simple md:[--cols:2]">...</div>

If you’re not a fan of writing responsive variants in the HTML, you can still choose to apply your Tailwind utility and extend the column count with media queries — still much easier than writing the CSS from scratch.

.two-column {
@apply grid-simple;
/* --cols: 1 is the default */
@media (width >= 800px) {
--cols: 2;
}
}

To top it off — we can even reuse the same simple-grid utility to create various different grid variants. For example, I have two columns on mobile and five columns on desktop in here.

<div class="grid-simple [--cols:2] md:[--cols:5]">...</div>
Mobile layout with 2 columns
Mobile layout. 2 Columns
Desktop layout with 5 columns
Desktop layout. 5 Columns

Of course, this is just another example where you don’t have to name your layouts. So you can tell by the number of times I’m emphasizing this, that naming layouts is a real pain point for me.