Modern Card Carousel
Home
Snippets
Modern Card Carousel
HTML
CSS
JS
<div class="carousel-container"> <button class="prev">❮</button> <div class="carousel" id="carousel"> <div class="card"> <img src="https://picsum.photos/300/150?1" alt="Card 1"> <h3>Card One</h3> <p>Beautiful design for card one.</p> </div> <div class="card"> <img src="https://picsum.photos/300/150?2" alt="Card 2"> <h3>Card Two</h3> <p>Beautiful design for card two.</p> </div> <div class="card"> <img src="https://picsum.photos/300/150?3" alt="Card 3"> <h3>Card Three</h3> <p>Beautiful design for card three.</p> </div> <div class="card"> <img src="https://picsum.photos/300/150?4" alt="Card 4"> <h3>Card Four</h3> <p>Beautiful design for card four.</p> </div> <div class="card"> <img src="https://picsum.photos/300/150?5" alt="Card 5"> <h3>Card Five</h3> <p>Beautiful design for card five.</p> </div> </div> <button class="next">❯</button> </div>
body { margin: 0; font-family: Arial, sans-serif; display: flex; justify-content: center; align-items: center; min-height: 100vh; background: #f0f4f8; } .carousel-container { position: relative; width: 90%; max-width: 900px; overflow: hidden; } .carousel { display: flex; transition: transform 0.5s ease-in-out; } .card { flex: 0 0 250px; /* default card width */ margin: 0 10px; background: #fff; border-radius: 12px; box-shadow: 0 8px 20px rgba(0,0,0,0.15); text-align: center; padding: 20px; box-sizing: border-box; } .card img { width: 100%; height: 150px; object-fit: cover; border-radius: 8px; } .card h3 { margin: 15px 0 10px; font-size: 1.2rem; color: #333; } .card p { font-size: 0.9rem; color: #666; } .prev, .next { position: absolute; top: 50%; transform: translateY(-50%); background: #3f51b5; color: #fff; border: none; border-radius: 50%; width: 40px; height: 40px; cursor: pointer; font-size: 1.2rem; display: flex; justify-content: center; align-items: center; z-index: 10; } .prev { left: 5px; } .next { right: 5px; } .prev:hover, .next:hover { background: #2c3e91; } @media(max-width: 768px){ .card { flex: 0 0 200px; margin: 0 8px; } .prev, .next { width: 35px; height: 35px; font-size: 1rem; } } @media(max-width: 480px){ .card { flex: 0 0 90%; margin: 0 5%; } .prev, .next { width: 30px; height: 30px; font-size: 0.9rem; } }
const carousel = document.getElementById('carousel'); const prev = document.querySelector('.prev'); const next = document.querySelector('.next'); let index = 0; const totalCards = carousel.children.length; function getCardWidth() { const card = carousel.children[0]; const style = getComputedStyle(card); return card.offsetWidth + parseInt(style.marginLeft) + parseInt(style.marginRight); } function updateCarousel() { const cardWidth = getCardWidth(); carousel.style.transform = `translateX(${-index * cardWidth}px)`; } next.addEventListener('click', () => { index = (index + 1) % totalCards; updateCarousel(); }); prev.addEventListener('click', () => { index = (index - 1 + totalCards) % totalCards; updateCarousel(); }); window.addEventListener('resize', updateCarousel); window.addEventListener('load', updateCarousel);
Ad #1
Ad #2
Scroll to Top