1、背景属性有以下:
背景颜色、背景图片、背景图片填充方式、背景图片大小、背景图片显示位置。
2、background-color属性
设置背景颜色
div{
width: 300px;
height: 300px;
background-color:red;
}
3、background-image属性
设置背景图片
background-image: url(./1.jpg);
4、background-repeat属性
设置背景图片填充方式,如果元素大小是小于背景图片的,那么会默认背景图片是显示不全的,只显示一部分的,如果元素大小是大于背景图片的,那么会在横向和纵向多出重复的背景图片,此时便需要进行填充设置。
background-repeat:repeat; 默认值,水平与垂直方向重复填充
background-repeat:repeat-x; 只向水平方向平铺,水平方向重复填充
background-repeat:repeat-y; 只向垂直方向平铺,垂直方向重复填充
background-repeat: no-repeat; 不重复填充
5、background-size属性
设置背景图片大小。
length 设置背景图片的宽度和高度,第一个值宽度,第二个值高度,如果只设置一个,第二个auto。
background-size: 1200px 1000px;
percentage 计算相对位置区域的百分比,第一个值宽度,第二个值高度,如果只设置一个,第二个auto。
background-size: 100% 100%;
cover 保持图片纵横比并将图片缩放成完全覆盖背景区域的最小大小,这种方式用的最多
background-size: cover;
contain 保持图片纵横比并将图片缩放成适合背景定位区域的最大大小
background-size: contain;
6、background-position属性
设置背景图片显示位置,默认是0% 0%(左上角)。
left top 左上角
left center 左中
left bottom 左下
right top 右上角
right center 右中
right bottom 右下
center top 中上
center center 中中
center bottom 中下
x% y% 第一个值是水平位置,第二个值是垂直位置,
左上角是0% 0%,右下角是100% 100%。如果只指定了一个值,别一个值为50%。
示例代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>背景属性</title>
<style>
div{
width: 2040px;
height: 1020px;
background-image: url(./1.jpg);
background-repeat: no-repeat;
background-size: cover;
}
</style>
</head>
<body>
<div>
</div>
</body>
</html>