Stretch and fill image in Bootstrap carousel

前端 未结 14 1309
情歌与酒
情歌与酒 2021-01-11 13:11

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 a

相关标签:
14条回答
  • 2021-01-11 14:09

    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

    .carousel{
      width:300px;
    }
    .item img{
      object-fit:cover;
      height:300px !important;
      width:350px;
    }
    
    0 讨论(0)
  • 2021-01-11 14:15

    I faced the same problem. What worked for me is instead of using img, I used div and background-image for css

    HTML:

    <div class="carousel-inner">
      <div class="item active">
        <div class="carousel-img"></div>
      </div>
    </div>
    

    CSS:

    .carousel-inner, .item {
        height: 100%;
    }
    
    .carousel-img {
        background-image: url('http://placehold.it/400x400');
        width: 100%;
        height:100%;
        background-position:center;
        background-size:cover;
    }
    
    0 讨论(0)
提交回复
热议问题