Resize an image to fit in div

前端 未结 5 749
我寻月下人不归
我寻月下人不归 2021-01-17 15:57

How can I resize the image to fit in the size of the div piecemaker-container?

相关标签:
5条回答
  • 2021-01-17 16:15

    Why don't you set the width and height attributes of the img-tag?

    <img src="splash.jpg" alt="some_text" width="960" height="460"/>
    
    0 讨论(0)
  • 2021-01-17 16:23
    #piecemaker-container div, #piecemaker-container img {
    width: 100%;
    height: 100%;
    }
    

    This should do it.

    0 讨论(0)
  • 2021-01-17 16:25
    $("#piecemaker-container").each( function(){
                    var imageUrl = $(this).find('img').attr("src");
                    $(this).find('img').css("visibility","hidden");
                    $(this).css('background-image', 'url(' + imageUrl + ')').css("background-repeat","no-repeat").css("background-size","cover").css("background-position","50% 50%");
                });
    
    0 讨论(0)
  • 2021-01-17 16:37

    declare a css class for the img tag and just give it the same height and width as the div.Or just declare it as 100% as given

    img { width: 100%; height: 100%; }
    
    0 讨论(0)
  • 2021-01-17 16:41

    This may sound like an overdo but if you don't want the image to be squeezed or stretched, why not re-size it proportionally, according to the size you want, before displaying? Because it is pretty much easy to re-size with PHP anyway

    Hope it helps?

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