【问题标题】:How to use CSS media query to scale background-image to viewing window如何使用 CSS 媒体查询将背景图像缩放到查看窗口
【发布时间】:2023-03-10 00:42:01
【问题描述】:

我有这张大图作为网站的背景:

body {
    background: #000 url(/assets/img/living.jpg) no-repeat center center;
    background-attachment: fixed;
    -webkit-background-size: cover;
    -moz-background-size: cover;
    -o-background-size: cover;
    background-size: cover;
    height:100%;
}

但在我的移动设备(iPhone 4S - Safari)上,background-attachment: fixed 的效果似乎与在台式机上的效果不同。为什么会这样?我可以使用媒体查询来解决这个问题吗?

【问题讨论】:

标签: css mobile mobile-safari background-image media-queries


【解决方案1】:

试试这个 - 您并不总是需要媒体查询来处理背景图片。 CSS3 background-size:cover 属性可以很好地处理这一切。以下内容在我测试过的所有移动设备上都非常适用——尤其适用于 Android、移动版浏览器和所有平板电脑。

body { 
    background-image: url(/assets/img/living.jpg); 
    background-repeat: no-repeat; 
    background-position: center;
    background-attachment: fixed;       
    webkit-background-size: cover;
    -moz-background-size: cover;
    -o-background-size: cover;
    background-size: cover;
    height:100%;
    width:100%; 
}   

【讨论】:

  • 不幸的是,覆盖和固定的无重复背景在 Android 平板电脑纵向模式下不起作用。
  • 在你的 CSS 中添加 html{height:100%,width:100%}。
  • @arp 哇哇哇。谢谢!这解决了我在移动设备上无法始终显示背景图片的问题!
猜你喜欢
  • 2014-08-14
  • 1970-01-01
  • 2016-11-30
  • 1970-01-01
  • 2012-10-13
  • 1970-01-01
  • 1970-01-01
  • 2015-06-08
  • 2012-03-19
相关资源
最近更新 更多