UIKit Slider Component
Live Examples
1. Basic Responsive Slider
The Complete Guide to the UIKit Slider Component
In modern web design, effectively displaying large collections of content within a limited amount of screen real estate is a constant challenge. Whether you are showcasing an e-commerce product catalog, a portfolio of design work, or a row of client testimonials, the UIKit Slider component (often referred to as a Carousel) is the definitive solution.
Unlike the UIKit Slideshow component (which typically displays one massive hero image at a time), the Slider component is specifically engineered to display multiple items side-by-side, allowing the user to seamlessly scroll or swipe horizontally through the list.
Understanding the Slider Architecture
The UIKit Slider is built upon a specific hierarchy of HTML containers. Understanding this markup structure is critical for the component to function correctly.
- The Component Wrapper (
uk-slider): This is the outermost boundary of the slider. The JavaScript engine looks for this attribute to initialize the carousel logic. - The Track (
.uk-slider-items): This is the internal container (usually a<ul>tag) that holds all the individual slides. This container actually moves left and right when the slider is activated. - The Slides (
<li>): These are the direct children of the track. Each slide represents one item (e.g., one image or one product card).
Mastering Responsive Item Widths
The true power of the UIKit Slider lies in its integration with the UIKit Grid and Width systems. You do not need to write complex media queries in CSS to define how many items show on a phone versus a desktop monitor; UIKit handles this declaratively via HTML classes applied to the .uk-slider-items container.
By using the uk-child-width-* classes, you define exactly what fraction of the container each slide should consume at specific breakpoints:
In the example above, the slider behaves perfectly across all devices:
- Mobile (Default):
uk-child-width-1-1forces each slide to take up 100% of the screen. The user sees one item at a time. - Tablet (
@s):uk-child-width-1-3@skicks in, showing exactly 3 items side-by-side. - Desktop (
@l):uk-child-width-1-5@lkicks in, showcasing 5 items horizontally.
You must also add the uk-grid class to the .uk-slider-items container if you want standard gaps (gutters) between your slides. Without it, the images will touch edge-to-edge.
Center Mode for Highlighted Content
A very popular modern UX pattern is "Center Mode." Instead of aligning the active slide to the far left of the container, center mode places the active slide dead-center on the screen, allowing users to peek at the previous and next slides cut off on the edges of the viewport.
You can activate this by passing the center: true parameter to the component:
When combined with custom CSS to lower the opacity of non-centered items, this creates a highly engaging, interactive focal point for galleries.
Adding Navigation: Arrows and Dots
While touch-device users will naturally swipe the slider, desktop users relying on a mouse require visual navigation controls. UIKit provides seamless integration with two types of navigation: Slidenav (arrows) and Dotnav (pagination dots).
1. Slidenav (Arrows): To add previous/next arrows, you place anchor tags inside the main wrapper (but outside the track) and use the uk-slider-item="previous" and "next" attributes. By applying positioning utilities like uk-position-center-left, you can overlay the arrows directly on top of the images.
2. Dotnav (Pagination): To add dots at the bottom indicating how many "pages" of content exist, you add a <ul class="uk-slider-nav uk-dotnav"></ul> element below the track. The UIKit JavaScript engine will automatically count the slides and generate the correct number of dots, binding click events to them automatically.
Autoplay Logic
If you are building a sponsor banner or a news ticker, you likely want the slider to move automatically without user interaction. UIKit supports robust autoplay functionality via component parameters.
autoplay: true: Turns on automatic scrolling.autoplay-interval: 3000: Defines the delay between transitions in milliseconds (e.g., 3 seconds).pause-on-hover: true: (Default) Automatically pauses the autoplay timer if the user hovers their mouse over the slider, ensuring they can read text without the slide disappearing.
Touch and Drag Interactions
Out of the box, the UIKit Slider provides native-feeling swipe gestures for mobile devices and mouse-drag gestures for desktop users. The physics of the drag—including momentum and snapping back to the nearest slide—are handled automatically by the JavaScript engine.
However, if you are placing complex interactive elements inside the slides (like a map you need to pan, or text you want the user to highlight and copy), the drag behavior can interfere. You can disable mouse dragging by setting draggable: false.
Performance Considerations: Lazy Loading
Sliders are notorious for destroying page load performance because they often contain dozens of high-resolution images. If you have 20 photos in a slider, the browser attempts to download all 20 immediately, even though the user only sees the first 3 on load.
UIKit offers a brilliant native solution: Lazy Loading. Instead of using standard <img src="..."> tags, you can use the UIKit Image component logic. By replacing src with data-src and adding the uk-img attribute, the slider becomes intelligent.
By targeting the slider track, UIKit will monitor the scroll position of the carousel. It will only download images when they are about to slide into the user's viewport, saving massive amounts of bandwidth and ensuring a perfect PageSpeed score.
