css background-attachment属性详解

background-attachment属性用途

background-attachment属性用于设置背景图片定位的参照方式

background-attachment可设置的值

  • scroll: 背景图片相对于容器位置固定
  • fixed: 背景图片相对于浏览器窗口固定
  • local: 背景图片相对于容器内容位置固定

background-attachment demo

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        .container{
            height: 800px;
            width: 100%;
        }
        .bg{
            height: 300px;
            width: 100%;
            margin-top: 450px;
            border: #00cdcd 2px solid;
            background-image: url("flower.png"), url("star.png"), url("star2.png"),linear-gradient(red, orange);
            background-position: bottom, left, right top;
            background-attachment: fixed, scroll, local;
            background-repeat: no-repeat, no-repeat, no-repeat;
            overflow-y: scroll;
        }
        .bg>div{
            height: 1000px
        }
    </style>
</head>
<body>
<div class="container">
    <div class="bg">
        <div></div>
    </div>
</div>
</body>
</html>


效果展示

在这里插入图片描述

滚动浏览器窗口滚动条

在这里插入图片描述

滚动div滚动条
在这里插入图片描述

附件-三个图片素材

star.png
star.png
flower.png
flower.png
star2.png
star2.png

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐