React Carousel Component
React carousel is a slideshow component for cycling through elements—images or slides of text—like a carousel.
Other frameworks
CoreUI components are available as native Angular, Bootstrap (Vanilla JS), and Vue components. To learn more please visit the following pages.
How it works#
The React carousel is a slideshow for cycling within a group of content. It runs with a group of images, text, or html elements. It also incorporates support for previous/next buttons.
In browsers where the Page Visibility API is supported, the carousel will avoid sliding when the webpage is not visible to the user (such as when the browser tab is inactive, the browser window is minimized, etc.).
Example#
Carousels don't automatically normalize slide dimensions. As such, you may want to use extra utilities or custom methods to properly size content. While carousels support previous/next controls and indicators, they're not explicitly expected. Add and customize as you see fit.
Slides only#
<CCarousel> <CCarouselItem> <CImage className="d-block w-100" src={ReactImg} alt="slide 1" /> </CCarouselItem> <CCarouselItem> <CImage className="d-block w-100" src={VueImg} alt="slide 2" /> </CCarouselItem> <CCarouselItem> <CImage className="d-block w-100" src={AngularImg} alt="slide 3" /> </CCarouselItem></CCarousel>
With controls#
Adding in the previous and next controls by controls
property.
<CCarousel controls> <CCarouselItem> <CImage className="d-block w-100" src={ReactImg} alt="slide 1" /> </CCarouselItem> <CCarouselItem> <CImage className="d-block w-100" src={VueImg} alt="slide 2" /> </CCarouselItem> <CCarouselItem> <CImage className="d-block w-100" src={AngularImg} alt="slide 3" /> </CCarouselItem></CCarousel>
With indicators#
You can attach the indicators to the carousel, lengthwise the controls, too.
<CCarousel controls indicators> <CCarouselItem> <CImage className="d-block w-100" src={ReactImg} alt="slide 1" /> </CCarouselItem> <CCarouselItem> <CImage className="d-block w-100" src={VueImg} alt="slide 2" /> </CCarouselItem> <CCarouselItem> <CImage className="d-block w-100" src={AngularImg} alt="slide 3" /> </CCarouselItem></CCarousel>
With captions#
You can add captions to slides with the <CCarouselCaption>
element within any <CCarouselItem>
. They can be immediately hidden on smaller viewports, as shown below, with optional display utilities. We hide them with .d-none
and draw them back on medium-sized devices with .d-md-block
.
<CCarousel controls indicators> <CCarouselItem> <CImage className="d-block w-100" src={ReactImg} alt="slide 1" /> <CCarouselCaption className="d-none d-md-block"> <h5>First slide label</h5> <p>Some representative placeholder content for the first slide.</p> </CCarouselCaption> </CCarouselItem> <CCarouselItem> <CImage className="d-block w-100" src={VueImg} alt="slide 2" /> <CCarouselCaption className="d-none d-md-block"> <h5>Second slide label</h5> <p>Some representative placeholder content for the first slide.</p> </CCarouselCaption> </CCarouselItem> <CCarouselItem> <CImage className="d-block w-100" src={AngularImg} alt="slide 3" /> <CCarouselCaption className="d-none d-md-block"> <h5>Third slide label</h5> <p>Some representative placeholder content for the first slide.</p> </CCarouselCaption> </CCarouselItem></CCarousel>
Crossfade#
Add transition="crossfade"
to your carousel to animate slides with a fade transition instead of a slide.
<CCarousel controls transition="crossfade"> <CCarouselItem> <CImage className="d-block w-100" src={ReactImg} alt="slide 1" /> </CCarouselItem> <CCarouselItem> <CImage className="d-block w-100" src={VueImg} alt="slide 2" /> </CCarouselItem> <CCarouselItem> <CImage className="d-block w-100" src={AngularImg} alt="slide 3" /> </CCarouselItem></CCarousel>
Dark variant#
Add dark
property to the CCarousel
for darker controls, indicators, and captions. Controls have been inverted from their default white fill with the filter
CSS property. Captions and controls have additional Sass variables that customize the color
and background-color
.
<CCarousel controls indicators dark> <CCarouselItem> <CImage className="d-block w-100" src={ReactImg} alt="slide 1" /> <CCarouselCaption className="d-none d-md-block"> <h5>First slide label</h5> <p>Some representative placeholder content for the first slide.</p> </CCarouselCaption> </CCarouselItem> <CCarouselItem> <CImage className="d-block w-100" src={VueImg} alt="slide 2" /> <CCarouselCaption className="d-none d-md-block"> <h5>Second slide label</h5> <p>Some representative placeholder content for the first slide.</p> </CCarouselCaption> </CCarouselItem> <CCarouselItem> <CImage className="d-block w-100" src={AngularImg} alt="slide 3" /> <CCarouselCaption className="d-none d-md-block"> <h5>Third slide label</h5> <p>Some representative placeholder content for the first slide.</p> </CCarouselCaption> </CCarouselItem></CCarousel>
Customizing#
SASS variables#
$carousel-control-color: $high-emphasis-inverse !default;$carousel-control-width: 15% !default;$carousel-control-opacity: .5 !default;$carousel-control-hover-opacity: .9 !default;$carousel-control-transition: opacity .15s ease !default;
$carousel-indicator-width: 30px !default;$carousel-indicator-height: 3px !default;$carousel-indicator-hit-area-height: 10px !default;$carousel-indicator-spacer: 3px !default;$carousel-indicator-opacity: .5 !default;$carousel-indicator-active-bg: $white !default;$carousel-indicator-active-opacity: 1 !default;$carousel-indicator-transition: opacity .6s ease !default;
$carousel-caption-width: 70% !default;$carousel-caption-color: $high-emphasis-inverse !default;$carousel-caption-padding-y: 1.25rem !default;$carousel-caption-spacer: 1.25rem !default;
$carousel-control-icon-width: 2rem !default;
$carousel-control-prev-icon-bg: url("data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16' fill='#{$carousel-control-color}'><path d='M11.354 1.646a.5.5 0 0 1 0 .708L5.707 8l5.647 5.646a.5.5 0 0 1-.708.708l-6-6a.5.5 0 0 1 0-.708l6-6a.5.5 0 0 1 .708 0z'/></svg>") !default;$carousel-control-next-icon-bg: url("data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16' fill='#{$carousel-control-color}'><path d='M4.646 1.646a.5.5 0 0 1 .708 0l6 6a.5.5 0 0 1 0 .708l-6 6a.5.5 0 0 1-.708-.708L10.293 8 4.646 2.354a.5.5 0 0 1 0-.708z'/></svg>") !default;
$carousel-transition-duration: .6s !default;$carousel-transition: transform $carousel-transition-duration ease-in-out !default; // Define transform transition first if using multiple transitions (e.g., `transform 2s ease, opacity .5s ease-out`)
API#
CCarousel#
import { CCarousel } from '@coreui/react'// orimport CCarousel from '@coreui/react/src/components/carousel/CCarousel'
Property | Description | Type | Default |
---|---|---|---|
activeIndex | index of the active item. | number | 0 |
className | A string of all className you want applied to the base component. | string | - |
controls | Adding in the previous and next controls. | boolean | - |
dark | Add darker controls, indicators, and captions. | boolean | - |
indicators | Adding indicators at the bottom of the carousel for each item. | boolean | - |
interval | The amount of time to delay between automatically cycling an item. If false, carousel will not automatically cycle. | number | boolean | 5000 |
onSlid | Callback fired when a slide transition end. | (active: number, direction: string) => void | - |
onSlide | Callback fired when a slide transition starts. | (active: number, direction: string) => void | - |
pause | If set to 'hover', pauses the cycling of the carousel on mouseenter and resumes the cycling of the carousel on mouseleave. If set to false, hovering over the carousel won't pause it. | boolean | 'hover' | hover |
touch 4.5.0+ | Set whether the carousel should support left/right swipe interactions on touchscreen devices. | boolean | true |
transition | Set type of the transition. | 'slide' | 'crossfade' | - |
wrap | Set whether the carousel should cycle continuously or have hard stops. | boolean | true |
CCarouselCaption#
import { CCarouselCaption } from '@coreui/react'// orimport CCarouselCaption from '@coreui/react/src/components/carousel/CCarouselCaption'
Property | Description | Type | Default |
---|---|---|---|
className | A string of all className you want applied to the base component. | string | - |
CCarouselItem#
import { CCarouselItem } from '@coreui/react'// orimport CCarouselItem from '@coreui/react/src/components/carousel/CCarouselItem'
Property | Description | Type | Default |
---|---|---|---|
className | A string of all className you want applied to the base component. | string | - |
interval | The amount of time to delay between automatically cycling an item. | number | boolean | false |