Vertically center-align div in BxSlider Carousel
NickName:CMoreira Ask DateTime:2013-03-21T14:14:04

Vertically center-align div in BxSlider Carousel

I'm using bxslider script to build a carousel. I can do everything fine, I'm just having a problem trying to align to center the slides vertically, as they don't have the same height. I've tried numerous align techniques, but all seem to fail when the bxslider is in action. Here's a jsfiddle example.

Currently in the example I've set a very simple CSS rule that works when the carousel is not set:

.bxslider-inner {
   vertical-align: middle;
    display: inline-block;
} 

But, as you can see in the jsfiddle, when the carousel is active, the vertical alignment doesn't work anymore.

I'm starting to suspect I might have to change the core code of the script to achieve this.

Copyright Notice:Content Author:「CMoreira」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/15540673/vertically-center-align-div-in-bxslider-carousel

Answers
Matt Coughlin 2013-03-24T03:19:43

CSS tables\n\nOrdinarily, as long as support for IE7 or earlier wasn't needed, I'd suggest adding display: table-cell; (along the lines of the question @darthmaim linked to above):\n\n.bxslider-inner {\n display: table-cell;\n vertical-align: middle;\n}\n...\n<div class=\"bxslider-inner\"><div>Content goes here</div></div>\n\n\nHowever, display: table-cell; doesn't work on floated elements (tested in IE8/9/10, Firefox, Safari, Chrome, Opera), as shown in this JSFiddle Demo. In the question @darthmaim linked to, the parent of the element is the one that's floated; that works fine. Here the child element is floated; that doesn't work.\n\nWorkaround\n\nIf editing the HTML source code is an option, you can add a div wrapper inside each .bxslider-inner, so that the use of display: table-cell; can be moved to a non-floated element: Updated demo.\n\n.bxslider-inner > div {\n display: table-cell;\n vertical-align: middle;\n height: 90px;\n}\n...\n<div class=\"bxslider-inner\"><div><div>Content goes here</div></div></div>\n\n\nThis assumes that the carousel container is intended to have a fixed height of 90px. Supporting a variable-height container (which varies based on the height of the tallest carousel element), would require additional work.\n\nJavaScript\n\nIf editing the HTML source code isn't an option, there are two solutions using JavaScript or jQuery:\n\n\nDynamically inject div wrappers of the type described above, rather than adding them to the static HTML: Updated demo\nDynamically add the appropriate amount of top spacing to each element, based on the calculated height of each element.\n",


Vish 2013-03-24T12:43:29

We can vertcal align a div to the center by adding \"vertical-align:middle\" in the css and giving a fixesd height to the outer div. OR we can set by margin-top or can also use top attribute in css",


Rohan Kumar 2013-03-21T06:34:02

You can do it by adding top:30px\n\nHere vertical-align did not works because in the bxslider-inner div it adds additional style so it doesn't apply vertical-align here. So, in this case you can use top for that element.\n\nFiddle http://jsfiddle.net/sSqAx/3/",


Hassaan 2013-03-21T10:13:55

Set the values slideMargin:0 and slideWidth: 100\n\nIt works.",


user1467267 2013-03-24T13:37:34

This should fix your problem:\n\nJSfiddle\n\nAnd one with different heights: JSfiddle\n\nHTML\n\n<div class=\"bxslider\">\n <div class=\"bxslider-inner\"><div style=\"width:80px; height:80px; background:#CCC; padding:5px;\"></div></div>\n <div class=\"bxslider-inner\"><div style=\"width:80px; height:80px; background:#F5F5F5; padding:5px;\"></div></div>\n <div class=\"bxslider-inner\"><div style=\"width:80px; height:10px; background:#6699cc; padding:5px;\"></div></div>\n <div class=\"bxslider-inner\"><div style=\"width:80px; height:80px; background:#F5F5F5; padding:5px;\"></div></div>\n</div>\n\n\nCSS\n\nbody {\n background: orange;\n margin-top: 50px;\n}\n\n.bxslider-inner {\n vertical-align: middle;\n display: inline-block;\n height: 80px;\n}\n\n\nJavaScript\n\n'use strict';\n\njQuery(document).ready(function(){\n jQuery('.bxslider').bxSlider({\n mode: 'horizontal',\n speed: 500,\n slideMargin:20,\n infiniteLoop: true,\n pager: false,\n controls: true,\n slideWidth: 80,\n minSlides: 1,\n maxSlides: 5,\n moveSlides: 1\n }\n });\n\n // Initial\n $('.bxslider-inner').each(function(){\n var height_parent = $(this).css('height').replace('px', '') * 1;\n var height_child = $('div', $(this)).css('height').replace('px', '') * 1;\n var padding_top_child = $('div', $(this)).css('padding-top').replace('px', '') * 1;\n var padding_bottom_child = $('div', $(this)).css('padding-bottom').replace('px', '') * 1;\n var top_margin = (height_parent - (height_child + padding_top_child + padding_bottom_child)) / 2;\n\n $(this).html('<div style=\"height: ' + top_margin + 'px; width: 100%;\"></div>' + $(this).html());\n });\n});\n\n\nKeep in mind that the calculated middle is very dependent on other height variables like padding, margin and borders. If you add styles like that, keep in mind that you need to add those to the calculation. Another option is to use box-sizing: border-box;, so all of that collapses to the inside of the div.\n\nbxslider-inner MUST have a height, or you need to somehow fetch the calculated DOM height to avoid it. If you really want that, please leave a note and I'll look into it.\n\nGood luck!\n\nUPDATE\n\n\nJSfiddle updated, should not stuck in a loop now.\nTested with a too high height, code doesn't seem to break, but might overflow to hidden\nLook at http://bxslider.com/options (> Callbacks > onSlideAfter). You can do modifications after each cycle if needed too. Here you can modify your code again if needed for complexion\n",


Adrian 2022-12-13T10:48:03

Updated answer:\nBecause the above answer by @doitlikejustin uses display: inline-block I have found that this adds on an extra 4px to each item so if you had 4 items in your carousel, it is pushed 16px off the right hand side because the .bx-wrapper width is calculated.\nTo prevent this, use flexbox on the parent div instead:\n.bxslider {\n display: flex;\n align-items: center;\n}\n.bxslider > div {\n float: none!important;\n}\n\nThis will not add the extra 4px spacing per item and will fit nicely.",


doitlikejustin 2013-03-24T04:10:51

Update your CSS like this. The key is not floating the element because you are always making it inline-block\n\n.bxslider-inner {\n vertical-align: middle;\n display: inline-block;\n float: none !important;\n}\n\n\nOne more thing... To make it match your example, change slideMargin:20 to slideMargin:10 after you have done the float: none !important;\n\nFiddle: http://jsfiddle.net/sSqAx/9/",


More about “Vertically center-align div in BxSlider Carousel” related questions

Vertically center-align div in BxSlider Carousel

I'm using bxslider script to build a carousel. I can do everything fine, I'm just having a problem trying to align to center the slides vertically, as they don't have the same height. I've tried nu...

Show Detail

Move carousel to the clicked div, bxslider

How it's possible to move the carousel to the clicked div, if I'm using bxslider? Below is my carousel. When you click on controls (left/right arrows) it moves only one slide/div, so the active d...

Show Detail

bxslider carousel code

i am trying to implement the slider from this site http://bxslider.com/ i used the exact procedure what they said but still my code is not working can you guys tell me how to fix it i am providi...

Show Detail

BxSlider Carousel - Changing one by one

I am using BxSlider Carousel in my project. Here is the link from bxslider; http://bxslider.com/examples/carousel-demystified I am using the vertical one. What I want to adjust and achieve there ...

Show Detail

bxslider swiping only from carousel edge

We have other instances of bxslider on our site with images instead of videos that behave as expected but this is our only instance of bxslider with embedded youtube videos. The problem I am having...

Show Detail

Bxslider carousel with bootstrap align to the left

I have a bootstrap page that I have a bxslider Carousel (http://bxslider.com/) on and I would like it to align to the left the code is simply: &lt;div class="col-xs-12"&gt; &lt;/div&gt; Now I have

Show Detail

Image carousel slider not working bxSlider

This is how my carousel shows up as in my code ive included &lt;!-- bxSlider CSS file --&gt; &lt;link rel="stylesheet" href="&lt;c:url value="/resources/css/jquery.bxslider.css" /&

Show Detail

bxSlider carousel showing only 1 slide

I'm tyring to reproduce a carousel kinda like that one : http://bxslider.com/examples/carousel-dynamic-number-slides The only difference being that mine would include html content rather than simple

Show Detail

How to false infiniteLoop in bxslider for carousel?

How can i stop my carousel from infinite loop in bxslider ? $('.bxslider').bxSlider({ minSlides: 3, maxSlides: 4, slideWidth: 170, slideMargin: 10, infiniteLoop: false, hideControlOnEn...

Show Detail

bxslider: showing half an image outside of the div

tl;dr version: want to position half an image outside of the bxslider div however overflow:hidden causes every slide of the carousel to show up. Code I'm using: http://www.dtcontentguy.com/bxslide...

Show Detail