<div class="demo-container">
  <h2>Conditional styling & stagger effect</h2>
  <p>The peak notification is shown using <code>if()</code> when a value is the full 100%. Animations are staggered using <code>sibling-index()</code>.</p>
  <div class="controls">
    <button id="reset-animation">Reset Animation</button>
  </div>
  <div class="bar-chart-container">
    <ul class="bar-chart animate">
      <li style="--value: 70;"></li>
      <li style="--value: 45;"></li>
      <li style="--value: 20;"></li>
      <li style="--value: 100;"></li>
      <li style="--value: 70;"></li>
    </ul>
  </div>
</div>
@import url("https://fonts.googleapis.com/css2?family=Poppins:wght@400;600;700&display=swap");

.bar-chart {
  display: flex;
  justify-content: space-around;
  align-items: flex-end;
  gap: 1.5rem;
  list-style: none;
  padding: 0;
  margin: 0;
  height: 200px;
  li {
    width: 100%;
    height: calc(var(--value, 50) / 100 * 100%);
    background: var(--accent-grad);
    transform-origin: bottom;
    position: relative;
    &::after {
      display: if(style(--value: 100): block ; else: none);
      content: if(style(--value: 100): "Perfect!" ; else: "");
      position: absolute;
      top: -25px;
      left: 50%;
      transform: translateX(-50%);
      background-color: #333;
      color: white;
      font-size: 0.75rem;
      padding: 0.2em 0.5em;
      border-radius: 4px;
      white-space: nowrap;
    }
  }
  &.animate li {
    animation: grow-bar 0.8s cubic-bezier(0.68, -0.55, 0.27, 1.55) both;
    animation-delay: calc(sibling-index() * 0.1s);
  }
}

@keyframes grow-bar {
  from {
    transform: scaleY(0);
  }
  to {
    transform: scaleY(1);
  }
}

@layer presentaion {
  :root {
    --bg-color: #f0f2f5;
    --text-color: #333;
    --accent-grad: linear-gradient(160deg, #ff7e9b, #8a7eff);
  }

  body {
    font-family: "Poppins", sans-serif;
    background-color: var(--bg-color);
    color: var(--text-color);
    margin: 0;
    padding: 3rem 1rem;
    display: grid;
    gap: 3.5rem;
    justify-items: center;
  }

  .demo-container {
    width: 100%;
    max-width: 650px;
    text-align: center;
  }

  h2 {
    margin-bottom: 0.5rem;
  }

  p {
    margin-top: 0;
    margin-bottom: 1.5rem;
    color: #666;
    max-width: 500px;
    margin-left: auto;
    margin-right: auto;
  }

  code {
    background-color: #e2e5e9;
    padding: 0.2em 0.4em;
    border-radius: 4px;
    font-size: 0.9em;
  }

  button {
    font-family: inherit;
    font-size: 1rem;
    font-weight: 600;
    padding: 0.75rem 1.5rem;
    border: none;
    border-radius: 8px;
    background-image: var(--accent-grad);
    color: white;
    cursor: pointer;
    transition: transform 0.2s ease, box-shadow 0.2s ease;
    margin: 0 0.5rem 1.5rem;
    &:hover {
      transform: translateY(-2px);
      box-shadow: 0 6px B20px rgba(138, 126, 255, 0.4);
    }
  }

  .bar-chart-container {
    width: 100%;
    max-width: 500px;
    background-color: #fff;
    padding: 2rem 1.5rem;
    border-radius: 12px;
    box-shadow: 0 4px 15px rgba(0, 0, 0, 0.08);
    align-self: center;
    justify-self: center;
  }

  *,
  *::after {
    box-sizing: border-box;
  }
}
const resetBtn = document.getElementById("reset-animation");
const barChart = document.querySelector(".bar-chart");

resetBtn.addEventListener("click", () => {
  // A common trick to restart CSS animations
  barChart.classList.remove("animate");
  // This forces a reflow, which is needed for the class removal to be registered
  void barChart.offsetWidth;
  barChart.classList.add("animate");
});
Run Pen

External CSS

This Pen doesn't use any external CSS resources.

External JavaScript

This Pen doesn't use any external JavaScript resources.