css background-attachment属性基础介绍

小云云
小云云 原创
2017-12-19 13:17:45 2183浏览

本文主要为大家介绍了在CSS中,使用背景附件属性background-attachment可以设置背景图像是随对象滚动还是固定不动,需要的朋友可以参考下,希望能帮助大家。

一、background-attachment属性

在CSS中,使用背景附件属性background-attachment可以设置背景图像是随对象滚动还是固定不动。

语法:

background-attachment:scroll/fixed;

说明:

background-attachment 属性只有2个属性值。scroll表示背景图像随对象滚动而滚动,是默认选项;fixed表示背景图像固定在页面不动,只有其他的内容随滚动条滚动。

举例:

<!DOCTYPE html> 
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <style type="text/css">
        #p1
        {
            width:160px;
            height:1200px;
            border:1px solid gray;
            background-image:url("cartoongirl.gif");
            background-repeat:no-repeat;
            background-attachment:fixed;
        }
    </style>
</head>
<body>
    <p id="p1"></p>
</body>
</html>

在浏览器预览效果如下:

分析:

大家在浏览器中拖动右边的滚动条会发现,背景图片在页面固定不动。

在IE或者360中设置background-attachment之后不能设置background-position属性,不然图片没办法在浏览器显示。大家可以测试一下google浏览器、Firefox浏览器。

相关推荐:

css background-attachment属性的使用与定义

background-attachment PHP源代码数组统计count分析

谷歌Chrome浏览器 兼容background-attachment:fixed;吗?如何做到火狐背景图片固定的效果_html/css_WEB-ITnose

以上就是css background-attachment属性基础介绍的详细内容,更多请关注php中文网其它相关文章!

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。