开发者

force css to display image with ratio-aspect

开发者 https://www.devze.com 2023-01-04 05:30 出处:网络
imax is a div element. #imax { width:222px; height:222px; } #imax img { width:222px; height:auto; } 开发者_JS百科if either way I tried auto on width or height, the images with different orientation

imax is a div element.

#imax {
width:222px;
height:222px;
}
#imax img {
width:222px;
height:auto;
}
开发者_JS百科

if either way I tried auto on width or height, the images with different orientation will result in distort in either scale. How could I fix to display with ratio-aspect?


Leave out the height:

#imax img {
  width:222px;
}

And the browser will take care of the aspect ratio calculation.


The image is distorted since you put restrictions on it's parent. If you specify just one of the size attributes, the other should scale according to proportions - unless it's limited/affected by its container. Might work differently if you used max-height/width on the container


Cannot be done with CSS only.

You could use Javascript to do this: Get either height or width of the full size image, then calculate the correct smaller size.

0

精彩评论

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