Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting a max-height on a Bootstrap Carousel

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/

like image 507
James Ives Avatar asked Feb 11 '16 23:02

James Ives


2 Answers

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.

JSFIDDLE

like image 145
max Avatar answered Sep 19 '22 01:09

max


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();
    }); 
});
like image 22
alvseek Avatar answered Sep 19 '22 01:09

alvseek