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

查看:463
本文介绍了如何使用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) $ c> 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 shouldn't need height:100% or even necessarily need a media query to handle the background image. The CSS3 background-size:cover property handles that pretty well on its own. Works well for me on all mobile devices I've tested - especially well on Android, mobile version browsers and all tablets.

body {
    background-color: #000; 
    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; 
}   

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

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