CSS背景属性用于定义元素的背景效果,常用的有5种背景属性:背景颜色、背景图片、背景图片重复展示的方式、背景附着的方式以及背景位置
CSS中可以通过background-color属性指定元素的背景颜色,例如指定body元素的背景颜色:
body {
background-color: lightblue;
}
颜色的表示方式也有3中,具体可参见RGB颜色对照表以及详细介绍CSS中的三种颜色表示方式
CSS通过background-image属性指定元素的背景图片。 语法background-image: url| none| initial| inherit; 属性值:
body {
background-image: url("https://img-blog.csdn.net/20161118220122095");
}
CSS通过background-repeat属性指定背景图片的展示方式。 属性值:
body {
background-image: url("https://img-blog.csdn.net/20161118220122095");
background-repeat: no-repeat;
}
CSS可以通过background-position属性指定背景图片的位置。默认值为0% 0%
属性值:
例如:
body {
background-image: url("https://img-blog.csdn.net/20161118220122095");
background-repeat: no-repeat;
background-position: center
}
CSS使用 background-attachment属性指明 背景附件属性来设置背景图像是否是固定的或是与页面的其余部分一起滚动。 语法: background-attachment: scroll| fixed| local| initial| inherit; 属性值:
例如,背景图片一直显示在右上角:
body {
background-image: url("https://img-blog.csdn.net/20161118220122095");
background-repeat: no-repeat;
background-position: right top
background-attachment: fixed
}