Options
Embla Carousel takes various options in order to customize how the carousel works.
Usage
You can provide options in two different ways: With the constructor options and/or global options. If both are provided, they will be merged, and if any options are in conflict, the constructor option has precedence and will override global options.
Constructor options
The constructor options is the default way of providing options to Embla Carousel. In the following example, the carousel loop option is set to true
:
import EmblaCarousel from 'embla-carousel'
const emblaNode = document.querySelector('.embla')const emblaApi = EmblaCarousel(emblaNode, { loop: true })
import useEmblaCarousel from 'embla-carousel-react'
export function EmblaCarousel() { const [emblaRef] = useEmblaCarousel({ loop: true })
// ...}
<script setup> import emblaCarouselVue from 'embla-carousel-vue'
const [emblaRef] = emblaCarouselVue({ loop: true })
// ...</script>
import createEmblaCarousel from 'embla-carousel-solid'
export function EmblaCarousel() { const [emblaRef] = createEmblaCarousel(() => ({ loop: true }))
// ...}
<script> import emblaCarouselSvelte from 'embla-carousel-svelte'
let options = { loop: true }</script>
<div class="embla" use:emblaCarouselSvelte="{{ options }}">...</div>
Global options
Setting global options will be applied to all carousels which will override the Embla default options with your own. In the following example loop is set to true
:
import EmblaCarousel from 'embla-carousel'
EmblaCarousel.globalOptions = { loop: true }
const emblaNode = document.querySelector('.embla')const emblaApi = EmblaCarousel(emblaNode, { align: 'start' })
import useEmblaCarousel from 'embla-carousel-react'
useEmblaCarousel.globalOptions = { loop: true }
export function EmblaCarousel() { const [emblaRef] = useEmblaCarousel({ align: 'start' })
// ...}
<script setup> import emblaCarouselVue from 'embla-carousel-vue'
emblaCarouselVue.globalOptions = { loop: true }
const [emblaRef] = emblaCarouselVue({ align: 'start' })
// ...</script>
import createEmblaCarousel from 'embla-carousel-solid'
createEmblaCarousel.globalOptions = { loop: true }
export function EmblaCarousel() { const [emblaRef] = createEmblaCarousel(() => ({ align: 'start' }))
// ...}
<script> import emblaCarouselSvelte from 'embla-carousel-svelte'
emblaCarouselSvelte.globalOptions = { loop: true }
let options = { align: 'start' }</script>
<div class="embla" use:emblaCarouselSvelte="{{ options }}">...</div>
Make sure to assign global options before initializing any carousel and only assign it once. Re-assigning global options might lead to confusing code and unexpected behaviour.
Changing options
It's possible to change options passed to the Embla Carousel constructor after initialization with the reInit method.
In React, Vue, Solid and Svelte wrappers you can pass reactive options and the carousel will automatically reinitialize when they change. Here are some examples:
import EmblaCarousel from 'embla-carousel'
const emblaNode = document.querySelector('.embla')const emblaApi = EmblaCarousel(emblaNode, { loop: true })
emblaApi.reInit({ loop: false })
import { useState, useCallback } from 'react'import useEmblaCarousel from 'embla-carousel-react'
export function EmblaCarousel() { const [options, setOptions] = useState({ loop: true }) const [emblaRef, emblaApi] = useEmblaCarousel(options)
const toggleLoop = useCallback(() => { setOptions((currentOptions) => ({ ...currentOptions, loop: !currentOptions.loop })) }, [])
// ...}
<script setup> import emblaCarouselVue from 'embla-carousel-vue'
const options = ref({ loop: true }) const [emblaRef, emblaApi] = emblaCarouselVue(options)
function toggleLoop() { options.value = { ...options.value, loop: !options.value.loop } }
// ...</script>
import { createSignal } from 'solid-js'import createEmblaCarousel from 'embla-carousel-solid'
export function EmblaCarousel() { const [options, setOptions] = createSignal({ loop: true }) const [emblaRef] = createEmblaCarousel(() => options())
function toggleLoop() { setOptions((currentOptions) => ({ ...currentOptions, loop: !currentOptions.loop })) }
// ...}
<script> import emblaCarouselSvelte from 'embla-carousel-svelte'
let options = { loop: true }
function toggleLoop() { options = { ...options, loop: !options.loop } }</script>
<div class="embla" use:emblaCarouselSvelte="{{ options }}">...</div>
TypeScript
The EmblaOptionsType
is obtained directly from the core package embla-carousel
and used like so:
import EmblaCarousel, { EmblaOptionsType } from 'embla-carousel'
const emblaNode = document.querySelector('.embla')const options: EmblaOptionsType = { loop: true }const emblaApi = EmblaCarousel(emblaNode, options)
import React from 'react'import { EmblaOptionsType } from 'embla-carousel'import useEmblaCarousel from 'embla-carousel-react'
type PropType = { options?: EmblaOptionsType}
export function EmblaCarousel(props: PropType) { const [emblaRef, emblaApi] = useEmblaCarousel(props.options)
// ...}
If you're using pnpm
, you need to install embla-carousel
as a
devDependency when importing types from it like demonstrated above.
This is because even though embla-carousel-react
has embla-carousel
as a
dependency, pnpm
makes nested dependencies inaccessible by design.
<script setup lang="ts"> import { EmblaOptionsType } from 'embla-carousel' import emblaCarouselVue from 'embla-carousel-vue'
const options: EmblaOptionsType = { loop: true } const [emblaRef] = emblaCarouselVue(options)
// ...</script>
If you're using pnpm
, you need to install embla-carousel
as a
devDependency when importing types from it like demonstrated above.
This is because even though embla-carousel-vue
has embla-carousel
as a
dependency, pnpm
makes nested dependencies inaccessible by design.
import { EmblaOptionsType } from 'embla-carousel'import createEmblaCarousel from 'embla-carousel-solid'
type PropType = { options?: EmblaOptionsType}
export function EmblaCarousel(props) { const [emblaRef] = createEmblaCarousel(props.options)
// ...}
If you're using pnpm
, you need to install embla-carousel
as a
devDependency when importing types from it like demonstrated above.
This is because even though embla-carousel-solid
has embla-carousel
as a
dependency, pnpm
makes nested dependencies inaccessible by design.
<script> import { EmblaOptionsType } from 'embla-carousel' import emblaCarouselSvelte from 'embla-carousel-svelte'
let options: EmblaOptionsType = { loop: true }</script>
<div class="embla" use:emblaCarouselSvelte="{{ options }}">...</div>
If you're using pnpm
, you need to install embla-carousel
as a
devDependency when importing types from it like demonstrated above.
This is because even though embla-carousel-svelte
has embla-carousel
as a
dependency, pnpm
makes nested dependencies inaccessible by design.
Reference
Below follows an exhaustive list of all Embla Carousel options and their default values.
active
Type: boolean
Default: true
Setting this to false
will not activate or deactivate the carousel. Useful when used together with the breakpoints option to toggle the carousel active/inactive depending on media queries.
align
Type: string | (viewSize: number, snapSize: number, index: number) => number
Default: center
Align the slides relative to the carousel viewport. Use one of the predefined alignments start
, center
or end
. Alternatively, provide your own callback to fully customize the alignment.
axis
Type: string
Default: x
Choose scroll axis between x
and y
. Remember to stack your slides horizontally or vertically using CSS to match this option.
breakpoints
Type: EmblaOptionsType
Default: {}
An object with options that will be applied for a given breakpoint by overriding the options at the root level. Example: '(min-width: 768px)': { loop: false }
.
Note: If multiple queries match, they will be merged. And when breakpoint options clash, the last one in the list has precedence.
container
Type: string | HTMLElement | null
Default: null
Enables choosing a custom container element which holds the slides. By default, Embla will choose the first direct child element of the root element. Provide either a valid CSS selector string
or a HTML element
.
containScroll
Type: false
| string
Default: 'trimSnaps'
Clear leading and trailing empty space that causes excessive scrolling. Use trimSnaps
to only use snap points that trigger scrolling or keepSnaps
to keep them.
Note: When this is active, it will override alignments applied by the align option for enough slides at the start and the end of the carousel, in order to cover the leading and trailing space.
direction
Type: string
Default: ltr
Choose content direction between ltr
and rtl
.
Note: When using rtl
, the content direction also has to be set to RTL,
either by using the HTML dir
attribute
or the CSS
direction
property.
dragFree
Type: boolean
Default: false
Enables momentum scrolling. The duration of the continued scrolling is proportional to how vigorous the drag gesture is.
dragThreshold
Type: number
Default: 10
Drag threshold in pixels. This only affects when clicks are fired and not. In contrast to other carousel libraries, it will not affect when dragging of the carousel starts.
Note: Browsers handle touch events differently than mouse events. Browsers won't fire the click event when a touch event includes an accidental slight swipe gesture. This is why this threshold only works for mouse events.
duration
Type: number
Default: 25
Set scroll duration when triggered by any of the API methods. Higher numbers enables slower scrolling. Drag interactions are not affected because duration is then determined by the drag force.
Note: Duration is not in milliseconds because Embla uses an attraction
physics simulation when scrolling instead of easings. Only values between
20
-60
are recommended.
inViewThreshold
Type: IntersectionObserverInit.threshold
Default: 0
This is the Intersection Observer threshold option that will be applied to all slides.
loop
Type: boolean
Default: false
Enables infinite looping. Embla will apply translateX
or translateY
to the slides that need to change position in order to create the loop effect.
Embla automatically falls back to false
if slide content isn't enough to
create the loop effect without visible glitches.
skipSnaps
Type: boolean
Default: false
Allow the carousel to skip scroll snaps if it's dragged vigorously. Note that this option will be ignored if the dragFree option is set to true
.
slides
Type: string | HTMLElement[] | NodeListOf<HTMLElement> | null
Default: null
Enables using custom slide elements. By default, Embla will choose all direct child elements of its container. Provide either a valid CSS selector string
or a nodeList/array
containing HTML elements
.
Note: Even though it's possible to provide custom slide elements, they still have to be direct descendants of the carousel container.
Warning: If you place elements inside the carousel container that aren't
slides, they either shouldn't have any size, or should be detached from the
document flow with position: absolute
or similar.
slidesToScroll
Type: string | number
Default: 1
Group slides together. Drag interactions, dot navigation, and previous/next buttons are mapped to group slides into the given number, which has to be an integer. Set it to auto
if you want Embla to group slides automatically.
startIndex
Type: number
Default: 0
Set the initial scroll snap to the given number. First snap index starts at 0
. Please note that this is not necessarily equal to the number of slides when used together with the slidesToScroll option.
watchDrag
Type: boolean | (emblaApi: EmblaCarouselType, event: MouseEvent | TouchEvent) => boolean | void
Default: true
Enables for scrolling the carousel with mouse and touch interactions. Set this to false
to disable drag events or pass a custom callback to add your own drag logic.
Note: When passing a custom callback it will run before the default
Embla drag behaviour. Return true
in your callback if you want Embla to run
its default drag behaviour after your callback, or return false
if you want
to skip it.
watchFocus
Type: boolean | (emblaApi: EmblaCarouselType, event: FocusEvent) => boolean | void
Default: true
Embla automatically watches the slides for focus events. The default callback fires the slideFocus event and scrolls to the focused element. Set this to false
to disable this behaviour or pass a custom callback to add your own focus logic.
Note: When passing a custom callback it will run before the default
Embla focus behaviour. Return true
in your callback if you want Embla to run
its default focus behaviour after your callback, or return false
if you want
to skip it.
watchResize
Type: boolean | (emblaApi: EmblaCarouselType, entries: ResizeObserverEntry[]) => boolean | void
Default: true
Embla automatically watches the container and slides for size changes and runs reInit when any size has changed. Set this to false
to disable this behaviour or pass a custom callback to add your own resize logic.
Note: When passing a custom callback it will run before the default
Embla resize behaviour. Return true
in your callback if you want Embla to
run its default resize behaviour after your callback, or return false
if you
want to skip it.
watchSlides
Type: boolean | (emblaApi: EmblaCarouselType, mutations: MutationRecord[]) => boolean | void
Default: true
Embla automatically watches the container for added and/or removed slides and runs reInit if needed. Set this to false
to disable this behaviour or pass a custom callback to add your own slides changed logic.
Note: When passing a custom callback it will run before the default
Embla mutation behaviour. Return true
in your callback if you want Embla to
run its default mutation behaviour after your callback, or return false
if
you want to skip it.