一.背景颜色
代码:
<!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>
<style>
div {
width: 100px;
height: 100px;
/* background-color: transparent; 透明的,清澈的 */
background-color: pink;
}
</style>
</head>
<body>
<div></div>
</body>
</html>
运行结果
二.图片背景
代码:
<!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>
<style>
div {
width: 800px;
height: 500px;
/* 不要落下 url() */
/* background-image: url(地址); */
background-image: url(images/logo.png);
}
</style>
</head>
<body>
<div></div>
</body>
</html>
运行:
三 .背景平铺
代码
<!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>
<style>
div {
width: 800px;
height: 500px;
background-color: pink;
/* 不要落下 url() */
/* background-image: url(地址); */
background-image: url(images/logo.png);
/*1 背景图不平铺 */
/* background-repeat: no-repeat; */
/* 2 默认情况下都是平铺的 */
/* background-repeat: repeat; */
/* 3 沿着X轴平铺 */
/* background-repeat: repeat-x; */
/* 4 沿着y轴平铺 */
background-repeat: repeat-y;
/* 页面元素既可以添加背景颜色也可以添加背景图片,只不过背景图片会压住背景颜色 */
}
</style>
</head>
<body>
<div></div>
</body>
</html>
结果:
四. 背景图片位置
代码:
<!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>20</title>
<style>
div {
width: 800px;
height: 600px;
background-color: pink;
background-image: url(images/logo.png);
/*1 背景图不平铺 */
background-repeat: no-repeat;
/* background-position: 方位名词 */
/* background-position: center top; */
/* background-position: right center; */
/* 如果是方位名词 right center和 center right效果是等价的,和顺序无关 */
/* background-position: center right; */
/* 此时 水平一定是靠右侧对齐的,第二个参数省略y轴,是垂直居中显示的 */
/* background-position: right; */
/* 此时 第一个参数一定是top, Y轴顶部对齐,第二个参数省略X轴,是水平居中显示的 */
background-position: top;
}
</style>
</head>
<body>
<div></div>
</body>
</html>
结果:
例一
做一个
代码:
<!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>21-背景方位名词的应用</title>
<style>
h3 {
width: 118px;
height: 41px;
/* background-color: pink; */
font-size: 14px;
font-weight: 400;
line-height: 41px;
background-image: url(images/icon.png);
background-repeat: no-repeat;
background-position: left center;
text-indent: 2em;
}
</style>
</head>
<body>
<h3>
成长守护平台
</h3>
</body>
</html>
结果:
例二
做一个王者荣耀的背景图片:
五.背景图片的位置-精确单位
代码:
<!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>精确</title>
<style>
div {
width: 300px;
height: 300px;
background-color: pink;
background-image: url(images/logo.png);
background-repeat: no-repeat;
/* 20px, 50px; x轴一定是20;y轴一定是50; */
/* background-position: 20px, 50px; */
/* background-position: 50px, 20px; */
/* background-position: 20px; */
background-position: 20px center;
}
</style>
</head>
<body>
<div></div>
</body>
</html>
结果:
六 背景图片位置-参数混合单位
代码:
<!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>大图</title>
<style>
body {
background-image: url(images/bg.jpg);
background-repeat: no-repeat;
/* background-position: center top; */
background-position: center 41px;
}
</style>
<body>
</body>
</html>
运行结果
七.背景固定
代码:
<!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>背景固定</title>
<style>
body {
background-image: url(images/bg.jpg);
background-repeat: no-repeat;
background-position: center top;
color: #fff;
font-size: 20px;
/* 把背景图片固定住 */
background-attachment: fixed;
}
</style>
</head>
<body>
<p>天王盖地虎,pink老师两米五</p>
<p>天王盖地虎,pink老师两米五</p>
<p>天王盖地虎,pink老师两米五</p>
<p>天王盖地虎,pink老师两米五</p>
<p>天王盖地虎,pink老师两米五</p>
<p>天王盖地虎,pink老师两米五</p>
<p>天王盖地虎,pink老师两米五</p>
<p>天王盖地虎,pink老师两米五</p>
<p>天王盖地虎,pink老师两米五</p>
<p>天王盖地虎,pink老师两米五</p>
<p>天王盖地虎,pink老师两米五</p>
<p>天王盖地虎,pink老师两米五</p>
<p>天王盖地虎,pink老师两米五</p>
<p>天王盖地虎,pink老师两米五</p>
<p>天王盖地虎,pink老师两米五</p>
<p>天王盖地虎,pink老师两米五</p>
<p>天王盖地虎,pink老师两米五</p>
<p>天王盖地虎,pink老师两米五</p>
<p>天王盖地虎,pink老师两米五</p>
<p>天王盖地虎,pink老师两米五</p>
<p>天王盖地虎,pink老师两米五</p>
<p>天王盖地虎,pink老师两米五</p>
<p>天王盖地虎,pink老师两米五</p>
<p>天王盖地虎,pink老师两米五</p>
<p>天王盖地虎,pink老师两米五</p>
<p>天王盖地虎,pink老师两米五</p>
<p>天王盖地虎,pink老师两米五</p>
<p>天王盖地虎,pink老师两米五</p>
<p>天王盖地虎,pink老师两米五</p>
<p>天王盖地虎,pink老师两米五</p>
<p>天王盖地虎,pink老师两米五</p>
<p>天王盖地虎,pink老师两米五</p>
</body>
</html>
结果:
八.背景图片复合写法
代码:
<!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>背景固定</title>
<style>
body {
/* background-image: url(images/bg.jpg);
background-repeat: no-repeat;
background-position: center top;
/* 把背景图片固定住 */
/* background-attachment: fixed; */
background: black url(images/bg.jpg) no-repeat fixed center top;
color: #fff;
font-size: 20px;
}
</style>
</head>
<body>
<p>天王盖地虎,pink老师两米五</p>
<p>天王盖地虎,pink老师两米五</p>
<p>天王盖地虎,pink老师两米五</p>
<p>天王盖地虎,pink老师两米五</p>
<p>天王盖地虎,pink老师两米五</p>
<p>天王盖地虎,pink老师两米五</p>
<p>天王盖地虎,pink老师两米五</p>
<p>天王盖地虎,pink老师两米五</p>
<p>天王盖地虎,pink老师两米五</p>
<p>天王盖地虎,pink老师两米五</p>
<p>天王盖地虎,pink老师两米五</p>
<p>天王盖地虎,pink老师两米五</p>
<p>天王盖地虎,pink老师两米五</p>
<p>天王盖地虎,pink老师两米五</p>
<p>天王盖地虎,pink老师两米五</p>
<p>天王盖地虎,pink老师两米五</p>
<p>天王盖地虎,pink老师两米五</p>
<p>天王盖地虎,pink老师两米五</p>
<p>天王盖地虎,pink老师两米五</p>
<p>天王盖地虎,pink老师两米五</p>
<p>天王盖地虎,pink老师两米五</p>
<p>天王盖地虎,pink老师两米五</p>
<p>天王盖地虎,pink老师两米五</p>
<p>天王盖地虎,pink老师两米五</p>
<p>天王盖地虎,pink老师两米五</p>
<p>天王盖地虎,pink老师两米五</p>
<p>天王盖地虎,pink老师两米五</p>
<p>天王盖地虎,pink老师两米五</p>
<p>天王盖地虎,pink老师两米五</p>
<p>天王盖地虎,pink老师两米五</p>
<p>天王盖地虎,pink老师两米五</p>
<p>天王盖地虎,pink老师两米五</p>
</body>
</html>
结果
九.背景色半透明
代码
<!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>背景色半透明</title>
<style>
div {
width: 300px;
height: 300px;
/* background-color: black; */
/* background: rgba(0, 0, 0, 0.3); */
background: rgba(0, 0, 0, .3);
}
</style>
</head>
<body>
<div>隐形的翅膀</div>
</body>
</html>