Changing image sizes proportionally using CSS?

后端 未结 9 2147
面向向阳花
面向向阳花 2020-12-12 10:19

I have been trying for a couple of days now to configure my thumbnail gallery so all the images appear the same height and width. However when I change the Css code to,

9条回答
  •  庸人自扰
    2020-12-12 10:21

    If you don't want to stretch the image, fit it into div container without overflow and center it by adjusting it's margin if needed.

    1. The image will not get cropped
    2. The aspect ratio will also remain the same.

    HTML:

    something
    something
    something

    CSS:

    div#container {
        height: 200px;
        width: 200px;
        border: 1px solid black;
        margin: 4px;
    }
    img {
        max-width: 100%;
        max-height: 100%;
        display: block;
        margin: 0 auto;
    }
    

提交回复
热议问题