Box Shadows & Animation
Get a smooth animation with a 3D card effect using box shadow using just CSS animations and a pseudo element. Let’s see the final product, hover over the box below to see it:
Ok, let’s break that down. We can apply a box shadow using the box-shadow
css property with the following settings:
So in our case, we first make a .box
with rounded corners which is achieved
with border-radius: 5px with a box-shadow of 0 offset-x, 1px offset-y,
and 2px blur-radius, with a color of gray or rgba(0,0,0,0.15).
Next we create a pseudo element that is invisible at the beginning but when animated will receive the change. We animate the opacity because that causes the browser to do the least amount of repaints. A repaint is performed by the browser and occurs when changes are made to an elements skin that changes visibility, but do not affect its layout. You can see which css elements cause the least amount of browser adjustments here. In the pseudo element we increase the box shadow and make it invisible by giving it an opacity of 0.
Next we need to animate the box on hover, increase the size, and make the pseudo element by changing the opacity from 0 to 1:
Let’s see again for good measure 😌.
Check this box-shadow guide to read more. This was largely adapted from this guide. Check out our Codepen.