Pen Settings



CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ add another resource


Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource


Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.


Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.


                <div class="carousel" duration="7000">
    <li id="c1_slide1"><div>Nice cat number 1<br /><a href="">adopt me</a></div></li>  
    <li id="c1_slide2"><div>Nice cat number 2<br /><a href="">adopt me</a></div></li>  
    <li id="c1_slide3"><div>Nice cat number 3<br /><a href="">adopt me</a></div></li>  
    <li id="c1_slide4"><div>Nice cat number 4<br /><a href="">adopt me</a></div></li>  
    <li><a href="#c1_slide1"></a></li>
    <li><a href="#c1_slide2"></a></li>
    <li><a href="#c1_slide3"></a></li>
    <li><a href="#c1_slide4"></a></li>
  <div class="prev">&lsaquo;</div>
  <div class="next">&rsaquo;</div>
<p>The above is an unordened list with horizontal overflow set to auto, snap scrolling and hidden scrollbars. You can swipe the carousel with your finger on touch screens through native touch scroll. The bullets are actually anchor links, which trigger a 'scroll-behaviour: smooth' scroll. The script uses a simple setInterval for autoplay which does not fire during hover and can be set on the carousel div in a custom attribute in milliseconds.</p>

<h2>Browser compatibility</h2>
<p>The code is tested on the newest versions of Chrome, Firefox, Chrome mobile and Firefox mobile in april 2021. This code uses fairly new techniques, like scroll snapping. However, I expect this solution to degrade quite graceful. It even looks and works 'okay-ish' without javascript.</p>

<h2>About the author</h2>
<p>I am a <a href="">website performance expert</a> and for <a href="">a project</a> I was looking for a <a href="">touch enabled carousel</a>. I could not find one that was minimal, so I created a basic responsive and touch-enabled carousel that weighs only 3kb (uncompressed and including CSS). This carousel is based on modern web technology and will not slow down (the rendering of) your page... and yes, that <a href="">project link</a> is a link to a production website.</p>

<p>The approach is (partly) based on the 2019 scroll snap carousel from <a href="">Nolan Lawson</a>. The debounce function is written by <a href="">Chris Ferdinandi</a>. The cat images are borrowed from <a href="">Pexels</a>.</p>

<p>WTFPL - This is a GPL-compatible permissive license most commonly used as a free software license. As a public domain like license, the WTFPL is essentially the same as dedication to the public domain. It allows redistribution and modification of the work under any terms. The title is an abbreviation of "Do What The Fuck You Want To Public License".</p>

<p><br />Enjoy the code!<br /><br /><a href="">Joost van der Schee</a></p>

<br /><br /><br /><br />


                /* RESET CSS */
* {margin: 0; padding: 0;}

.carousel {position: relative;}
.carousel::after {
  content: "";
  position: absolute;
  left: 0;
  top: 0;
  box-shadow: inset 0px 0px 120px 30px rgba(0,0,0,0.35);
  width: 100%;
  height: 100%;
  pointer-events: none;
.carousel ul {
  overflow: auto; 
  display: flex; 
  height: 40vw;
  max-height: 500px;
  scroll-snap-type: x mandatory;
  scroll-snap-points-y: repeat(100%);
  scroll-behavior: smooth;
  background: gray;
  -ms-overflow-style: none; /* IE and Edge */
  scrollbar-width: none; /* Firefox */
.carousel ul::-webkit-scrollbar {display: none; /* Hide scrollbar for Chrome, Safari and Opera */} 
.carousel ul li {
  min-width: 100%; 
  list-style: none; 
  background: url( center center / cover no-repeat; 
  scroll-snap-align: start;
  display: flex;
  justify-content: center;
  align-items: center;
  color: white;
  font-weight: bold;
  font-size: 30px;
  text-align: center;
  text-shadow: 0px 0px 10px rgba(0,0,0,0.75);
.carousel ul li div {z-index: 9;}
.carousel ul li div a {color: white; display: inline-block; background: white; text-shadow: none; color: black; padding: 5px 15px; font-size: 18px; text-decoration: none; border-radius: 4px; margin-top: 10px;}
.carousel ol {position: absolute; bottom: 15px; display: flex; justify-content: center; left: 50%; transform: translateX(-50%); z-index: 9;}
.carousel ol li {list-style: none; padding: 0 5px;}
.carousel ol li a {display: block; height: 10px; width: 10px; border: 2px solid white; background: transparent; border-radius: 100%;}
.carousel ol li.selected a {background: white;}
.carousel .prev, .carousel .next {user-select:none; cursor: pointer; font-size: 50px; color: white; position: absolute; left: 0; padding: 15px; top: 50%; transform: translateY(-50%); z-index: 9;}
.carousel .next {left: auto; right: 0;}

@media only screen and (max-width: 600px) {
  .carousel ul li div {display: none;}

/* DEMO CSS */
.carousel ul li:nth-child(2) {background-image: url(;}
.carousel ul li:nth-child(3) {background-image: url(;}
.carousel ul li:nth-child(4) {background-image: url(;}
p, h2 {margin: 15px auto; padding: 0 15px; max-width: 800px;}
h2 {margin-top: 30px;}


                document.addEventListener('DOMContentLoaded', function() {
  const carousels = document.querySelectorAll('.carousel');
  carousels.forEach(function( carousel ) {

    const ele = carousel.querySelector('ul');
    const scrolllength = carousel.querySelector('ul li:nth-child(2)').offsetLeft - carousel.querySelector('ul li:nth-child(1)').offsetLeft;
    const amountvisible = Math.round(ele.offsetWidth/scrolllength);
    const bullets = carousel.querySelectorAll('ol li');
    const nextarrow = carousel.querySelector('.next');
    const prevarrow = carousel.querySelector('.prev');
    var removeels = carousel.querySelectorAll('ol li:nth-last-child(-n + '+(amountvisible-1)+')');
    removeels.forEach(function(removeel) {
    const setSelected = function() {
        bullets.forEach(function(bullet) {
        let nthchild = (Math.round((ele.scrollLeft/scrolllength)+1));
        carousel.querySelector('ol li:nth-child('+nthchild+')').classList.add('selected'); 

    const scrollTo = function(event) {
      ele.scrollLeft = ele.querySelector(this.getAttribute('href')).offsetLeft;
    const nextSlide = function() {
      if(!carousel.querySelector('ol li:last-child').classList.contains('selected')) {
        carousel.querySelector('ol li.selected').nextElementSibling.querySelector('a').click();
      } else {
        carousel.querySelector('ol li:first-child a').click();

    const prevSlide = function() {
      if(!carousel.querySelector('ol li:first-child').classList.contains('selected')) {
        carousel.querySelector('ol li.selected').previousElementSibling.querySelector('a').click();
      } else {
        carousel.querySelector('ol li:last-child a').click();
    // Attach the handlers
    ele.addEventListener("scroll", debounce (setSelected));
    nextarrow.addEventListener("click", nextSlide);
    prevarrow.addEventListener("click", prevSlide);
    bullets.forEach(function(bullet) {
        bullet.querySelector('a').addEventListener('click', scrollTo);
    //setInterval for autoplay
    if(carousel.getAttribute('duration')) {
      if (ele != document.querySelector(".carousel:hover ul")) {
      }, carousel.getAttribute('duration'));
  }); //end foreach
}); //end onload

 * Debounce functions for better performance
 * (c) 2021 Chris Ferdinandi, MIT License,
 * @param  {Function} fn The function to debounce
function debounce (fn) {
	// Setup a timer
	let timeout;
	// Return a function to run debounced
	return function () {
		// Setup the arguments
		let context = this;
		let args = arguments;
		// If there's a timer, cancel it
		if (timeout) {
		// Setup the new requestAnimationFrame()
		timeout = window.requestAnimationFrame(function () {
			fn.apply(context, args);