我需要响应 css 背景图像。这些哪个更好?或者有什么更好的方法?
1:添加img tag
HTML:
<body>
<img src="largeimage.jpg">
<img class="smallscreen" src="smallimage.jpg>
</body>
CSS:
img.smallscreen { display: none; }
@media only screen and (max-width: 320px) {
img {
display: none;
}
img.smallscreen {
display: inline;
}
}
2:将css图片背景添加到div标签中
HTML:
<body>
<div id="image"></div>
</body>
CSS:
#image {
background-image: url(largeimage.jpg);
}
@media only screen and (max-width: 320px) {
#image {
background-image: url(smallimage.jpg);
}
}