Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I center items in Slick Carousel?

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.

like image 937
cjm Avatar asked Oct 14 '15 15:10

cjm


2 Answers

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.

like image 166
spenibus Avatar answered Nov 08 '22 00:11

spenibus


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/

like image 24
IMB Avatar answered Nov 08 '22 01:11

IMB