How to keep image aspect ratio inside a %width div?

蓝咒 提交于 2020-01-04 02:12:24

问题


I'm sorry to ask this again, but my search has not revealed anything that I have been able to implement. I have an image inside a div with the following styles:

<div class="thumb grid_6">
    <img src="img/test2.jpg" alt="flavin" />
</div>

.grid_6 { width: 50%; }

.thumb img {
display: block;
max-width:100%;
max-height: 100px;
width:100%;

I'd like the image height to be locked at 100px, with the width remaining at 50%. Ideally the image would keep its aspect ratio, and just crop to the required size to fit the thumbnail. I'd rather not get into js, unless there is a much easier way to do it using js. I apologize for my lack of experience in coding.

Any help would be much appreciated.


回答1:


This should be what you are looking for:

FIDDLE

CSS:

.grid_6 {
    width: 50%;
    height:100px;
    overflow:hidden;
}
.thumb img {
    display: block;
    width:100%;
    height:auto;
}



回答2:


Is this what you want?

.grid_6 {overflow: hidden; }
.thumb img {
    height: 100px;
    width: auto;
}


来源:https://stackoverflow.com/questions/22177264/how-to-keep-image-aspect-ratio-inside-a-width-div

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!