# Quick Tip: How to Build a Simple Stack Hover Effect With CSS

A commonly seen design pattern is to have a card container with other cards underneath it, resulting in a multi-layer stacking effect. Then, during the hover state, the cards smoothly disappear or change their default position. In this short tutorial, we’ll learn a simple CSS technique for creating such an effect.

As usual, let’s have a first look at our demo (scroll through to see all the examples):

## 1. Begin With the HTML Markup

We’ll start with the .card-wrapper element which will include all the cards.

Each card, which is represented by the card class, will contain the .card-inner element. Inside that element, we’ll put the contents of the target card:

 1 
 2 
 3 
 4 

...

 5 
...
 6 
 7   8   9 


Depending on the hover effect we want to achieve, each class will receive one of the following classes:

• card-top-left
• card-top-right
• card-bottom-left
• card-bottom-right
• card-diagonal-from-right
• card-diagonal-from-left
• card-rotate
• card-perspective
• card-origin

## 2. Define the Styles

With the markup ready, we’ll set up a few CSS variables and reset styles:

 1 :root {  2  --body-text-color: #5c5957;  3  --body-bg-color: #e2d9d5;  4  --card-border-color: #e2d9d5;  5  --card-bg-color: #fff;  6  --offset-before: 8px;  7  --offset-after: 16px;  8 }  9 10 *,  11 *::before,  12 *::after {  13  box-sizing: border-box;  14 }  15 16 body {  17  color: var(--body-text-color);  18  background: var(--body-bg-color);  19 } 

To create the stacking effect, we’re going to take advantage of the pseudo-elements. For each card we’ll define the ::before and ::after pseudo-elements and absolutely position them relative to the parent card.

Next, we’ll move them away from their original position by using the transform property. For the first six examples in the demo, the amount of this reposition will be determined by the offset-before and offset-after variables.

Another thing to note is that we give z-index: 1 to all .card-inner elements so that they always sit on top of their pseudo-elements.

Here’s the card layout when the value of the offset-before variable is 8px and the value of the offset-after variable is 16px:

And here’s the layout when those variables double their values:

The required CSS styles for creating the card layout:

 1 /*VARIABLES HERE*/  2 3 .card {  4  position: relative;  5  cursor: pointer;  6  max-width: 400px;  7  margin: 60px auto;  8 }  9 10 .card::before,  11 .card::after {  12  content: "";  13  position: absolute;  14  top: 0;  15  left: 0;  16  width: 100%;  17  height: 100%;  18 }  19 20 .card::before,  21 .card::after,  22 .card .card-inner {  23  background-color: var(--card-bg-color);  24  border: 1px solid var(--card-border-color);  25  border-radius: 5px;  26  transition: transform 0.3s;  27 }  28 29 .card::before,  30 .card-inner {  31  z-index: 1;  32 }  33 34 .card-inner {  35  position: relative;  36  padding: 3rem;  37 } 

As said, each card includes a second class that handles its initial offsets along with the hover effect. For example, here are the extra rules that fire for a card that contains the card-top-left class:

 1 /*CUSTOM VARIABLES HERE*/  2 3 .card-top-left::before {  4  transform: translate(  5  calc(-1 * var(--offset-before)),  6  calc(-1 * var(--offset-before))  7  );  8 }  9 10 .card-top-left::after {  11  transform: translate(  12  calc(-1 * var(--offset-after)),  13  calc(-1 * var(--offset-after))  14  );  15 }  16 17 .card-top-left:hover::before,  18 .card-top-left:hover::after,  19 .card-top-left:hover .card-inner {  20  transform: translate(calc(-1 * var(--offset-before)), 0);  21 } 

## Conclusion

And we’re done, folks! In this quick tutorial, we went through a simple CSS approach for creating card stack hover effects.

Here’s a reminder of what we built:

Have you ever implemented this kind of layout? If so, what technique did you use? Join the discussion in the comments below!

As always, thanks a lot for reading!