如何使用 CSS 媒体查询将背景图像缩放到查看窗口 [英] How to use CSS media query to scale background-image to viewing window

查看:36
本文介绍了如何使用 CSS 媒体查询将背景图像缩放到查看窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have this large image as a background for a website:

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 似乎没有在桌面上的效果.为什么会这样,我可以使用媒体查询来解决这个问题吗?

But on my mobile device (iPhone 4S - Safari), the background-attachment: fixed does't seem to be having the same effect as it would on a desktop. Why is that and can I use a media query to fix that?

推荐答案

试试这个 - 您并不总是需要媒体查询来处理背景图像.CSS3 background-size:cover 属性可以很好地处理所有这些.以下内容适用于我测试过的所有移动设备 - 特别适用于 Android、移动版浏览器和所有平板电脑.

Try this - you don't always need a media query to handle background images. The CSS3 background-size:cover property handles all that quite well on its own. The below works well for me on all mobile devices I've tested - especially well on Android, mobile version browsers and all tablets.

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%; 
}   

这篇关于如何使用 CSS 媒体查询将背景图像缩放到查看窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆