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
beforeorafterpseudo-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-radiusthat is present - Include the
transitionproperties - Ensure it has an
opacityof 0… - And
transitiontheopacityto1when 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 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>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.
Welcome! Unfortunately, you don’t have access to this lesson. To get access, please purchase the course or enroll in Magical Dev School.
Unlock this lesson