2012-11-02 29 views
3

我如何制作100%的div图像?图片100%的div

我有一个300x300的图像,我想使它成为一个更大的div的全尺寸。 (这个div的大小可以改变,所以我不得不指定100%的地方)

在CSS或Javascript中有解决方案吗?

+2

背景尺寸:100%; – Vucko

+0

或背景:url('link')center top no-repeat; 这将适合div。 – Vucko

+0

我不想背景它。但下面的答案是解决方案。谢谢你:) – Emilie

回答

2

试试这个:

<img src="test.jpg" alt="test" id="bg" /> 

img#bg { 
    position:fixed; 
    top:0; 
    left:0; 
    width:100%; 
    height:100%; 
} 

CSS3也支持这一点:

#testbg{ 
background: url(bgimage.jpg) no-repeat; 
background-size: 100%; 
} 
+0

哇...我确定我已经尝试过了。那么,它是完美的工作。谢谢 ! – Emilie

+0

所有最好的Emile! – nirmalgyanwali

1

<div style="width:450px;height:450px;">
        <img src="photo.png" style="width:100%;height:100%;"/>
</div>

1

的CSS下面将规模你的形象,并填写您的div的宽度的100%

#imgId { 
    width: 100%; 
    height: auto; 
} 

但是,如果你真的想通过拉伸图像使用

#imgId { 
    width: 100%; 
    height: 100%; 
} 
填充整个DIV

另一个有用的提示是,当你的宽度被指定为百分比,而你的图像是正方形时,就像你的那样。上述

HTML

<div class="container"> 
    <img src="sample.jpg"> 
    <div style="clear:both;"></div> 
</div> 

CSS

​.container { 
    position: relative; 
    width: 50%; 
    height: 0; 
    // % padding is calculated as % of width rather than height 
    // so height will equal 50% 
    padding-bottom: 50%; 
} 

img { 
    position: relative; 
    width: 100%; 
    // image is square so as long as width is 100% then height will be the same. 
    height: auto; 
} 
html, body { 
    height: 100%; 
    width: 100%; 
} 

意味着图像会一直调整到正确配合母公司股利。

小提琴here

2

刚分配的CSS样式width: 100%在图像标签有它覆盖其父容器的整个空间。

实施例或jsFiddle

<div style="width: 500px;"> 
    <img src="yourPic.png" style="width: 100%" /> 
</div> 
1

尝试这种情况:http://jsfiddle.net/XUZV5/

<div style="height:100px;width:300px;"> 
<img src="http://upload.wikimedia.org/wikipedia/commons/thumb/d/d7/Mars-Schiaparelli.jpg/280px-Mars-Schiaparelli.jpg" style="width:100%;height:100px;"/> 
</div>​