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="https://via.placeholder.com/120x120/69c/fff/" alt=""></div>
<div><img src="https://via.placeholder.com/200x200/c69/fff/" alt=""></div>
<div><img src="https://via.placeholder.com/160x160/9c6/fff/" alt=""></div>
<div><img src="https://via.placeholder.com/240x240/fc6/fff/" alt=""></div>
<div><img src="https://via.placeholder.com/160x160/9c6/fff/" alt=""></div>
<div><img src="https://via.placeholder.com/200x200/c69/fff/" alt=""></div>
<div><img src="https://via.placeholder.com/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>
With owl carousel version 2.1.1 and CSS3 Flexbox, just add the style:
.owl-carousel .owl-stage {
display: flex;
align-items: center;
}
See the snippet:
$( document ).ready( function() {
$( '.owl-carousel' ).owlCarousel({
autoplay: true,
margin: 2,
loop: true,
responsive: {
0: {
items:1
},
200: {
items:3
},
500: {
items:4
}
}
});
});
.owl-carousel .owl-stage {
display: flex;
align-items: center;
}
.owl-carousel .caption {
text-align: center;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/OwlCarousel2/2.2.1/assets/owl.carousel.min.css" rel="stylesheet"/>
<div class="owl-carousel">
<div class="item">
<img src="https://via.placeholder.com/350x200/?text=1">
<div class="caption">Caption 1</div>
</div>
<div class="item">
<img src="https://via.placeholder.com/255x200/?text=2">
<div class="caption">Caption 2</div>
</div>
<div class="item">
<img src="https://via.placeholder.com/627x200/?text=3">
<div class="caption">Caption 3</div>
</div>
<div class="item">
<img src="https://via.placeholder.com/627x300/?text=4">
<div class="caption">Caption 4</div>
</div>
<div class="item">
<img src="https://via.placeholder.com/627x400/?text=5">
<div class="caption">Caption 5</div>
</div>
<div class="item">
<img src="https://via.placeholder.com/255x200/?text=6">
<div class="caption">Caption 6</div>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/OwlCarousel2/2.2.1/owl.carousel.min.js"></script>
The Owl Carousel creates additional blocks within your layout. To add CSS properties correctly, you need to work with this HTML structure:
<div id="owl-demo" class="owl-carousel owl-theme">
<div class="owl-wrapper-outer">
<div class="owl-wrapper">
<div class="owl-item">
<div>
<img src="" alt="">
</div>
</div>
<div class="owl-item">
<div>
<img src="" alt="">
</div>
</div>
</div>
</div>
</div>
Moreover the carousel adds a style attribute to all blocks. For example, a block with the .owl-wrapper class receives the display property with the value of block. 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:
Owl Carousel 2.3.4: https://codepen.io/glebkema/pen/vYKMgzj
Owl Carousel 1.3.3: https://codepen.io/glebkema/pen/BzgZxX
$("#owl-example").owlCarousel({
navigation: true
});
#import url('//cdnjs.cloudflare.com/ajax/libs/owl-carousel/1.3.3/owl.carousel.min.css');
#import url('//cdnjs.cloudflare.com/ajax/libs/owl-carousel/1.3.3/owl.theme.min.css');
.owl-centered .owl-wrapper {
display: table !important;
}
.owl-centered .owl-item {
display: table-cell;
float: none;
vertical-align: middle;
}
.owl-centered .owl-item > div {
text-align: center;
}
<div id="owl-example" class="owl-carousel owl-centered">
<div><img src="https://via.placeholder.com/120x120/69c/fff/" alt=""></div>
<div><img src="https://via.placeholder.com/200x200/c69/fff/" alt=""></div>
<div><img src="https://via.placeholder.com/160x160/9c6/fff/" alt=""></div>
<div><img src="https://via.placeholder.com/240x240/fc6/fff/" alt=""></div>
<div><img src="https://via.placeholder.com/160x160/9c6/fff/" alt=""></div>
<div><img src="https://via.placeholder.com/200x200/c69/fff/" alt=""></div>
<div><img src="https://via.placeholder.com/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>
This works for me:
#media (max-width:500px) {
.owl-carousel .owl-item {
text-align:center;
}
.owl-carousel .item {
float: none;
display: inline-block;
}
}
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:
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 and table-cell to block and inline-block
.owl-stage{
display: block !important;
}
.owl-item{
display: inline-block;
float: none;
vertical-align: middle;
}
.item{
text-align: center;
}
The result (all images has different sizes):
Another solution is to use flexbox with margin.
.owl-stage-outer {
display: flex;
flex-wrap: nowrap;
flex-direction: column;
}
.owl-stage-outer > .owl-stage {
margin:0 auto;
}
I advise this solution:
.owl-carousel .owl-stage {
margin: 0 auto;
}
Related
I defined flexbox properties of container display: flex; flex-wrap: wrap; justify-content: center; but there are always 3 divs on the first row and 2 divs on the second. How to make it wrap divs when the browser resizes?
I tried almost everything (changed the width and height of parent container, changed width to min-width/max-width, set margin of parent container margin: 0 auto;).
<!-- HTML -->
<div class="parent">
<div class="headline">
<h2>Tea of the Month</h2>
<h4>What's Stepping at The Tea Cozy?</h4>
</div>
<div class="container">
<img src="..." class="image">
<p>Fall Berry Blitz Tea</p>
</div>
</div>
<div class="container">
<img src="..." class="image">
<p>Spiced Rum Tea</p>
</div>
</div>
<div class="container">
<img src="..." class="image">
<p>Seasonal Donuts</p>
</div>
</div>
<div class="container">
<img src="..." class="image">
<p>Myrtle Ave Tea</p>
</div>
</div>
<div class="container">
<img src="..." class="image">
<p>Bedford Bizarre Tea</p>
</div>
</div>
<!-- URL of images is correct -->
/* CSS */
.parent {
display: flex;
flex-wrap: wrap;
justify-content: center;
width: 1000px;
margin: 0 auto;
}
.headline {
display: block;
width: 100%;
}
.container {
margin: 0px 10px;
}
.image {
width: 300px;
height: 200px;
}
I expect the divs would wrap on next line. But there always are 3 divs on first and 2 divs on second line.
Your HTML contained some invalid closure tags, please validate your HTML you can check out: https://validator.w3.org/
Also removed the fixed width of 1000px, you want to have a fluid parent so it sizes acording to the device or browser width.
/* CSS */
.parent {
display: flex;
flex-wrap: wrap;
justify-content: center;
margin: 0 auto;
/* changed */
width: 100%;
}
.headline {
display: block;
width: 100%;
}
.container {
margin: 0px 10px;
}
.image {
width: 300px;
height: 200px;
}
<!-- HTML -->
<div class="parent">
<div class="headline">
<h2>Tea of the Month</h2>
<h4>What's Stepping at The Tea Cozy?</h4>
</div>
<div class="container">
<img src="https://placehold.it/300x300" class="image">
<p>Fall Berry Blitz Tea</p>
</div>
<!-- </div> REMOVED -->
<div class="container">
<img src="https://placehold.it/300x300" class="image">
<p>Spiced Rum Tea</p>
</div>
<!-- </div> REMOVED -->
<div class="container">
<img src="https://placehold.it/300x300" class="image">
<p>Seasonal Donuts</p>
</div>
<!-- </div> REMOVED -->
<div class="container">
<img src="https://placehold.it/300x300" class="image">
<p>Myrtle Ave Tea</p>
</div>
<!-- </div> REMOVED -->
<div class="container">
<img src="https://placehold.it/300x300" class="image">
<p>Bedford Bizarre Tea</p>
</div>
<div class="container">
<img src="https://placehold.it/300x300" class="image">
<p>Bedford Bizarre Tea</p>
</div>
<div class="container">
<img src="https://placehold.it/300x300" class="image">
<p>Bedford Bizarre Tea</p>
</div>
</div>
It happens because you set width:1000px on your .parent. This means that no matter the device size the parent will always be 1000px and there will always be 3 elements on the first row.
To solve this, set .parent {width: 100%; max-width: 1000px;}
cheers
I'm trying to make a side navbar made up of 7 icon images. I have all of the images wrapped in a div, but I want them to be responsive if the browser window shrinks (height). Here's what I have so far,
HTML:
<body>
<div id="sidenav">
<div id="nav1"><img src="img/menu.png" alt=""></div>
<div id="nav2"><img src="img/icon1.png" alt=""></div>
<div id="nav3"><img src="img/icon2.png" alt=""></div>
<div id="nav4"><img src="img/icon3.png" alt=""></div>
<div id="nav5"><img src="img/icon4.png" alt=""></div>
<div id="nav6"><img src="img/icon5.png" alt=""></div>
<div id="nav7"><img src="img/icon6.png" alt=""></div>
</div>
</body>
CSS:
body {
margin: 0;
padding: 0;
}
body, html {
height: 100%;
}
#nav1 img, #nav2 img, #nav3 img, #nav4 img, #nav5 img, #nav6 img, #nav7 img {
width: 100px;
}
#sidenav {
position: fixed;
}
So basically, whenever the browser is resized vertically, I want all of the images to resize with it, so that all 7 are always equally visible and square. Later, I want to make these images buttons, but firt I need to figure this out. Thanks in advance for the help! :-)
well, i actually have accomplished this on my page (super excited to truly answer my first question).
so what did was divided the screen by the number of icons and got a position for each icon. for mine i have 5 and I also need the top for other things. so after messing with the positioning a bit, i decided i wanted about 17% of vh for each and left room at the top. you have 7, so i would probably start around 14% vh (100/7 = ~14.2)for placing, giving each icon it's own css id, and a class to handle all the images. so for yours I would do about the same thing. to keep it square, you'll have to use vh for both the height and width. (vh stands for viewport height, so it will adjust as the view window does.)
#sidenav img{
height: 14vh;
width: 14vh;
}
#nav1{
position: fixed;
top:0vh;
}
#nav2{
position: fixed;
top:14vh;
}
#nav3{
position: fixed;
top:28vh;
}
#nav4{
position: fixed;
top:42vh;
}
#nav5{
position: fixed;
top:56vh;
}
#nav6{
position: fixed;
top:70vh;
}
#nav7{
position: fixed;
top:84vh;
}
hope that works for you. Hoping to get my first question answer.
As far as I know, this can not be done with css alone.
window.onresize = function() {
var height = document.body.clientHeight;
var nav = document.getElementById('sidenav');
var image = document.getElementsByTagName('img');
nav.setAttribute('style', 'height:' + height + 'px');
for (var i = 0; i < image.length; ++i) {
image[i].setAttribute('style', 'height:' + height / image.length + 'px');
}
};
<div id="sidenav">
<div id="nav1"><img src="https://images.pexels.com/photos/104827/cat-pet-animal-domestic-104827.jpeg?auto=compress&cs=tinysrgb&h=750&w=1260" alt=""></div>
<div id="nav2"><img src="https://images.pexels.com/photos/104827/cat-pet-animal-domestic-104827.jpeg?auto=compress&cs=tinysrgb&h=750&w=1260" alt=""></div>
<div id="nav3"><img src="https://images.pexels.com/photos/104827/cat-pet-animal-domestic-104827.jpeg?auto=compress&cs=tinysrgb&h=750&w=1260" alt=""></div>
<div id="nav4"><img src="https://images.pexels.com/photos/104827/cat-pet-animal-domestic-104827.jpeg?auto=compress&cs=tinysrgb&h=750&w=1260" alt=""></div>
<div id="nav5"><img src="https://images.pexels.com/photos/104827/cat-pet-animal-domestic-104827.jpeg?auto=compress&cs=tinysrgb&h=750&w=1260" alt=""></div>
<div id="nav6"><img src="https://images.pexels.com/photos/104827/cat-pet-animal-domestic-104827.jpeg?auto=compress&cs=tinysrgb&h=750&w=1260" alt=""></div>
<div id="nav7"><img src="https://images.pexels.com/photos/104827/cat-pet-animal-domestic-104827.jpeg?auto=compress&cs=tinysrgb&h=750&w=1260" alt=""></div>
</div>
I Suggest you to try bootstrap library, it's a really good responsive library with tons of great goodies.
Here's my code using bootstrap:
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.2.1/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
<div class="row">
<div class="col-md-12">
<img src="https://via.placeholder.com/1000" alt="" class="img-fluid">
</div>
<div class="col-md-12">
<img src="https://via.placeholder.com/1000" alt="" class="img-fluid">
</div>
<div class="col-md-12">
<img src="https://via.placeholder.com/1000" alt="" class="img-fluid">
</div>
<div class="col-md-12">
<img src="https://via.placeholder.com/1000" alt="" class="img-fluid">
</div>
<div class="col-md-12">
<img src="https://via.placeholder.com/1000" alt="" class="img-fluid">
</div>
</div>
</div>
Best Regards,
Khaled.
You can use media breakpoints to style your icons for special window height
#sidenav{
display: flex;
}
.nav-icon img{
max-height: 50px;
}
#media (max-height: 700px){
.nav-icon img{
max-height: 40px;
}
}
#media (max-height: 500px){
.nav-icon img{
max-height: 30px;
}
}
#media (max-height: 300px){
.nav-icon img{
max-height: 20px;
}
}
<body>
<div id="sidenav">
<div id="nav1" class="nav-icon"><img src="https://image.flaticon.com/icons/svg/1301/1301804.svg" alt=""></div>
<div id="nav2" class="nav-icon"><img src="https://image.flaticon.com/icons/svg/1301/1301805.svg" alt=""></div>
<div id="nav3" class="nav-icon"><img src="https://image.flaticon.com/icons/svg/1301/1301814.svg" alt=""></div>
<div id="nav4" class="nav-icon"><img src="https://image.flaticon.com/icons/svg/1301/1301808.svg" alt=""></div>
<div id="nav5" class="nav-icon"><img src="https://image.flaticon.com/icons/svg/1301/1301812.svg" alt=""></div>
<div id="nav6" class="nav-icon"><img src="https://image.flaticon.com/icons/svg/1301/1301809.svg" alt=""></div>
<div id="nav7" class="nav-icon"><img src="https://image.flaticon.com/icons/svg/1301/1301817.svg" alt=""></div>
</div>
</body>
another way - using vh metrics in css to make height responsive to screen size.
#sidenav{
display: flex;
}
.nav-icon img{
height: 15vh; /* 100vh = size of window */
}
<body>
<div id="sidenav">
<div id="nav1" class="nav-icon"><img src="https://image.flaticon.com/icons/svg/1301/1301804.svg" alt=""></div>
<div id="nav2" class="nav-icon"><img src="https://image.flaticon.com/icons/svg/1301/1301805.svg" alt=""></div>
<div id="nav3" class="nav-icon"><img src="https://image.flaticon.com/icons/svg/1301/1301814.svg" alt=""></div>
<div id="nav4" class="nav-icon"><img src="https://image.flaticon.com/icons/svg/1301/1301808.svg" alt=""></div>
<div id="nav5" class="nav-icon"><img src="https://image.flaticon.com/icons/svg/1301/1301812.svg" alt=""></div>
<div id="nav6" class="nav-icon"><img src="https://image.flaticon.com/icons/svg/1301/1301809.svg" alt=""></div>
<div id="nav7" class="nav-icon"><img src="https://image.flaticon.com/icons/svg/1301/1301817.svg" alt=""></div>
</div>
</body>
How to make my imgs not to overflow outside its container <div class="display"> and perfectly make a square/box?
It should've work for flexbox.
As you can see, this snippets resulting 3 images with 1 outside which not properly a box.
.display {
min-width: 500px;
max-width: 500px;
}
.scenery {
display: flex;
flex-direction: column;
}
.scenery_1 {
display: flex;
}
.scenery_2 {
display: flex;
}
<div class="display">
<div class="scenery">
<div class="scenery_1">
<img src="https://s-media-cache-ak0.pinimg.com/736x/02/e1/52/02e1528600a592817fbfa1c67158c13f.jpg" alt="display">
<img src="https://s-media-cache-ak0.pinimg.com/736x/02/e1/52/02e1528600a592817fbfa1c67158c13f.jpg" alt="display">
</div>
<div class="scenery_2">
<img src="https://s-media-cache-ak0.pinimg.com/736x/02/e1/52/02e1528600a592817fbfa1c67158c13f.jpg" alt="display">
<img src="https://s-media-cache-ak0.pinimg.com/736x/02/e1/52/02e1528600a592817fbfa1c67158c13f.jpg" alt="display">
<img src="https://s-media-cache-ak0.pinimg.com/736x/02/e1/52/02e1528600a592817fbfa1c67158c13f.jpg" alt="display">
</div>
</div>
</div>
I'm not sure why you're using display: flex, but this works:
.display {
min-width: 500px;
max-width: 100%;
display: block; /* Add this, remove everything else*/
}
<div class="display">
<div class="scenery">
<div class="scenery_1">
<img src="https://s-media-cache-ak0.pinimg.com/736x/02/e1/52/02e1528600a592817fbfa1c67158c13f.jpg" alt="display">
<img src="https://s-media-cache-ak0.pinimg.com/736x/02/e1/52/02e1528600a592817fbfa1c67158c13f.jpg" alt="display">
</div>
<div class="scenery_2">
<img src="https://s-media-cache-ak0.pinimg.com/736x/02/e1/52/02e1528600a592817fbfa1c67158c13f.jpg" alt="display">
<img src="https://s-media-cache-ak0.pinimg.com/736x/02/e1/52/02e1528600a592817fbfa1c67158c13f.jpg" alt="display">
<img src="https://s-media-cache-ak0.pinimg.com/736x/02/e1/52/02e1528600a592817fbfa1c67158c13f.jpg" alt="display">
</div>
</div>
</div>
How can I place 2 divs side by side, both which have images. I want the divs to remain side by side and the images auto size ,with screen size.When I reduce the size of the screen the images re-position themselves one below the other.
How will be the css for the below html?
<div class="container">
<div class="wrapper">
<div class="left">
<img src="img1.png" />
</div>
<div class="right">
<img src="img2.png" />
</div>
</div>
</div>
You can use css float to get contained elements to sit either side
.container {
width: 100%;
}
.left, .right {
width: 50%;
float: left;
}
<div class="container">
<div class="left"><img src='http://placehold.it/350x150'/></div>
<div class="right"><img src='http://placehold.it/350x150'/></div>
</div>
Edit: You can use Flex-Box
.container {
width: 100vw;
display: inline-flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: space-evenly;
align-items: center;
align-content: space-around;
}
.container > img {
align-self: auto;
margin: 4px;
}
<div class="container">
<img src='http://placehold.it/350x150'/>
<img src='http://placehold.it/350x150'/>
</div>
Try using bootstrap grid's. It can be something like this.
<div class="col-sm-12">
<div class="col-sm-6">
<img src="http://www.thebrandbite.com/wp-content/media/2015/07/apple-7.jpg" width="30%">
</div>
<div class="col-sm-6">
<img src="https://d3nevzfk7ii3be.cloudfront.net/igi/KRLMkuaBjm5mKDDP" width="30%">
</div>
</div>
Demo
I have this on my page. i wanted the container divs to align at the center. so i gave display:inline-block. but it didn't work. why is this happening? is there a way to display the containers as inline-block elements so that they appear exactly at the center?
<div id="container">
<div id="definition">
<p>Nothing</p>
</div>
<div id="image">
<img src="img1.jpg" />
</div>
</div>
<div id="container">
<div id="definition">
<p>Nothing</p>
</div>
<div id="image">
<img src="img1.jpg" />
</div>
</div>
<div id="container">
<div id="definition">
<p>Nothing</p>
</div>
<div id="image">
<img src="img1.jpg" />
</div>
</div>
css
#container {
vertical-align:top;
}
#image {
height:30%;
width:30%;
position: absolute;
overflow: hidden;
}
#definition {
width: 30%;
height: 30%;
position: absolute;
background-color: red;
}
Using display: table and display: table-cell this code will centre the divs on the page: Fiddle. Hopefully this is the effect you want.