Fill a div with an image respecting aspect ratio

后端 未结 4 1828
悲哀的现实
悲哀的现实 2020-12-04 03:51

Is it possible to fill a div with an image such that at least one image dimension is 100% and the other dimension is either wider or equal size as the div, whilst r

4条回答
  •  情书的邮戳
    2020-12-04 04:04

    This is not the exact solution, but it could be an implementation that you could try to make your code work. Here is an example:

    As you can't predict the aspect ratio of the image here is what I would do:

    HTML: Set the div tag to 'fill':

    CSS: This will place your image as the background, and stretch it to fit the div size without distortion.

    .fill {
        overflow: hidden;
        background-size: cover;
        background-position: center;
        background-image:"path/to/image.jpg";
    }
    

提交回复
热议问题