背景样式分为两种:背景颜色,背景图像
1.背景颜色background-color
语法:
backgroud-color:颜色值;
说明:
颜色值是一个关键字或一个16进制的RGB值。
示例:
#div1{background-color:green;}
#div2{background-color:#ff0000;}
2.背景图像
背景图像属性
属性 | 说明 |
---|---|
background-image | 定义背景图像的路径,这样图片才能显示 |
background-repeat | 定义背景图像显示方式,如纵、横向平铺 |
background-position | 定义背景图像在元素哪个位置 |
background-attachment | 定义背景图像是否随内容而滚动 |
<1>.background-image
在CSS中该属性是必选属性,定义了背景图像的来源。
语法:
background-image:url("图像地址");
说明:
图像地址可以是相对地址,也可以是绝对地址。
想要给某个元素设置背景图像,该元素必须要有一定的宽度和高度,背景图片才会显示出来。
<2>.background-repeat
背景重复样式,可以定义背景图像的平铺方式。
语法:
background-repeat:取值;
说明:
属性值 | 说明 |
---|---|
no-repeat | 表示不平铺 |
repeat | 默认值,表示在水平方向(X轴)和垂直方向(Y轴)同时平铺 |
repeat-x | 表示在水平方向(x轴)平铺 |
repeat-y | 表示在垂直方向(y轴)平铺 |
<3>.background-position
背景图像位置,该属性只能应用于块级元素和替换元素。其中替换元素包括==img、input、textarea、select和object.==
语法:
background-postion:像素值/关键字;
- 像素值:如 background-position:12px 24px;
设置值 | 说明,相对于左上角而言 |
---|---|
x(数值) | 设置网页的横向位置,单位为px |
y(数值) | 设置网页的纵向位置,单位为px |
2. 关键字:如 background-position:right center;
属性值 | 说明 |
---|---|
top left | 左上 |
top center | 靠上居中 |
top right | 右上 |
left center | 靠左居中 |
center center | 正中 |
right center | 靠右居中 |
bottom left | 左下 |
bottom center | 靠下居中 |
bottom right | 右下 |
<4>.background-attachment
背景固定样式,定义背景图像是随对象滚动还是固定不动。
语法:
扫描二维码关注公众号,回复:
10301632 查看本文章
background-attachment:scroll/fixed;
说明:
- scroll表示背景图像随对象滚动而滚动,是默认选项。
- fixed表示背景图像固定在页面不动,只有其他的内容随滚动条滚动。