2012-03-16 106 views
7

我有一张图片,我希望宽度填满浏览器窗口,无论窗口的大小如何。拉伸图片以填充浏览器窗口的宽度

如何在HTML和CSS中执行此操作?

+0

[img height小于窗口高度时,下面没有空格的CSS或jQuery可缩放背景图像的可能重复](http://stackoverflow.com/questions/5072750/css-or-jquery-scalable-background- image-with-no-white-space-under-when-img) – Joseph 2012-03-16 02:54:06

回答

5
<img src="filename.jpg" alt="alt text" width="100%" /> 

这假设图像的容器和浏览器窗口一样宽。

+1

这对HTML 5无效。在HTML5中,值必须以像素为单位。 – sbagdat 2012-03-16 03:03:23

+0

@sbagdat有趣。我不知道。任何想法为什么百分比作为选项被删除? – 2012-03-16 03:06:10

+0

可能当图像越来越大,它们正在失去质量。这里是w3c链接:http://www.w3schools.com/html5/att_img_width.asp – sbagdat 2012-03-16 03:11:01

10

您可以添加一个div宽度100%的高度,还设置图像的宽度和高度均为100%

<div id="wrapper"> 
    <img src="http://lorempixel.com/200/200" /> 
</div>​​​​​​​​​​ 

CSS:

#wrapper, img{ 
    width:100%; 
    height: 100%; 
} 

jsfiddle

1
<div class="fixpixel"> 
    <img src="ImagePath" /> 
</div>​​​​​​​​​​ 

CSS文件

.fixpixel img{ 
height: auto; 
background-position: center; 
background-repeat: no-repeat; 
background-size: cover; 
} 
相关问题