34

I have a jsfiddle here - http://jsfiddle.net/gh4Lur4b/8/

It's a full width bootstrap carousel.

I'd like to change the height and still keep it full width.

Is the height determined by the image height?

How can I change the height of the carousel and keep it 100% width?

.carousel-inner{
    // height: 300px;
}

.item, img{
    height: 100% !important;
    width:  100% !important;
    border: 1px solid red;
}
kelsny
  • 23,009
  • 3
  • 19
  • 48
ttmt
  • 5,822
  • 27
  • 106
  • 158

7 Answers7

38

The following should work

.carousel .item {
  height: 300px;
}

.item img {
    position: absolute;
    top: 0;
    left: 0;
    min-height: 300px;
}

JSFiddle for reference.

kelsny
  • 23,009
  • 3
  • 19
  • 48
Bart Jedrocha
  • 11,450
  • 5
  • 43
  • 53
  • This doesn't change the image size. The carousel changes size but not the image. I think I was hoping for something like `background-size:cover;` so the image stretches http://jsfiddle.net/gh4Lur4b/17/ – ttmt Feb 18 '15 at 19:44
  • Oh I see what you mean. Sorry that wasn't clear from your original question. Please see my updated answer. – Bart Jedrocha Feb 18 '15 at 20:50
  • But now the image is squashed when the window is made smaller. In your jsfiddle when the window is made bigger the image is not stretched but expands. I need a similar effect when the window is made smaller. – ttmt Feb 18 '15 at 22:25
  • Do you know these images before-hand? In other words, can you access them within your CSS? – Bart Jedrocha Feb 19 '15 at 00:22
  • Also, this method seems to not be working if you use 50% as the height. Anyone know how to do that? Thanks. – HumbleWebDev Nov 03 '16 at 01:03
17

You may also want to add

object-fit: cover;

To preserve the aspect ration for different window sizes

.carousel .item {
  height: 500px;
}

.item img {
    position: absolute;
    object-fit:cover;
    top: 0;
    left: 0;
    min-height: 500px;
}

For Bootstrap 4 and above replace .item with .carousel-item

.carousel .carousel-item {
  height: 500px;
}

.carousel-item img {
    position: absolute;
    object-fit: cover;
    top: 0;
    left: 0;
    min-height: 500px;
}
MAXE
  • 4,978
  • 2
  • 45
  • 61
Brandon Walton
  • 179
  • 1
  • 2
  • 2
    Speaking only for Bootstrap 4 (I haven't looked at the older ones), surely you mean `.carousel .carousel-item { height: .....` , not `.carousel .item ` ? – Reversed Engineer Dec 28 '19 at 12:00
  • excellent answer for bootstrap 4 and above replace `.item` with `.carousel-item` as the comment above says. – Bobby Axe Aug 09 '20 at 20:30
9

From Bootstrap 4

.carousel-item{
    height: 200px;
} 
.carousel-item img{
    height: 200px;
}
1
like Answers above, if you do bootstrap 4 just add few line of css to .carousel , carousel-inner ,carousel-item and img as follows

.carousel .carousel-inner{
height:500px
}
.carousel-inner .carousel-item img{
min-height:200px;
//prevent it from stretch in screen size < than 768px
object-fit:cover
}

@media(max-width:768px){
.carousel .carousel-inner{
//prevent it from adding a white space between carousel and container elements
height:auto
 }
}
1

if someone comes here looking exactly for what the headline points to, this will help (remember it's OK to use jQuery in Bootstrap v4):

$.fn.normalizeHeight = function () {
    $(this).css("height", Math.max.apply(null, $(this).children().map(function () {
        return $(this).height();
    })) + "px");

    return this;
};

It would simply be called with:

$("#slider .carousel-inner").normalizeHeight();

$(window).on('resize', function () {
  $("#slider .carousel-inner").normalizeHeight();
});

It is ES5 compliant + would work in other needed equal child height.

Litzer
  • 119
  • 2
  • 3
-2

For Bootstrap 4

In the same line as image add height: 300px;

<img src="..." style="height: 300px;" class="d-block w-100" alt="image">
-2

Try following it should work:

.carousel .carousel-item {
    max-height:550px;
}

.carousel-item img {
    object-fit:cover;
    max-height:550px;
}
mousetail
  • 7,009
  • 4
  • 25
  • 45