Changing image sizes proportionally using CSS?

后端 未结 9 2157
面向向阳花
面向向阳花 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:33

    this is a known problem with CSS resizing, unless all images have the same proportion, you have no way to do this via CSS.

    The best approach would be to have a container, and resize one of the dimensions (always the same) of the images. In my example I resized the width.

    If the container has a specified dimension (in my example the width), when telling the image to have the width at 100%, it will make it the full width of the container. The auto at the height will make the image have the height proportional to the new width.

    Ex:

    HTML:

    CSS:

    .container {
        width: 200px;
        height: 120px;
    }
    
    /* resize images */
    .container img {
        width: 100%;
        height: auto;
    }
    

提交回复
热议问题