How to make bootstrap carousel image responsive?
I want to keep the same ratio of the images. The problem is it streches when the browser is wide. and squezes when it's reduced.
I have checked all SO question here but most of them did not help me.
Here is the markup:
<!-- Carousel
================================================== -->
<div id="myCarousel" class="carousel slide" data-ride="carousel">
<!-- Indicators -->
<ol class="carousel-indicators">
<li data-target="#myCarousel" data-slide-to="0" class="active"></li>
<li data-target="#myCarousel" data-slide-to="1"></li>
<li data-target="#myCarousel" data-slide-to="2"></li>
</ol>
<div class="carousel-inner">
<div class="item active">
<img src="images/female/IMG_5053-2.jpg"
data-src="images/female/IMG_5053-2.jpg" alt="First slide">
and here is the CSS
.carousel .item>img {
position: absolute;
top: 0;
left: 0;
min-width: 100%;
height: 100%;
}
I tried to make a jsfiddle but I couldnt here is the link to the page http://maanastore.com/home.php
Solution 1:
Remove the following CSS rules from respective files:
In home.php file
.carousel .item>img {
position: absolute;
top: 0;
left: 0;
max-width: 100%;
height: 100%;
}
in carousel.css
.carousel-inner > .item > img {
position: absolute;
top: 0;
left: 0;
min-width: 100%;
height: 500px;
}
Also, add margin-top: 51px;
to .carousel
class in carousel.css file and remove height:500px
from the same class, because you have fixed navbar.
Solution 2:
I found it best to remove the following.
.carousel-inner > .item > img {
position: absolute;
top: 0;
left: 0;
min-width: 100%;
height: 500px;
}
and in .carousel remove:
height: 500px;
and in .carousel .item remove
height: 500px;
for me this made the image work the way I wanted it to but be sure to insert an image in your html first or else I think it will just collapse into nothingness if you are working locally.
edit: Another user suggested I add this
Please Note that if the image shows grey bars on either side or even on one side, I'd recommend keeping
.carousel-inner > .item > img {
min-width: 100%;
}
Solution 3:
what i ended up doing was adding a div inside then have the image background of the div as responsive
<div class="carousel-inner" role="listbox">
<div class="carousel-item active">
<div class="img1">
</div>
<!--<img class=" imgCstm " src="images/home/bng_00.jpg" alt="First slide">-->
</div>
</div>
<style>
.carousel-inner {
height: 100vh;
<! -- custom hight -->
}
.carousel-item {
height: 100%;
}
.img1 {
background-image: url('../images/home/bng_00.jpg');
height: 100%;
width: 100%;
background-repeat: no-repeat;
background-attachment: fixed !important;
background-size: cover !important;
background-position: center !important;
}
</style>