How to Make a CSS-Only Carousel
This tutorial details creating a fully functional carousel using only CSS, eliminating the need for JavaScript or jQuery. We'll build a carousel with left-side image navigation and a larger image gallery on the right.
The foundation is simple HTML: a wrapper div containing a navigation (<nav></nav>
) and a gallery (<div>) element.
<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false"><div class="wrapper">
<nav class="lil-nav"></nav>
<div class="gallery"></div>
</div></pre><div class="contentsignin">Copy after login</div></div>
<p>Images, sourced from a free resource like Unsplash, are added to both the navigation and the gallery. Navigation images link to corresponding gallery images using IDs:</p>
<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false"><nav class="lil-nav">
<a href="https://www.php.cn/link/c60a617f9800bf968672bda753787d99">
<img src="/static/imghw/default1.png" data-src="https://img.php.cn/" class="lazy" alt="How to Make a CSS-Only Carousel ">
</a>
<a href="https://www.php.cn/link/911b10b73d26005b82c8d2b5256f2403">
<img src="/static/imghw/default1.png" data-src="..." class="lazy" alt="Basketball hoop">
</a>
</nav>
<div class="gallery">
<img src="/static/imghw/default1.png" data-src="..." class="lazy" id="image-1" alt="How to Make a CSS-Only Carousel">
<img src="/static/imghw/default1.png" data-src="..." class="lazy" id="image-2" alt="Basketball hoop">
</div></pre><div class="contentsignin">Copy after login</div></div>
<p>CSS provides the layout using <code>grid
:
img { display: block; max-width: 100%; } .wrapper { display: grid; grid-template-rows: 2fr 1fr; /* Default mobile layout */ grid-gap: 10px; overflow: hidden; height: 100vh; } .gallery { overflow-x: scroll; /* Default mobile scroll */ overflow-y: hidden; display: flex; scroll-snap-type: x mandatory; /* Enable snapping */ } .gallery__img { scroll-snap-align: start; min-height: 100vh; object-fit: cover; }
For larger screens, a media query adjusts the layout:
@media screen and (min-width: 1200px) { .wrapper { grid-template-columns: 1fr 5fr; grid-template-rows: auto; } .lil-nav { overflow-y: scroll; overflow-x: hidden; display: block; } .gallery { display: block; overflow-y: scroll; overflow-x: hidden; scroll-snap-type: y mandatory; } }
Smooth scrolling is achieved with scroll-behavior: smooth;
in the .gallery
styles. Further styling enhances the navigation images:
.lil-nav a { height: 200px; display: flex; margin-bottom: 10px; } .lil-nav__img { object-fit: cover; filter: saturate(0); transition: 0.3s ease all; } .lil-nav__img:hover { transform: scale(1.05); filter: saturate(1); }
This creates a responsive, CSS-only carousel. While functional, consider accessibility (e.g., screen reader compatibility) and performance optimizations (e.g., lazy loading) for production use.
The above is the detailed content of How to Make a CSS-Only Carousel. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











I see Google Fonts rolled out a new design (Tweet). Compared to the last big redesign, this feels much more iterative. I can barely tell the difference

Have you ever needed a countdown timer on a project? For something like that, it might be natural to reach for a plugin, but it’s actually a lot more

Everything you ever wanted to know about data attributes in HTML, CSS, and JavaScript.

At the start of a new project, Sass compilation happens in the blink of an eye. This feels great, especially when it’s paired with Browsersync, which reloads

Tartan is a patterned cloth that’s typically associated with Scotland, particularly their fashionable kilts. On tartanify.com, we gathered over 5,000 tartan

The inline-template directive allows us to build rich Vue components as a progressive enhancement over existing WordPress markup.

PHP templating often gets a bad rap for facilitating subpar code — but that doesn't have to be the case. Let’s look at how PHP projects can enforce a basic

Let’s attempt to coin a term here: "Static Form Provider." You bring your HTML
