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

Transitioning shadows

5m:28s

Transitions are very nice effects because they guide a user’s view from one state to another. We can probably spend one entire afternoon just talking about transitions — but right now I want to focus on transitioning shadows.

box-shadow is expensive to transition because it lives in the paint layer. If we transition a box shadow directly, it is likely to cause jank. So what we usually do is transition the opacity of the element that contains the shadow.

This means:

  • We can either use a before or after pseudo-element to house the shadow
  • Or we can house the shadow on another HTML element.

Both work — and they have their pros and cons.

Using before/after pseudo elements

It is quite a large amount of code to put a box shadow on a pseudo-element, because we have to:

  • Create the pseudo element
  • Position it nicely behind the card
  • Include any border-radius that is present
  • Include the transition properties
  • Ensure it has an opacity of 0…
  • And transition the opacity to 1 when a user interacts with the card.

Here’s what I found is needed for this element to work — taking tiny bugs into account:

.card {
position: relative;
&::after {
content: '';
position: absolute;
z-index: -1;
opacity: 0;
transition-property: opacity;
/* This is required if parent element is inline-block. */
/* But not required if parent element is inline-flex */
top: calc(var(--border-width, 0px) * -1);
left: calc(var(--border-width, 0px) * -1);
box-sizing: content-box;
/* Ensure it's same size as the element */
width: 100%;
height: 100%;
/* Inherit borders */
border: inherit;
border-radius: inherit;
/* Gotta fill in these values here */
box-shadow: /* shadow here */;
transition: /* transition values here */;
}
}

Detecting when a user interacts with the box can require a lot of selectors depending on what the interactions are. It could be: :hover, :focus, :active; maybe aria-selected, aria-pressed, and aria-expanded.

But instead of writing a long line of selectors, we can use the interact variant that we have created in a previous lesson.

.card {
/* ... */
@variant interact {
&::after {
opacity: 1;
}
}
}

This is a card. Hover over me!

But this is still a lot of work. I don’t think anyone would want to recreate this multiple times in their code, so I made utilities for it, and I call them shadow-before and shadow-after.

Shadow-before and shadow-after

Now, instead of writing the whole list of CSS code that you saw above, you just have to use the shadow-before utility (or shadow-after if you want to place it on the after pseudo-element).

<div class="shadow-before elevation-5">This is a card. Hover over me!</div>

This is a card. Hover over me!

With a starting shadow

You can also start with a lighter shadow before the interaction happens. To do that, you just add the opacity directly on the pseudo element.

<div class="shadow-before elevation-5 before:opacity-30">
This is a card. Hover over me!
</div>

This is a card. Hover over me!

Sweet. Now for the second method — which is far more effective and controllable.

Using an HTML element

The second method is to use an HTML element. The whole idea is the same as a pseudo-element. But this time we use CSS Grid to overlay things on top of one another.

And guess what? We already have a layout that allows us to do that — stack.

<div class="stack">
<div class="elevation-5"></div>
<div class="p-4">This is a card</div>
</div>
This is a card

This works, but the stack itself is not interactive, so we can’t transition the shadow just yet.

Stack-interactive to the rescue

We can use stack-interactive, because it allows overlays and underlays to appear when a user interacts with it. Both overlay and underlay start at 0 opacity and transition to 1 upon any interaction within the element.

<div class="stack-interactive">
<div class="underlay elevation-5"></div>
<div class="p-4">Your main content</div>
</div>
This is a card, hover over me

Here’s the minimal CSS for stack-interactive if you’re curious:

@utility stack-interactive {
display: grid;
> * {
grid-column: 1 / span 1;
grid-row: 1 / span 1;
}
> .underlay,
> .overlay {
display: grid;
opacity: 0;
transition: /* transition here */;
transition-property: opacity;
}
&:hover,
&:focus-visible,
&:focus-within {
> .underlay,
> .overlay {
pointer-events: auto;
opacity: 1;
}
}
}

The only problem with this approach is you have to provide the border-radius to the underlay yourself. Without this, the elevation layer is disconnected from the actual content layer.

<div class="stack-interactive">
<div class="underlay rounded-[1rem] elevation-5"></div>
<button class="p-4 rounded-[1rem]">Your main content</button>
</div>

Without border radius

With border radius

With a starting shadow

The element can have a shadow when it is not being interacted with as well. To do that, we can add an opacity to the underlay — this is similar to the pseudo-element concept, except it is being applied to an actual element.

<div class="stack-interactive">
<div class="underlay elevation-5 opacity-30"></div>
<button class="p-4">Your main content</button>
</div>

From one elevation to another elevation

The best part about using an actual HTML element is that we can transition from one elevation to another without the percentage of opacity trick.

To do this, we can use another interesting feature that stack-interactive has — to fade-out a layer upon any interaction.

<div class="stack-interactive">
<!-- Shadow layer that fades out when interacted -->
<div class="elevation-3 fade-out"></div>
<!-- Shadow layer that fades in when interacted -->
<div class="underlay elevation-5"></div>
<div>Your main content</div>
</div>

It’s a bit more code, but this makes the shadows a lot more crisp and nice.