开发者

Force image to a specific size if it is larger

开发者 https://www.devze.com 2023-02-13 10:25 出处:网络
I have an image I am displaying in a image tag: These are thumbnails, but some are a little to wide so for those images I want to force the display to shrink them (even though the larger image is l

I have an image I am displaying in a image tag:

These are thumbnails, but some are a little to wide so for those images I want to force the display to shrink them (even though the larger image is loaded).

How can i do this for images that are larger, while not expanding images t开发者_StackOverflow社区hat are smaller.


You can use CSS properties max-width and max-height on images.

img {
    max-width: 300px;
    max-height: 200px;
}

works great with all popular browsers, including IE8.


You could add a css class to only those images that exceed a given height or width, then set the height and width in your stylesheet.


You could just specify the height and width attribute of the <img> tag to a value that generally suits most of the images that you are likely to encounter.

0

精彩评论

暂无评论...
验证码 换一张
取 消