resize an image in proportion with CSS, is it possible?

前端 未结 2 1768
栀梦
栀梦 2021-02-03 15:44

Is there a way to resize images retaining their proportions with CSS?

The container has a fixed width and height

相关标签:
2条回答
  • 2021-02-03 16:17
    .theimage{
        width:100%;
        height:auto;
    }
    

    or

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

    Depending on how you wanna give the scale preference... :) :)

    Thats all.

    0 讨论(0)
  • 2021-02-03 16:29

    To save the Image ratio while scaling you can use object-fit CSS3 propperty.

    Useful article: Control image aspect ratios with CSS3

    img {
        width: 100%; /* or any custom size */
        height: 100%; 
        object-fit: contain;
    }
    
    0 讨论(0)
提交回复
热议问题