htmlcsscarouselvertical-alignmentflickity

Vertically center a carousel inside a section


I have a really basic Flickity carousel with some images. My problem is it reamins on the top of the page and I want to center the whole carousel vertically inside my section. My section height is equal to my device's height, so its basically a fullscreen. I tried so many things but I dont know how to solve it. I tried to apply display:flex; and align-items:center; trick on my section but then it destroys the whole carousel...

My code:

**style.css** ```
/* external css: flickity.css */

* {
  box-sizing: border-box;
}

body {
  font-family: sans-serif;
}

#section-b {
  padding: 0px;
  margin: 0px;
  background: #0b1320;
  text-align: center;
  color: white;
  min-height: 100vh;
}

.carousel {
  background: #0b1320;
}

.carousel-cell {
  width: 70%;
  height: 200px;
  /* flex-box, center image in cell */
  display: -webkit-box;
  display: -webkit-flex;
  display: flex;
  -webkit-box-pack: center;
  -webkit-justify-content: center;
  justify-content: center;
  -webkit-align-items: center;
  align-items: center;
}

.carousel-cell img {
  display: block;
  max-width: 100%;
  max-height: 100%;
  /* dim unselected */
  opacity: 0.7;
  -webkit-transform: scale(0.85);
  transform: scale(0.85);
  -webkit-filter: blur(5px);
  filter: blur(5px);
  -webkit-transition: opacity 0.3s, -webkit-transform 0.3s, transform 0.3s, -webkit-filter 0.3s, filter 0.3s;
  transition: opacity 0.3s, transform 0.3s, filter 0.3s;
}


/* brighten selected image */

.carousel-cell.is-selected img {
  opacity: 1;
  -webkit-transform: scale(1);
  transform: scale(1);
  -webkit-filter: none;
  filter: none;
}

@media screen and (min-width: 768px) {
  .carousel-cell {
    height: 400px;
  }
}

@media screen and (min-width: 960px) {
  .carousel-cell {
    width: 60%;
  }
}


/* buttons, no circle */

.flickity-prev-next-button {
  width: 60px;
  height: 60px;
  background: transparent;
  opacity: 0.6;
}

.flickity-prev-next-button:hover {
  background: transparent;
  opacity: 1;
}


/* arrow color */

.flickity-prev-next-button .arrow {
  fill: white;
}

.flickity-prev-next-button.no-svg {
  color: white;
}


/* closer to edge */

.flickity-prev-next-button.previous {
  left: 0;
}

.flickity-prev-next-button.next {
  right: 0;
}


/* hide disabled button */

.flickity-prev-next-button:disabled {
  display: none;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>Document</title>
  <link rel="stylesheet" href="css/style.css" />
  <link rel="stylesheet" href="https://npmcdn.com/flickity@2.2.1/dist/flickity.css" />
</head>

<body>
  <!-- Flickity HTML init -->
  <section id="section-b">
    <div class="carousel js-flickity">
      <!-- images from unsplash.com -->
      <div class="carousel-cell">
        <img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/82/orange-tree.jpg" alt="..." />
      </div>
      <div class="carousel-cell">
        <img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/82/submerged.jpg" alt="..." />
      </div>
      <div class="carousel-cell">
        <img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/82/look-out.jpg" alt="..." />
      </div>
      <div class="carousel-cell">
        <img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/82/one-world-trade.jpg" alt="..." />
      </div>
      <div class="carousel-cell">
        <img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/82/drizzle.jpg" alt="..." />
      </div>
    </div>
    <script src="https://npmcdn.com/flickity@2.2.1/dist/flickity.pkgd.js"></script>
  </section>
</body>

</html>

Links to flickity.css and flickity.js

js: https://npmcdn.com/flickity@2/dist/flickity.pkgd.js

css: https://npmcdn.com/flickity@2/dist/flickity.css


Solution

  • Your section block is aligned properly, while div.carousel is not.

    I've solved the problem, using additional wrapper element - div.center-wrap. Also style was changed:

    .carousel {
      background: #0b1320;
      width: 100%;
    }
    div.center-wrap {
      display: flex;
      justify-content: center;
      align-items: center;
      min-height: 100vh;
    }
    

    Full code snippet:

    **style.css** ```
    /* external css: flickity.css */
    
    * {
      box-sizing: border-box;
    }
    
    body {
      font-family: sans-serif;
    }
    
    #section-b {
      padding: 0px;
      margin: 0px;
      background: #0b1320;
      text-align: center;
      color: white;
      min-height: 100vh;
    }
    
    .carousel {
      background: #0b1320;
      width: 100%;
    }
    
    div.center-wrap {
      display: flex;
      justify-content: center;
      align-items: center;
      min-height: 100vh;
    }
    
    .carousel-cell {
      width: 70%;
      height: 200px;
      /* flex-box, center image in cell */
      display: -webkit-box;
      display: -webkit-flex;
      display: flex;
      -webkit-box-pack: center;
      -webkit-justify-content: center;
      justify-content: center;
      -webkit-align-items: center;
      align-items: center;
    }
    
    .carousel-cell img {
      display: block;
      max-width: 100%;
      max-height: 100%;
      /* dim unselected */
      opacity: 0.7;
      -webkit-transform: scale(0.85);
      transform: scale(0.85);
      -webkit-filter: blur(5px);
      filter: blur(5px);
      -webkit-transition: opacity 0.3s, -webkit-transform 0.3s, transform 0.3s, -webkit-filter 0.3s, filter 0.3s;
      transition: opacity 0.3s, transform 0.3s, filter 0.3s;
    }
    
    
    /* brighten selected image */
    
    .carousel-cell.is-selected img {
      opacity: 1;
      -webkit-transform: scale(1);
      transform: scale(1);
      -webkit-filter: none;
      filter: none;
    }
    
    @media screen and (min-width: 768px) {
      .carousel-cell {
        height: 400px;
      }
    }
    
    @media screen and (min-width: 960px) {
      .carousel-cell {
        width: 60%;
      }
    }
    
    
    /* buttons, no circle */
    
    .flickity-prev-next-button {
      width: 60px;
      height: 60px;
      background: transparent;
      opacity: 0.6;
    }
    
    .flickity-prev-next-button:hover {
      background: transparent;
      opacity: 1;
    }
    
    
    /* arrow color */
    
    .flickity-prev-next-button .arrow {
      fill: white;
    }
    
    .flickity-prev-next-button.no-svg {
      color: white;
    }
    
    
    /* closer to edge */
    
    .flickity-prev-next-button.previous {
      left: 0;
    }
    
    .flickity-prev-next-button.next {
      right: 0;
    }
    
    
    /* hide disabled button */
    
    .flickity-prev-next-button:disabled {
      display: none;
    }
    <!DOCTYPE html>
    <html lang="en">
    
    <head>
      <meta charset="UTF-8" />
      <meta name="viewport" content="width=device-width, initial-scale=1.0" />
      <title>Document</title>
      <link rel="stylesheet" href="css/style.css" />
      <link rel="stylesheet" href="https://npmcdn.com/flickity@2.2.1/dist/flickity.css" />
    </head>
    
    <body>
      <!-- Flickity HTML init -->
      <section id="section-b">
        <div class="center-wrap">
    
          <div class="carousel js-flickity">
            <!-- images from unsplash.com -->
            <div class="carousel-cell">
              <img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/82/orange-tree.jpg" alt="..." />
            </div>
            <div class="carousel-cell">
              <img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/82/submerged.jpg" alt="..." />
            </div>
            <div class="carousel-cell">
              <img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/82/look-out.jpg" alt="..." />
            </div>
            <div class="carousel-cell">
              <img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/82/one-world-trade.jpg" alt="..." />
            </div>
            <div class="carousel-cell">
              <img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/82/drizzle.jpg" alt="..." />
            </div>
          </div>
          <script src="https://npmcdn.com/flickity@2.2.1/dist/flickity.pkgd.js"></script>
        </div>
      </section>
    </body>
    
    </html>