Tailwind CSS Carousel Plugin API
Headless, unstyled carousel plugin built with JavaScript and TypeScript, including installation, usage, options, methods, events, and selectors.
Installation
To get started, install Carousel plugin via npm, else you can skip this step if you are already using Preline UI as a package.
-
Install the plugin
Install
@preline/carouselvia npmTerminalnpm i @preline/carousel -
Add the plugin CSS
Use @source to register the plugin's JavaScript path for Tailwind CSS scanning, then @import the plugin's CSS files into your Tailwind CSS file.
main.css@import "tailwindcss"; /* @preline/carousel */ /* [!code highlight:3] */ @source "../node_modules/@preline/carousel/*.js"; @import "./node_modules/@preline/carousel/variants.css"; @import "./node_modules/@preline/carousel/theme.css"; -
Add the plugin JavaScript
Include the JavaScript
<script>that powers the interactive elements near the end of your</body>tag:HTML
Additional Initialization Options
Use the
non-autoentry if you need manual initialization. In this mode, automatic initialization on page load is not included, so the component should be initialized explicitly.HTML (non-auto)
Via bundler
When using a bundler (Vite, webpack, etc.), import the plugin directly as an ES module.
JavaScript (auto)import "@preline/carousel";JavaScript (non-auto)import HSCarousel from "@preline/carousel/non-auto"; HSCarousel.autoInit(); // Or initialize a specific element manually const el = document.querySelector("#carousel"); if (el) new HSCarousel(el);
Basic usage
Prefer to create your own style? Here is a completely unstylized example.
<div data-hs-carousel='{
"loadingClasses": "opacity-0"
}' class="relative">
<div class="hs-carousel relative overflow-hidden w-full min-h-64">
<div class="hs-carousel-body absolute top-0 bottom-0 inset-s-0 flex flex-nowrap transition-transform duration-700 opacity-0">
<div class="hs-carousel-slide">
1
</div>
<div class="hs-carousel-slide">
2
</div>
<div class="hs-carousel-slide">
3
</div>
</div>
</div>
<button type="button" class="hs-carousel-prev">
<span aria-hidden="true">«</span>
<span class="sr-only">Previous</span>
</button>
<button type="button" class="hs-carousel-next">
<span class="sr-only">Next</span>
<span aria-hidden="true">»</span>
</button>
</div>
Methods
The HSCarousel object is contained within the global window object
Go to some slide by button click example (public method).
const carousel = new HSCarousel(document.querySelector('#carousel'));
const goTo2Btn = document.querySelector('#go-to-2-btn');
goTo2Btn.addEventListener('click', () => {
carousel.goTo(1);
});
Go to some slide by button click example (mixed).
const carousel = HSCarousel.getInstance('#carousel');
const goTo2Btn = document.querySelector('#go-to-2-btn');
goTo2Btn.addEventListener('click', () => {
carousel.goTo(1);
});
Ready to use Components
Looking for prebuilt UI components based on the Tailwind CSS? Preline UI packs hundreds of component examples for all your website needs.
Carousel
Explore ready-to-use Tailwind CSS examples built with Preline UI.
View Carousel examples