提示
本文主要讲解 CSS 中背景的相关样式。@ermo
# 背景样式
# background-color
background-color
表示背景颜色,有以下语法:
background-color: pink;
,使用颜色关键字background-color: #ccc;
,使用十六进制表示法background-color: rgb(0, 0, 0);
,使用 rgb 表示法background-color: rgba(0, 0, 0, 0);
,使用 rgba 表示法,最后以为代表透明度,取值为0~1
<!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>
#div1 {
background-color: pink;
}
#div2 {
background-color: #FF00FF;
}
#div3 {
background-color: rgb(0, 0, 0);
}
#div4 {
background-color: rgb(0, 0, 0, 0.5);
}
</style>
</head>
<body>
<div id="div1">div1</div>
<div id="div2">div2</div>
<div id="div3">div3</div>
<div id="div4">div4</div>
</body>
</html>
# background-image
background-image
表示背景图片。
<!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: 400px;
height: 400px;
background-image: url(./img/logo.png);
}
</style>
</head>
<body>
<div>
</div>
</body>
</html>
# background-repeat
background-repeat
表示背景平铺,有4中取值:
repeat
,水平和垂直方向都平铺,为默认值norepeat
,不平铺repeat-x
,水平方向平铺repeat-y
,垂直方向平铺
<!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: 2000px;
height: 2000px;
background-image: url(./img/logo.png);
background-repeat: no-repeat;
/* background-repeat: repeat-x; */
/* background-repeat: repeat-y; */
/* background-repeat: repeat; */
}
</style>
</head>
<body>
<div>
</div>
</body>
</html>
# background-positon
background-positon
表示背景图的位置。取值可以使用关键字和像素两种方式。
第一个值代表水平方向取值,第二个值代表垂直方向取值。
网页默认取值为左上角,即 left top。
- 水平方向,left、center、right
- 垂直方向,top、center、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>
<style>
div {
width: 400px;
height: 400px;
background-color: pink;
background-image: url(./img/1.jpg);
background-repeat: no-repeat;
background-position: center center;
/* background-position: rigth bottom; */
/* background-position: center; */
/* background-position: 50px 0; */
/* background-position: -50px 0; */
}
</style>
</head>
<body>
<div>div</div>
</body>
</html>
# background
background
是背景的复合属性。
语法为:
<style>
background: color image repeat position
</style>
<!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: 400px;
height: 400px;
background: pink url(./img/1.jpg) no-repeat center center;
}
</style>
</head>
<body>
<div></div>
</body>
</html>