html – 正确使用背景图像的媒体视口

前端之家收集整理的这篇文章主要介绍了html – 正确使用背景图像的媒体视口前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我不是一个前端开发者,但我正在写一个简单的单页网站,其中包含一个图像.我已创建此图像的几个版本,以最小化要在小屏幕设备中下载的位.这个图像应该水平“展开”,所以我还创建了一些应该水平重复的背景图像.见下面的模型:@H_403_2@

mockup

@H_403_2@

在我写的代码下面.这是正确的方法吗?我的代码中是否有任何错误,或者这是使用视口的正确方法?虽然它在我的320px iPhone屏幕上运行良好,但我无法在我的桌面浏览器上使用320px版本.总的来说,我并不满意.@H_403_2@

HTML:@H_403_2@

@H_403_2@

CSS:@H_403_2@

@H_403_2@

最佳答案
看看这支笔:https://codepen.io/anon/pen/XqJRKM@H_403_2@

基本上,解决方案是:@H_403_2@

@H_403_2@

#image {
  background-image: url("http://lorempixel.com/output/sports-h-c-1-480-10.jpg");
  background-repeat: repeat;
  background-size:contain;
  width: 100vw;
  background-color:#f00;
}

@media (min-width: 480px) {
  #image {
    background-image: url("http://lorempixel.com/output/sports-h-c-1-480-5.jpg");
    background-repeat: repeat;
  }
}
@media (min-width: 800px) {
  #image {
    background-image: url("http://lorempixel.com/output/sports-h-c-1-480-6.jpg");
    background-repeat: repeat;
  }
}
@media (min-width: 1200px) {
  #image {
    background-image: url("http://lorempixel.com/output/sports-h-c-1-480-7.jpg");
    background-repeat: repeat;
  }
}

#image img{
  width:80vw;
  height:auto;
  max-width:1200px;
}

HTML:@H_403_2@

@H_403_2@

请注意,使用此示例,original_image.jpg将仅显示在旧浏览器上.它将始终在Chrome v38上方的浏览器上覆盖

原文链接:https://www.f2er.com/html/425638.html

猜你在找的HTML相关文章