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

Nicer shadows

6m:22s

Shadows are one of the most used elements in web design. They’re commonly used to define the soft edges of an element, or to lift an element up above other things.

Tailwind’s shadows

Tailwind has a few basic shadow-* utilities that let you do this. Here are their declarations:

CSS declarations for Tailwind's smaller shadow utilities: shadow-2xs, shadow-xs, shadow-sm, and shadow-md
CSS declarations for Tailwind's larger shadow utilities: shadow-lg, shadow-xl, and shadow-2xl

You use them with the shadow-* utilities:

<!-- md-sized shadow, -->
<div class="shadow-md">...</div>

The default output from Tailwind’s shadow utilities is as follows:

I’ll be honest with you, they look rough and not nice at all.

What nicer shadows look like

Josh Comeau has an excellent tutorial on making beautiful shadows that look richer. I highly recommend reading this. He talks about:

  • Shadow elevation
  • The right shadow length and blur
  • And choosing the right shadow colours.

He also built a shadow generator tool that gives you the right CSS for the shadows you want.

Josh Comeau's Shadow Palette Generator interface, showing three elevation previews on the left and controls for Oomph, Crispy, Light Position, Background Color, Tint Shadow, and Resolution on the right
The shadow generator tool
CSS output from Josh's shadow generator, defining --shadow-color and three layered custom properties: --shadow-elevation-low, --shadow-elevation-medium, and --shadow-elevation-high
Output from Josh’s shadow generator

The tool is amazing, but I don’t use it. Because I don’t want to go to an external tool and copy-paste the CSS from there — I find it to be a cumbersome and slow process.

So I chose to build my own tools to create a similar effect.

Making a shadow utility

The basic idea behind creating a nice shadow is pretty simple if you begin to break down the code. This is an example that I lifted from Tobias Ahlin’s article on smooth box shadows that Josh referenced.

.selector {
box-shadow:
0 1px 1px rgba(0, 0, 0, 0.12),
0 2px 2px rgba(0, 0, 0, 0.12),
0 4px 4px rgba(0, 0, 0, 0.12),
0 8px 8px rgba(0, 0, 0, 0.12),
0 16px 16px rgba(0, 0, 0, 0.12);
}

You can see that there is a very specific pattern — each layer of the box shadow has values that are twice the values of the previous one.

So the idea is simple. To create a five-layered box shadow, you need to have:

  • a starting x value
  • a starting y value
  • a starting blur value
  • a multiple for each of them
  • the shadow color

We can extract these and put them into CSS variables like this.

.selector {
--shadow-x: 0;
--shadow-y: 1px;
--shadow-blur: 1px;
--shadow-x-multiple: 2;
--shadow-y-multiple: 2;
--shadow-blur-multiple: 2;
--shadow-color: rgba(0, 0, 0, 0.12);
}

Now let’s say we only use this to create a three-layer shadow. You can create the first layer by using the variables like this.

.shadow-3 {
box-shadow: var(--shadow-x) var(--shadow-y) var(--shadow-blur) 0
var(--shadow-color);
}

For the second and third layers, we can use calc and pow to magnify the shadow to the appropriate amount.

.shadow-3 {
box-shadow:
/* Layer 1 */
var(--shadow-x) var(--shadow-y) var(--shadow-blur) 0 var(--shadow-color),
/* Layer 2 */
calc(var(--shadow-x) * pow(var(--shadow-x-multiple), 1))
calc(var(--shadow-y) * pow(var(--shadow-y-multiple), 1))
calc(var(--shadow-blur) * pow(var(--shadow-blur-multiple), 1))
0 var(--shadow-color),
/* Layer 3 */
calc(var(--shadow-x) * pow(var(--shadow-x-multiple), 2))
calc(var(--shadow-y) * pow(var(--shadow-y-multiple), 2))
calc(var(--shadow-blur) * pow(var(--shadow-blur-multiple), 2))
0 var(--shadow-color);
}

The result of this 3-layer shadow looks like this:

Developing this further

3 layers are not enough, so I developed the idea and ended up with 7 shadow layers. I call each layer an elevation — because they’re there to lift things up.

  • elevation-1 has one layer of shadow
  • elevation-2 has two layers
  • and so on
<div class="elevation-1">...</div>
<div class="elevation-2">...</div>
<div class="elevation-3">...</div>
<div class="elevation-4">...</div>
<div class="elevation-5">...</div>
<div class="elevation-6">...</div>
<div class="elevation-7">...</div>

The output of these elevations (with their defaults) is as follows. I think they’re much richer than Tailwind’s built-in shadows — although it might be a tad overpowering.

Tailwind’s shadows

My elevation utility

So I added other variables like --shadow-alpha and --shadow-spread that allow us to adjust the generated shadows even more.

Here’s a demo you can play with to see how they all work together:

shadow-x
0px
shadow-y
1px
shadow-blur
1px
shadow-spread
0px
shadow-x-multiple
2
shadow-y-multiple
2
shadow-blur-multiple
2
shadow-spread-multiple
2
shadow-alpha
0.25

Now, creating the perfect shadow can be as easy as using the right elevation-* and adjusting a few variables!

<div
class="elevation-5"
style="--shadow-y: 2px; --shadow-color: black;"
>
...
</div>

5

Functional shadow utilities

Variables like --shadow-y and --shadow-color are very verbose. They’re fine in CSS, but they’re a code smell if we were to write them into the HTML, so we need functional utilities if we want to make this easier to use.

But Tailwind has taken up the shadow-* namespace, so we need another name.

I thought about it for a while, and I chose to use s-* as my shadow namespace eventually. This allows me to write functional utilities for text-shadow with ts-* and drop-shadow with ds-*.

Then I built one utility for each of these variables:

Utility The property it adjusts
sx-* --shadow-x
sy-* --shadow-y
sb-* --shadow-blur
ss-* --shadow-spread
sxm-* --shadow-x-multiple
sym-* --shadow-y-multiple
sbm-* --shadow-blur-multiple
ssm-* --shadow-spread-multiple
sc-* --shadow-color
sa-* Shadow alpha (transparency)

The code for this is similar to what we’ve discussed in the previous lessons so I’m not going to repeat it. You can find the actual utilities over at Splendid Labz.

Now I can use these utilities to adjust shadows easily. But the downside here is that these are quite cryptic to people who’re looking at them for the first time.

<!-- From this -->
<div
class="elevation-5"
style="--shadow-y: 2px; --shadow-color: black; --shadow-alpha: 25;"
>
...
</div>
<!-- To this -->
<div class="elevation-5 sy-[2px] sc-black sa-25">...</div>