简体   繁体   English

根据父级div宽度/高度调整图像的100%高度或宽度

[英]Adjust 100% height or width of image as per parent div width / height

I have one dynamic/responsive div with image. 我有一个动态/响应的div图像。 And want to make image height or width 100% as per DIV height/width. 并希望按照DIV高度/宽度将图像高度或宽度设置为100%。 I dont want to stretch the image. 我不想拉伸图像。 Either width or height should be 100% not both. 宽度或高度都不应为100%。

Here is the example that I am trying http://jsfiddle.net/CLrUS/ 这是我尝试http://jsfiddle.net/CLrUS/的示例

Give CSS for the Image like this 像这样给CSS图片

.css{ max-width:100%;

    max-height:100%}

Here the image takes the width of the parent div. 此处的图片采用父div的宽度。 Here image wont stretch, image gets the propotional height and weight 在这里图像不会拉伸,图像会得到比例的高度和重量

Example : http://jsfiddle.net/CLrUS/1/ 范例: http : //jsfiddle.net/CLrUS/1/

for increase just height: 用于增加高度:

.class img{
    height: 100%;
    width: auto;
}

and this one for just width: 而这只是宽度:

.class img{
    height: auto;
    width: 100%;
}

Try this: 尝试这个:

<img src="#your image#" onload="imgFitParent($(this));" style="width:inherit; height:inherit;">

<script type="text/javascript">
function imgFitParent(imgElemt)
{
    if(imgElemt.parent().width() > imgElemt.parent().height()){ 
        imgElemt.height("100%"); 
        imgElemt.width('auto'); 
    }else{ 
        imgElemt.width("100%"); 
        imgElemt.height('auto'); 
    }
}

</script>

Like this: JSFIDDLE 像这样: JSFIDDLE

use the min-width and max-width 使用最小宽度和最大宽度

img{
    min-width: 100%;
    max-width: 100%;
}

Try resizing the panel 尝试调整面板大小

http://jsfiddle.net/e3yPT/ http://jsfiddle.net/e3yPT/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM