Stretch and fill image in Bootstrap carousel

前端 未结 14 1315
情歌与酒
情歌与酒 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 13:51

    You can use background-size: cover, while still having hidden element for SEO and semantic purposes. Single img url is used both times, so there's no additional loading, and background-size is well supported (unlike object-fit which lacks support in IE, Edge and Android < 4.4.4).

    Fiddle Demo

    HTML change:

    CSS change:

    .carousel {
      /* any dimensions are fine, it can be responsive with max-width */
      width: 300px;
      height: 350px;
    }
    
    .carousel-inner {
      /* make sure your .items will get correct height */
      height: 100%;
    }
    
    .item {
      background-size: cover;
      background-position: 50% 50%;
      width: 100%;
      height: 100%;
    }
    
    .item img {
      visibility: hidden;
    }
    

提交回复
热议问题