1

I have 4 cards (extendible to N cards) in a stack that I want to show in a stacked effect using HTML and CSS.

Desired Output: Desired Stacked Card Effect

I have the following code so far, but need some help tweaking the CSS to show the stacked effect for all 4 cards instead of 2 cards.

body {
  background-color: #e8eaed;
}
.stack {
  width: 500px;
  height: 500px;
  position: absolute;
}

.card {
  width: 80%;
  min-height: 40%;
  background-color: white;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  display: grid;
  justify-content: center;
  align-items: center;
  border-radius: 5px;
  font-family: sans-serif;
  font-size: 10rem;
  color: #00000080;
  box-shadow: 0 5px 15px 0 #00000040, 0 5px 5px 0#00000020;
  transition: transform 200ms;
}

.card:nth-last-child(1) {
  --y: calc(-50% + 15px);
  transform: translate(-50%, var(--y)) scale(1.05);
}
<div class="stack">
      <div class="card" style="--i:1">1</div>
      <div class="card" style="--i:2">2</div>
      <div class="card" style="--i:3">3</div>
      <div class="card" style="--i:4">4</div>
</div>

1
  • It's not clear what you're asking until you click on the codepen and see that each card is larger than the previous one in the stack. Please edit your question to include the codepen as a stacksnippet rather than an external link. Commented Mar 1, 2021 at 21:54

1 Answer 1

2

You are using the CSS Custom Property --y in your styles and the custom property --i in your markup.

I've made some edits to your styles, to demonstrate how you can achieve the effect you describe.

Working Example:

body {
  background-color: #e8eaed;
}
.stack {
  width: 500px;
  height: 500px;
  position: absolute;
}

.card {
  width: 80%;
  min-height: 40%;
  background-color: white;
  position: absolute;
  top: 100px;
  left: 100px;
  transform: translate(-50%, -50%);
  display: grid;
  justify-content: center;
  align-items: center;
  border-radius: 5px;
  font-family: sans-serif;
  font-size: 10rem;
  color: #00000080;
  box-shadow: 0 5px 15px 0 #00000040, 0 5px 5px 0#00000020;
  transition: transform 200ms;
}

.card {
  transform: translateY(calc((var(--y) * 20px) - 50%)) scale(calc(1.0 + var(--y) * 0.05));
}
<div class="stack">
      <div class="card" style="--y:1">1</div>
      <div class="card" style="--y:2">2</div>
      <div class="card" style="--y:3">3</div>
      <div class="card" style="--y:4">4</div>
</div>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.