About this snippet

Create an immersive experience with this 3D image carousel. We use CSS perspective to arrange your photos in a rotating circle, which adds depth and style to your portfolio or product showcase. It works smoothly on all devices so your visitors stay engaged.

Size
2.8 KB
Uses
HTML, CSS, JavaScript
Loads
Nothing external
Views
1,017
<div class="carousel-container">
  <div class="carousel-3d" id="carousel">
    <div class="item">1</div>
    <div class="item">2</div>
    <div class="item">3</div>
    <div class="item">4</div>
    <div class="item">5</div>
    <div class="item">6</div>
  </div>
  <div class="controls">
    <button id="prevBtn">Prev</button>
    <button id="nextBtn">Next</button>
  </div>
</div>
.carousel-container {
  font-family: system-ui, "Segoe UI", Roboto, Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
  margin: 50px auto;
  width: 300px;
  height: 200px;
  position: relative;
  perspective: 1000px;
}
.carousel-3d {
  width: 100%;
  height: 100%;
  position: absolute;
  transform-style: preserve-3d;
  transition: transform 1s cubic-bezier(0.19, 1, 0.22, 1);
}
.item {
  position: absolute;
  width: 280px;
  height: 180px;
  left: 10px;
  top: 10px;
  background: #fff;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 5px 15px rgba(0,0,0,0.1);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 3rem;
  color: white;
  font-weight: 800;
  border: 4px solid #fff;
  opacity: 0.9;
}
.item:nth-child(1) { background: linear-gradient(135deg, #3b82f6, #2563eb); }
.item:nth-child(2) { background: linear-gradient(135deg, #ec4899, #db2777); }
.item:nth-child(3) { background: linear-gradient(135deg, #10b981, #059669); }
.item:nth-child(4) { background: linear-gradient(135deg, #f59e0b, #d97706); }
.item:nth-child(5) { background: linear-gradient(135deg, #8b5cf6, #7c3aed); }
.item:nth-child(6) { background: linear-gradient(135deg, #ef4444, #dc2626); }
.controls {
  position: absolute;
  bottom: -100px;
  left: 0;
  width: 100%;
  display: flex;
  justify-content: center;
  gap: 20px;
}
.controls button {
  padding: 12px 30px;
  background: #111827;
  color: #fff;
  border: none;
  border-radius: 50px;
  cursor: pointer;
  font-weight: 600;
  transition: transform 0.2s;
  font-size: 1rem;
}
.controls button:hover {
  transform: scale(1.05);
  background: #000;
}
const carousel = document.getElementById("carousel");
const items = document.querySelectorAll(".item");
const prevBtn = document.getElementById("prevBtn");
const nextBtn = document.getElementById("nextBtn");
const radius = 350;
let currAngle = 0;

const angleStep = 360 / items.length;
items.forEach((item, index) => {
  const angle = angleStep * index;
  item.style.transform = `rotateY(${angle}deg) translateZ(${radius}px)`;
});

function rotateCarousel(direction) {
  currAngle += direction * angleStep;
  carousel.style.transform = `rotateY(${currAngle * -1}deg)`;
}

prevBtn.addEventListener("click", () => rotateCarousel(-1));
nextBtn.addEventListener("click", () => rotateCarousel(1));

/* Uncomment to automatically go to next slide every 5 seconds
setInterval(() => {
  nextBtn.click();
}, 5000);
*/
Esc
↑↓ moveEnter select

Start from a template

Open an .html file, or HTML, CSS, JS, Markdown, Less or TypeScript files. You can also drop them here. Styles and scripts go into their panes.

Share

Source

Keyboard shortcuts

Save