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.
<pre class="brush:php;toolbar:false"><div class="wrapper">
<nav class="lil-nav"></nav>
<div class="gallery"></div>
</div></pre>
<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>
<pre class="brush:php;toolbar:false"><nav class="lil-nav">
<a href="https://www.php.cn/link/c60a617f9800bf968672bda753787d99">
<img src="/static/imghwm/default1.png" data-src="https://img.php.cn/?x-oss-process=image/resize,p_40" class="lazy" alt="How to Make a CSS-Only Carousel ">
</a>
<a href="https://www.php.cn/link/911b10b73d26005b82c8d2b5256f2403">
<img src="/static/imghwm/default1.png" data-src="..." class="lazy" alt="Basketball hoop">
</a>
</nav>
<div class="gallery">
<img src="/static/imghwm/default1.png" data-src="..." class="lazy" id="image-1" alt="How to Make a CSS-Only Carousel">
<img src="/static/imghwm/default1.png" data-src="..." class="lazy" id="image-2" alt="Basketball hoop">
</div></pre>
<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!

Deploying like an idiot comes down to a mismatch between the tools you use to deploy and the reward in complexity reduced versus complexity added.

There was once upon a time when native CSS lacked many essential features, leaving developers to come up with all sorts of ways to make CSS easier to write over the years.

HTML 5 Readiness was a site that showed through a rainbow of colors the browser support for several web features. What about a new version?

One thing we can do to help teams code consistently is provide type-checking so that all of the configurable options for a specific component are available while coding. Bryan demonstrates how he does this with TypeScript when working with Astro comp

If you've ever had to display an interactive animation during a live talk or a class, then you may know that it's not always easy to interact with your slides

With Astro, we can generate most of our site during our build, but have a small bit of server-side code that can handle search functionality using something like Fuse.js. In this demo, we’ll use Fuse to search through a set of personal “bookmarks” th

I wanted to implement a notification message in one of my projects, similar to what you’d see in Google Docs while a document is saving. In other words, a

Some months ago I was on Hacker News (as one does) and I ran across a (now deleted) article about not using if statements. If you’re new to this idea (like I


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

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Zend Studio 13.0.1
Powerful PHP integrated development environment

SublimeText3 English version
Recommended: Win version, supports code prompts!