Swiper: vertically center slides

Created on 24 Nov 2015  路  8Comments  路  Source: nolimits4web/swiper

Unless I am doing something incorrectly, it doesn't appear that slides are vertically centered in the container when slides vary in height.

Most helpful comment

This is for vertical direction. Otherwise you may just add this CSS:

.swiper-wrapper {
  align-items: center; /* add this will all relevant prefixes */
}

All 8 comments

Something simple like the following onSlideChangeStart seems to do the trick:

            function verticallyCenterSlide(api) {
                var activeSlide = api.slides[api.activeIndex];
                var top = ($(api.container).height() - $(activeSlide).height()) / 2;
                $(activeSlide).css("top", ""+top+"px");
            }

But it would be nicer if it was calculated in the transform as a real solution

Do you mean centeredSlides: true parameter?

centeredSlides: true seems to do horizontal centering but not vertical centering.

This is for vertical direction. Otherwise you may just add this CSS:

.swiper-wrapper {
  align-items: center; /* add this will all relevant prefixes */
}

Great! thanks. Seems I am behind the times with my css.

align-items on the .swiper-wrapper doesn't work for me with the current version. But on .swiper-slide it did the trick ;)

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings