Hi guys i am using bootstrap
carousal and having some problem with image height and width. Even though i define it in the img
attribute it still displays as original resolution,following is the code i am using
<div class="col-md-6 col-sm-6">
<div id="myCarousel" class="carousel slide">
<div class="carousel-inner">
<div class="item active">
<img src="http://placehold.it/650x450/aaa&text=Item 3" height="300" width="300" />
</div>
<div class="item">
<img src="http://placehold.it/350x350/aaa&text=Item 3" height="300" width="300" />
</div>
<div class="item">
<img src="http://placehold.it/350x350/aaa&text=Item 3" height="300" width="300" />
</div>
</div>
<!-- Controls -->
<a class="left carousel-control" href="#myCarousel" data-slide="prev">
<span class="icon-prev"></span>
</a>
<a class="right carousel-control" href="#myCarousel" data-slide="next">
<span class="icon-next"></span>
</a>
</div>
</div>
Here is demo of that.
What should i do to fill the image in a certain height and width regardless of its original resolution.
Make your image take up 100% of the container size. using height:100% and width:100%.
Then restrict the size by setting values of the container to desired size.
I hope this helps.
You can do that with the following styles:
By setting
min-*
to the image attributes, we're making sure we have at least100%
of bothwidth
andheight
.Updated JSFiddle
That you have width in placeholder should not matter, put this in your css and it should work, If it works you can try remove !important.
.img-responsive in bootstrap will just set the width to 100%, So in the case that the image original proportions are less than carousel width it will not fill it out.
You can place height in css and add !important because bootstrap is overriding your height with auto , and object-fit:cover; will work like background size cover a fiddle
I faced the same problem. What worked for me is instead of using
img
, I useddiv
andbackground-image
for cssHTML:
CSS:
The carousel will grow to fit whatever the parent is by default, which in your case is
.col-md-6
and.col-sm-6
. If you want the carousel to be 300x300, style the carousel class/id or whatever to be the dimensions you want.Then after that, bootstrap applies
height: auto;
to images in the carousel, which will override theheight
attribute you've specified in the HTML. 2 ways you can address that - either make theheight
(and for consistency sake,width
, too) an inlinestyle
on theimg
tag if you want to keep it in the HTML, or apply the height to carousel images in your CSS file.