背景图
怎样插入背景图
方式: background-image: url(背景图地址);
相关属性
属性 | 描述 |
---|---|
background-repeat | 设置或检索对象的背景图像如何铺排填充 |
background-attachment | 设置或检索对象的背景图像是随对象内容滚动还是固定的 |
background-position | 设置或检索对象的背景图像位置 |
background-origin | 设置或检索对象的背景图像显示的原点 |
background-clip | 检索或设置对象的背景向外裁剪的区域 |
background-size | 检索或设置对象的背景图像的尺寸大小 |
1.背景重复background-repeat
属性 | 描述 |
---|---|
repeat-x | 背景图像在横向上平铺 |
repeat-y | 背景图像在纵向上平铺 |
repeat | 背景图像在横向和纵向平铺 |
no-repeat | 背景图像不平铺 |
round | 背景图像自动缩放直到适应且填充满整个容器。 |
space | 背景图像以相同的间距平铺且填充满整个容器或某个方向。 |
例子:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="">
</head>
<style>
body{
height: 200px;
width:400px;
background-image: url(./img/man.png);
background-repeat: repeat;
}
</style>
<body>
</div>
</html>
效果图如下所示:
1) 默认情况下(repeat
)
2) 不平铺 (no-repeat
)
3) 横向上平铺 (repeat-x
)
4) 纵向上平铺 (repeat-y
)
5) 适应填充(round
)
6)平铺填充(space
)
2.位置是否固定background-attachment
属性 | 描述 |
---|---|
fixed | 背景图像相对于窗体固定 |
scroll | 背景图像相对于元素固定,也就是说当元素内容滚动时背景图像不会跟着滚动,因为背景图像总是要跟着元素本身。但会随元素的祖先元素或窗体一起滚动 |
local | 背景图像相对于元素内容固定,也就是说当元素随元素滚动时背景图像也会跟着滚动,因为背景图像总是要跟着内容 |
例子:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="">
</head>
<style>
div{
height: 200px;
width:200px;
background-image: url(./img/man.png);
overflow:scroll;
background-attachment:local;
}
body{
height: 1000px;
}
</style>
<body>
<div>
加油加油加油加油加油加油加油加油加油加油加油加油
加油加油加油加油加油加油加油加油加油加油加油加油
加油加油加油加油加油加油加油加油加油加油加油加油
加油加油加油加油加油加油加油加油加油加油加油加油
加油加油加油加油加油加油加油加油加油加油加油加油
加油加油加油加油加油加油加油加油加油加油加油加油
加油加油加油加油加油加油加油加油加油加油加油加油
加油加油加油加油加油加油加油加油加油加油加油加油
加油加油加油加油加油加油加油加油加油加油加油加油
加油加油加油加油加油加油加油加油加油加油加油加油
加油加油加油加油加油加油加油加油加油加油加油加油
加油加油加油加油加油加油加油加油加油加油加油加油
加油加油加油加油加油加油加油加油加油加油加油加油
加油加油加油加油加油加油加油加油加油加油加油加油
加油加油加油加油加油加油加油加油加油加油加油加油
加油加油加油加油加油加油加油加油加油加油加油加油
加油加油加油加油加油加油加油加油加油加油加油加油
加油加油加油加油加油加油加油加油加油加油加油加油
</div>
</div>
</html>
效果图如下:
1) 默认情况下(scroll
)
由观察可知,随着div内容的滚动,背景图并没有发生滚动。
2)固定(fixed
)
3)相对元素内容固定(local
)
3.位置background-position
属性 | 描述 |
---|---|
用百分比指定背景图像填充的位置。可以为负值。其参考的尺寸为容器大小减去背景图片大小 | |
用长度值指定背景图像填充的位置。可以为负值。 | |
center | 背景图像横向和纵向居中。 |
left | 背景图像在横向上填充从左边开始。 |
right | 背景图像在横向上填充从右边开始。 |
top | 背景图像在纵向上填充从顶部开始。 |
bottom | 背景图像在纵向上填充从底部开始。 |
例子如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="">
</head>
<style>
div{
height: 200px;
width:200px;
background-image: url(./img/man.png);
border: 10px solid black;
background-position:top;
}
body{
height: 1000px;
}
</style>
<body>
<div>
</div>
</div>
</html>
1.center
2.left
3.right
4.bottom
5.top
4.开始显示的位置background-origin
属性 | 描述 |
---|---|
padding-box | 从padding区域(含padding)开始显示背景图像。 |
border-box | 从border区域(含border)开始显示背景图像。 |
content-box | 从content区域开始显示背景图像。 |
例子如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="">
</head>
<style>
div{
height: 200px;
width:200px;
background-image: url(./img/man.png);
border: 10px solid black;
background-origin:content-box;
}
body{
height: 1000px;
}
</style>
<body>
<div>
</div>
</div>
</html>
1.padding-box
2.border-box
3.content-box
5.背景图的大小background-size
属性 | 描述 |
---|---|
用长度值指定背景图像大小。不允许负值。 | |
用百分比指定背景图像大小。不允许负值。 | |
auto | 背景图像的真实大小。 |
cover | 将背景图像等比缩放到完全覆盖容器,背景图像有可能超出容器。 |
contain | 将背景图像等比缩放到宽度或高度与容器的宽度或高度相等,背景图像始终被包含在容器内。 |
例子:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="">
</head>
<style>
div{
height: 200px;
width:200px;
background-image: url(./img/man.png);
border: 10px solid black;
background-size: auto;
}
body{
height: 1000px;
}
</style>
<body>
<div>
</div>
</div>
</html>
1.contain
2.cover
3.auto