Stretch and fill image in Bootstrap carousel

前端 未结 14 1306
情歌与酒
情歌与酒 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:00

    if it's okay for you to use simple javaScript, here is the updated fiddle https://jsfiddle.net/a1x1dnaa/2/

    what i have done is, used the image as background and removed the tag.

    jQuery(function(){
      var items = $('.item');
      items.each(function() {
        var item = $(this);
        img = item.find("img");
        img.each(function() {
            var thisImg = $(this);
            url = thisImg.attr('src');
            thisImg.parent().attr('style', 'background-image:url('+url+')');
            thisImg.remove();
        });
      });
    });
    

    then you can use some styles to make the image fill the div

    .item {
      -webkit-background-size: cover;
      background-size: cover;
      background-repeat: no-repeat;
      background-position: 50% 50%;
      height: 200px; //you can use the height as you need 
    }
    

    this should work for you.

    if you don't want to use javaScript, you can simply use the image as inline-style

    and use the same CSS as above.

提交回复
热议问题