Search code examples
javascripthtmlcssflexboxslick.js

Slick slider not working in a flex container


I'm trying to center vertically a slick carousel (http://kenwheeler.github.io/slick/) in a column. So I use display: flex; align-items: center; on the column (container of the slick slider) but it breaks the slider.

So I have try to center the carousel with an absolute position or by using flexbox but they both breaks the slick slider.

I hope someone got a css/js/jquery solution for this issue :)

Here a fiddle of the issue : https://jsfiddle.net/victor_allegret/g3dsd78w/

(sorry I have an issue adding slick to the stack overflow snippet)

HTML :

<div class='flex-container'>

 <div class='single-item'>

  <div><h3>1</h3></div>
  <div><h3>2</h3></div>
  <div><h3>3</h3></div>
  <div><h3>4</h3></div>
  <div><h3>5</h3></div>
  <div><h3>6</h3></div>

 </div>

</div>

CSS :

.flex-container {
 /* Using flexbox */
 display: flex;
 align-items: center;
 /*----*/
 margin: 0 auto;
 padding: 40px;
 height: 500px;
 width: 80%;
 color: #333;
 background: #419be0;
}

.slick-slide {
 text-align: center;
 color: #419be0;
 background: white;
}

JS :

$(".single-item").slick({
 dots: true
});

Solution

  • Like this ? With absolute position.

    .abs-container {
      position:absolute;
    
      height:140px;
      width:300px;
      top:50%;
      left:50%;
      margin:-70px 0 0 -150px;
    
      color: #333;
      background: #419be0;
    }
    

    And this with FlexBox.

    .flex-container {
        position:absolute;
        top:0px;
        bottom:0px;
        width:100%;
        height:100%;
    
      /* Using flexbox */
        display: flex;
        flex-direction: row;
        flex-wrap: nowrap;
        justify-content: center;
        align-content: stretch;
        align-items: center;
      /*----*/
    
      color: #333;
      background: #419be0;
    }
    .flex-child {
        width: 300px;
        order: 0;
        flex: 0 1 auto;
        align-self: center;
    }
    

    HTML

    <div class='flex-container'>
        <div class='flex-child'>
          <div class='single-item'>
          .
          .
          .