Pen Settings

HTML

CSS

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. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

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

Packages

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.

Behavior

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.

HTML

              
                <section class="slider">
   
   <h1>
      CSS slider <br/>
      <span>Reveal all the slides <br />
         on page load, <br />
         show them on page scroll.
      </span>   
   </h1>

   <a href="https://dev.to/fcalderan/a-css-carousel-with-snapping-points-and-a-scroll-linked-navigation-5h6j" target="_blank">Read the article on dev.to</a>
   
   <div>
      <ul id="s">
         <li id="s1">
            <img src="//fabrizio.dev/demo/tesseract1.jpg" 
                 alt="Daniel Tompkins (Portals 2020)" />
         </li>
         <li id="s2">
            <img src="//fabrizio.dev/demo/tesseract2.jpg" 
                 alt="TesseracT (Portals 2020)" />
         </li>
         <li id="s3">
            <img src="//fabrizio.dev/demo/tesseract3.jpg" 
                 alt="Amos Prem Williams (Portals 2020)"/>
         </li>
         <li id="s4">
            <img src="//fabrizio.dev/demo/tesseract4.jpg" 
                 alt="TesseracT (Portals 2020)"/>
         </li>
         <li id="s5">
            <img src="//fabrizio.dev/demo/tesseract5.jpg" 
                 alt="TesseracT (Portals 2020)"/>
         </li>
      </ul>
   </div>
   
   <nav>
      <a href="#s1" title="title slide 1">
         Go to Slide 1</a>
      <a href="#s2" title="title slide 2">
         Go to Slide 2</a>
      <a href="#s3" title="title slide 3">
         Go to Slide 3</a>
      <a href="#s4" title="title slide 4">
         Go to Slide 4</a>
      <a href="#s5" title="title slide 5">
         Go to Slide 5</a>
   </nav>
   
   
   <p class="warning">
      Your browser doesn't support 
      <code>animation-timeline</code>. 
      <br /><br />Please, try this demo on 
      Chrome Canary with the
      <code>--enable-experimental-web-platform-features</code> flag enabled.
   </p>
   
   
      
   <p>
      Use the navigation or swipe left/right
      on the carousel.
   </p>
   
   <p>
      <b>— No JS required —</b><br /><br />
      When you click on a dot (anchor) the 
      page could scroll vertically so I've 
      set a smooth 
      <code>scroll-behavior</code> and a 
      <code>scroll-padding-top</code> on the root. 
      Every dot is a permalink as well, thus 
      you may even share a URL showing a specific 
      slide of the carousel on page load.
   </p>
   
   <p>
      <a class="demo" href="https://codepen.io/fcalderan/pen/KKmKpaZ" 
         target="_top">Basic demo</a>
      
         <a class="demo" href="https://codepen.io/fcalderan/pen/abWObbx"
            target="_top">Demo with images</a>
      
      <a class="demo" href="https://twitter.com/fcalderan"
         target="_top">Follow me on Twitter</a>
   </p>
</section>
              
            
!

CSS

              
                html { 
   scroll-behavior: smooth; 
   scroll-padding-top: 2rem;
}


.slider div {
   /* need to hide the horizontal scrollbar of 
    * the inner list
    */
   overflow: hidden;   
   
   /* using logical properties instead of
    * width and/or height
    */
   inline-size: 80%;
   min-inline-size: 300px;
   max-inline-size: 680px;
   
   margin: 4rem auto 3rem;
   aspect-ratio: 2 / 1;
   border: 1px #e2e2e2 solid;
   box-shadow: 0 0 60px rgba(0, 0, 20, .6);
   border-radius: 1rem;
}


.slider ul {
   display: flex;
   flex-flow: row nowrap;
   overflow-x: scroll;
   scroll-snap-type: x mandatory;
   scroll-behavior: smooth;
   cursor: ew-resize;   
   
   /* so as to clip away the scrollbar */
   block-size: calc(100% + 25px);
   
}

.slider li {
   flex: 0 0 100%;
   scroll-snap-align: start;
   scroll-snap-stop: always;
   /* restore the actual height of the slider */
   block-size: calc(100% - 25px);
}



.slider nav {
   position: relative;
   display: flex;
   margin: 0 auto;
   inline-size: max-content;
   
   
   /* DRY: set here the gap between the dots,
    * we will need it later to set the position
    * of the nav::before pseudoelement
    */
   --gap: .8rem;
   gap: var(--gap, .5rem);
}



.slider ul {
   overflow-x: hidden;
   animation: overflow .35s linear forwards;
   animation-timeline: pagescroll;
}

.slider li {
  flex-basis: 20%;
  animation: flex .35s linear forwards;
  animation-timeline: pagescroll;

}

.slider nav {
  opacity: 0;
  animation: opacity .35s linear forwards;
  animation-timeline: pagescroll;
}

.slider nav::after {
  content: "";
  position: absolute;
  display: block;
  cursor: not-allowed;
  z-index: 2;
  inline-size: 100%;
  block-size: 100%;
  animation: dotblock .35s linear forwards;
  animation-timeline: pagescroll;
}


@supports (animation-timeline: works) {

   
   
   .slider nav::before, .slider nav a {
      inline-size: 1rem;
      aspect-ratio: 1;
      border-radius: 50%;
      background: #9bc;
   }

   .slider nav a {
      text-indent: 100%;
      overflow: hidden;
      white-space: nowrap;
      opacity: .33;
   }


   .slider nav::before {
      content: "";
      position: absolute;
      z-index: 1;
      display: block;
      cursor: not-allowed;
      
      
      /* here we use the above --gap variable.
       * the --slide variable is set in the
       * keyframes below.
       */
      transform: translateX(
         calc((100% + var(--gap, .5rem)) * 
         calc(var(--slide, 1) - 1))
      );
      animation: dot 1s steps(1, end) forwards;
      /* shhh... here is the magic */
      animation-timeline: slide;
   }
   
   
   /* timeline animation */
   @scroll-timeline slide {
      source: selector(#s);
      orientation: inline; 
      time-range: 1s;
   }

   @keyframes dot {
      0%    { --slide: 1; }
      12.5% { --slide: 2; }
      37.5% { --slide: 3; } 
      62.5% { --slide: 4; }
      87.5%, 100% { --slide: 5; }
   }
   

   
   @scroll-timeline pagescroll {
      time-range: 1s;
      source: selector(body);
      start: selector(#s) end 0;
      scroll-offsets: 
         selector(#s) end 0;
     
   }
   
   @keyframes flex {
      0% { flex-basis: 20%;}
      100% { flex-basis: 100%; }
   }
   
   @keyframes opacity {
      0%  { opacity: 0; }
      100% { opacity: 1; }
   }
   
  @keyframes overflow {
      0%,98% { overflow-x: hidden; }
      98.001%, 100% { overflow-x: scroll; }
   }
   
   @keyframes dotblock {
      0%, 99.9% { inline-size: 100%; }
      99.91%, 100% { inline-size: 0; }
   }
   

}


@supports (not (animation-timeline: works)) {
   .slider nav {
      display: none;
   }
   
   :root:hover .slider .warning {
      display: block;
   }
}


/* end of relevant CSS code */























/* The following declarations are not strictly 
 * relevant for this demo to work, it's just to make it
 * more pleasant, but feel free to take a look
 */

.slider {
   text-align: center;
   line-height: 1.5;
   padding: 0 1rem;
}

.slider * {
   box-sizing: border-box;
   font-family: 'Hachi Maru Pop', cursive;
}

.slider h1 {
   font-weight: 700;
   font-size: 3.4rem;
   line-height: 1.2;
   margin-top: 2rem;
   margin-bottom: 7rem;
   color: #313131;
   padding: 0 3rem;
}

.slider h1 + a {
   color: yellowgreen;
   display: block;
   font-size: 1.4rem;
   margin-bottom: 9rem;
}

.slider h1 span {
   color: #9bc;
   display: block;
   font-size: 50%;
   line-height: 1.5;
   margin-top: 1rem;
}

.slider li {
   font-size: 2rem;
   color: yellowgreen;
}

.slider img {
   object-fit: cover;
   max-inline-size: 100%;
   block-size: 100%; 
}

.slider nav {
   margin-bottom: 2rem;
}

.slider .warning {
   position: fixed;
   display: none;
   left: 50%;
   top: 50%;
   box-shadow: 0 0 0 50vmax rgba(127,155,188, .75);
   text-align: left;
   transform: translate(-50%, -50%);
   font-size: 1.2rem;
   border-radius: 1.2rem;
   padding: 3rem;
   margin: 0 auto;
   line-height: 1.7;
   color: #313139;
   border: 1px yellowgreen solid;
   background: #fbfbff;
   inline-size: 86%;
   max-inline-size: 800px;
}

.slider code {
   background: #e6e9ef;
   padding: .1rem .2rem;
   border-radius: .25rem;
}

.slider p {
   display: block;
   inline-size: 80vw;
   margin: 0 auto;
   color: #313131;
   margin-bottom: 3rem;
}

.slider b {
   font-size: 1.2rem;
   color: #9bc;
}

.slider .demo {
   display: block;
   color: yellowgreen;
   font-size: 1.2rem;
   margin: 2rem auto 0 auto;
}


              
            
!

JS

              
                /* no JS */
              
            
!
999px

Console