6

I'm using the Slick Carousel, but there's one layout itch I can't scratch. When I have fewer items than the carousel can display at once, they're left-justified. I'd like them to be centered. For example, with slidesToShow equal 3 and only 2 items, I get

-------------------
 IIIII IIIII
-------------------

when what I want is

-------------------
    IIIII IIIII
-------------------

or if there's only 1 item:

-------------------
       IIIII
-------------------

I've tried setting centerMode, but that's a completely different effect.

Is there a way to get the effect I want? It needs to handle slidesToShow being reduced by responsive breakpoints.

2 Answers 2

13

A quick inspection of the carousel's DOM shows that the items are put in an element with the class slick-track. It is possible to center the items by modifying the margins of that element:

.slick-track {
    margin:auto;
}

Demo in this JSFiddle.

Note that the carousel in the demo sometimes stops working correctly when being resized while the last item is active but that happens regardless of this CSS modification.

2
  • That looks like it, except I used margin-left:auto; margin-right:auto; instead.
    – cjm
    Oct 14, 2015 at 19:00
  • I had this problem and I had to use margin: 0; to get it centered Aug 5, 2018 at 18:26
8

Putting margin:0 auto; on the <img> tag worked for me.

E.g.,

<div class="items">
    <div>
        <img src="https://via.placeholder.com/25x50" />
    </div>
    <div>
        <img src="https://via.placeholder.com/25x50" />
    </div>
    <div>
        <img src="https://via.placeholder.com/25x50" />
    </div>
<div>

CSS

.items { background:red; }
.items img { margin:0 auto; }

https://jsfiddle.net/cegor9j2/

1
  • Do you know how to get this to work if the number of slides in the carousel is less than the slidesToShow parameter?
    – Sean H
    Mar 9, 2022 at 12:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.