Bootstrap change carousel height

前端 未结 5 1594
北荒
北荒 2020-12-10 11:05

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.<

相关标签:
5条回答
  • 2020-12-10 11:35

    For Bootstrap 4

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

    <img src="..." style="height: 300px;" class="d-block w-100" alt="image">
    
    0 讨论(0)
  • 2020-12-10 11:37

    From Bootstrap 4

    .carousel-item{
        height: 200px;
    } 
    .carousel-item img{
        height: 200px;
    }
    
    0 讨论(0)
  • 2020-12-10 11:40

    Thank you! This post is Very Helpful. 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;
    }
    
    0 讨论(0)
  • 2020-12-10 11:53
    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
     }
    }
    
    0 讨论(0)
  • 2020-12-10 12:01

    The following should work

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

    JSFille for reference.

    0 讨论(0)
提交回复
热议问题