CSS: 100% width or height while keeping aspect ratio?

前端 未结 14 2630
粉色の甜心
粉色の甜心 2020-11-28 21:05

Currently, with STYLE, I can use width: 100% and auto on the height (or vice versa), but I still can\'t constrain the image into a specific positio

14条回答
  •  误落风尘
    2020-11-28 21:46

    By setting the CSS max-width property to 100%, an image will fill the width of it's parenting element, but won’t render larger than it's actual size, thus preserving resolution.

    Setting the height property to auto maintains the aspect ratio of the image, using this technique allows static height to be overridden and enables the image to flex proportionally in all directions.

    img {
        max-width: 100%;
        height: auto;      
    }
    

提交回复
热议问题