Stretch Image to Fit 100% of Div Height and Width

前端 未结 4 975
余生分开走
余生分开走 2020-12-10 01:11

I have a div with the following CSS

#mydiv{
    top: 50px;
    left: 50px;
    width: 200px;
    height: 200px;
}

and my HTML looks like th

相关标签:
4条回答
  • 2020-12-10 01:42

    will the height attribute stretch the image beyond its native resolution? If I have a image with a height of say 420 pixels, I can't get css to stretch the image beyond the native resolution to fill the height of the viewport.

    I am getting pretty close results with:

     .rightdiv img {
            max-width: 25vw;
            min-height: 100vh;
        }
    

    the 100vh is getting pretty close, with just a few pixels left over at the bottom for some reason.

    0 讨论(0)
  • 2020-12-10 01:53

    You're mixing notations. It should be:

    <img src="folder/file.jpg" width="200" height="200">
    

    (note, no px). Or:

    <img src="folder/file.jpg" style="width: 200px; height: 200px;">
    

    (using the style attribute) The style attribute could be replaced with the following CSS:

    #mydiv img {
        width: 200px;
        height: 200px;
    }
    

    or

    #mydiv img {
        width: 100%;
        height: 100%;
    }
    
    0 讨论(0)
  • 2020-12-10 01:54

    Instead of setting absolute widths and heights, you can use percentages:

    #mydiv img {
        height: 100%;
        width: 100%;
    }
    
    0 讨论(0)
  • 2020-12-10 01:57

    Or you can put in the CSS,

    <style>
    div#img {
      background-image: url(“file.png");
      color:yellow (this part doesn't matter;
      height:100%;
      width:100%;
    }
    </style>
    
    0 讨论(0)
提交回复
热议问题