CSS background-attachment,背景图像固定或滚动
设置背景图像固定或滚动
CSS background-attachment属性(右上角的背景图像为本章节示例),可以用于设置HTML文档元素的背景图像是固定还是随着页面的滚动而滚动。如当前页面的右上角的背景图像,在滚动页面的时候,背景图像的位置“好像还是在那里”,这就是背景图像不随页面滚动的用法。backgroundpattachment属性的值如下表:
属性值 | 描述 |
---|---|
fixed | 固定 |
scroll | 滚动 |
inherit | 从父元素继承 |
background-attachment示例代码
下方在线编辑器内的代码便是右上角背景图像的示例代码,如果在线编辑并运行的话,比如将background-attachment的fixed属性值改为scroll,然后关掉在线编辑器,并滚动页面,查看背景图像的滚动效果。
<style>
body{background-image:url("../../static/img/bmi.jpg");background-attachment:fixed;background-repeat:no-repeat;background-size:200px 200px;background-position:right top;}
</style>