My Owl Carousel contains pictures of different width and height. How do I align them in the middle - both horizontally and vertically?
$("#owl-example").owlCarousel({
navigation: true
});
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/owl-carousel/1.3.3/owl.carousel.min.css">
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/owl-carousel/1.3.3/owl.theme.min.css">
<div id="owl-example" class="owl-carousel">
<div><img src="//placehold.it/120x120/69c/fff/" alt=""></div>
<div><img src="//placehold.it/200x200/c69/fff/" alt=""></div>
<div><img src="//placehold.it/160x160/9c6/fff/" alt=""></div>
<div><img src="//placehold.it/240x240/fc6/fff/" alt=""></div>
<div><img src="//placehold.it/160x160/9c6/fff/" alt=""></div>
<div><img src="//placehold.it/200x200/c69/fff/" alt=""></div>
<div><img src="//placehold.it/120x120/69c/fff/" alt=""></div>
</div>
<script src="//code.jquery.com/jquery-1.12.4.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/owl-carousel/1.3.3/owl.carousel.min.js"></script>
The solution with display table is OK, but for me the divs on the right and left side slides out from the container. My code is close the same, I changed the
table
andtable-cell
toblock
andinline-block
The result (all images has different sizes):
The Owl Carousel creates additional blocks within your layout. To add CSS properties correctly, you need to work with this HTML structure:
Moreover the carousel adds a
style
attribute to all blocks. For example, a block with the.owl-wrapper
class receives thedisplay
property with the value ofblock
. So you have to use an!important
declaration in your CSS.To obtain a horizontal alignment, you can simply add
text-align: center;
property to each.owl-item > div
.To align vertically, you can turn the carousel items in table cells. But do not forget to cancel the
float
property for them.Let us arrange all of the changes as a new
.owl-centered
class. Please check the result:https://codepen.io/glebkema/pen/BzgZxX
Another solution is to use flexbox with margin.
With owl carousel version 2.1.1 and CSS3 Flexbox, just add the style:
See the snippet:
This works for me:
You might adjust it to your HTML though, but the idea is that you text-align a parent, and float:none and display:inline-block the child that holds the content, on mobile: