How to scale an image with css to fill a div keeping aspect ratio?

后端 未结 6 1895
情深已故
情深已故 2021-02-13 15:10

I\'d like to fill a div with an img, keeping aspect ratio and stretching either width or height as much as required to fit in.

6条回答
  •  别跟我提以往
    2021-02-13 15:33

    .thumb {
        max-width:100%;
        height:auto;
    }
    

    Or ( to allow scale up and down, which will look pixelated if you scale up, where the above will only scale to the max size of the image )

    .thumb {
        width:100%;
        height:auto;
    }
    

    Is what you are looking for.

    More info on responsive images:

    1. http://demosthenes.info/blog/586/CSS-Fluid-Image-Techniques-for-Responsive-Site-Design

    2. http://www.w3schools.com/css/css_rwd_images.asp

提交回复
热议问题