限制img的高度,并使其缩放

时间:2017-03-03 14:50:24

标签: html css

我正在尝试使用CSS来限制img的高度。如果它的高度大于给定的高度,它应缩小到所需的高度,并缩放宽度。他们没有父母。

我想出了这个:

img {
    max-height: 500px;
    width: auto;
}

它没有用。

2 个答案:

答案 0 :(得分:0)

如果你想做出回应,你应该使用%来代替px;此链接将帮助您。 https://www.w3schools.com/css/css_rwd_images.asp

答案 1 :(得分:0)

您可能需要考虑将此图像用作CSS背景图像,同时还要使用' background-size'属性。

IE:

.img-wrap {
    background: url(/path/to/image.jgp) no-repeat;
    background-size: cover; /* 'cover' is one of many other options */
}

CSS background-size Property