由于 width: 100%;
看起来被广泛支持,我建议选择以下方案之一:
#header {
width: 100%;
padding: 0;
margin: 0;
}
#header img {
width: 100%;
padding: 0;
border: 0;
outline: 0;
}
<div id="header">
<img src="header.png" />
</div>
或者
#header img {
width: 100%;
padding: 0;
border: 0;
outline: 0;
}
<img src="header.png" />
仅设置width
意味着浏览器将保留图像的宽高比。但请注意,将图像操作(缩放/调整大小)传递给手机浏览器可能会导致出现不太美观的伪影。
如果您可以使用不同尺寸的图像,则可以使用@media查询来调用这些图像:
<link rel="stylesheet" href="mobileStylesheet1.css" media="only screen and (max-device width:840px)"/>
<link rel="stylesheet" href="mobileStylesheet2.css" media="only screen and (max-device width:800px)"/>
<link rel="stylesheet" href="mobileStylesheet3.css" media="only screen and (max-device width:480px)"/>
在这些样式表中,定义:
mobileStylesheet1.css
#header {
background: transparent url(path/to/840pxImage.png) 0 50% no-repeat;
}
mobileStylesheet2.css
#header {
background: transparent url(path/to/800pxImage.png) 0 50% no-repeat;
}
mobileStylesheet3.css
#header {
background: transparent url(path/to/480pxImage.png) 0 50% no-repeat;
}