I'm attempting to enforce a max-height on a Bootstrap carousel. The idea is that any images which are too tall will appear with black bars to the left and right so it maintains its aspect ration without stretching the page down too far. The content being pumped into the carousel will often vary in height which is why I want to do this.
Currently in my Carousel the images which are around 1024x768 look fine, but if a tall piece of content is placed into it it gets cut off at the bottom, takes up the spot for the image caption underneath and still stretches the bottom out.
.carousel {
width: auto;
max-height: 768px;
overflow: hidden;
}
@media screen and (max-width: 768px) {
.carousel {
/* Pushes out the margins on mobile devices so it lines up with the article body. */
margin-left: -20px !important;
margin-right: -20px !important;
width: inherit !important;
}
}
.carousel:hover {
visibility: visible;
color: white;
bottom: 17%;
}
.carousel .carousel > .carousel-control {
visibility: hidden;
}
.carousel .carousel-control {
visibility: visible;
color: white;
}
.carousel .carousel-inner {
background-color: #000000;
}
.carousel .carousel-caption {
position: relative;
left: auto;
right: auto;
padding-bottom: 0;
}
.carousel .item img {
margin-left: auto;
margin-right: auto;
min-width: 100%;
}
What would be the best way to enforce a height restriction on the carousel while still having the content maintain its aspect ratio, regardless of how tall it is?
JSFiddle: https://jsfiddle.net/1exapzk8/3/
Try this CSS and delete max-height from .carousel
:
.carousel .item img {
max-height: 768px;
min-width: auto;
}
I added code at the bottom to override Bootstrap styles.
if you want to maintain responsive on the width, you can use javascript/jquery.
I used jquery to maintain it's height to 50% of the width
$(function(){
var SetCarouselHeight = function() {
$("#myCarousel .item > img").height(function(){
return $("#myCarousel").width() * 0.5;
});
}
SetCarouselHeight();
$(window).resize(function(){
SetCarouselHeight();
});
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With