目录
1.1.3 背景图片的重复样式background-repeat
1.背景
1.1 背景颜色、背景图片、背景图片的重复属性
1.1.1 background-color属性
- background-color属性表示背景颜色
- 背景颜色可以用十六进制、rgb()、rgba()表示法表示
- padding区域是有背景颜色的
<!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>
body{
background-color: gold;
width: 200px;
height: 200px;
padding: 100px;
background-color: rgb(209, 228, 41);
border: 1px solid #000;
}
</style>
</head>
<body>
<div class="box">12321</div>
</body>
</html>
1.1.2 背景图片 background-image
1.1.3 背景图片的重复样式background-repeat
注意,有些图片重复 可以实现无缝拼接,可以用来做网页的背景,如下图:
1.2 背景尺寸
1.2.1 background-size属性
1.2.2 contain和cover
- contain和cover是两个特殊的background-size的值
- contain表示将背景图片智能改变尺寸以容纳到盒子里
- cover表示将背景图片智能改变尺寸以撑满盒子
上述代码示例:
<!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>
.box1{
width: 500px;
height: 300px;
border: 1px solid #000;
background-image: url(images/Bloodborne™\ The\ Old\ Hunters\ Edition_20210515213015.jpg);
/* 背景尺寸 */
background-size: 300px auto;
margin-bottom: 10px;
}
.box2{
width: 500px;
height: 300px;
border: 1px solid #000;
background-image: url(images/Bloodborne™\ The\ Old\ Hunters\ Edition_20210515213015.jpg);
/* 背景尺寸 */
background-size: 25% 25%;
margin-bottom: 10px;
}
.box3{
width: 1000px;
height: 700px;
border: 1px solid #000;
background-image: url(images/Bloodborne™\ The\ Old\ Hunters\ Edition_20210515213015.jpg);
/* 背景尺寸 */
background-size: contain;
margin-bottom: 10px;
background-repeat: no-repeat;
}
.box4{
width: 1000px;
height: 300px;
border: 1px solid #000;
background-image: url(images/Bloodborne™\ The\ Old\ Hunters\ Edition_20210515213015.jpg);
/* 背景尺寸 */
background-size: cover;
margin-bottom: 10px;
}
</style>
</head>
<body>
<div class="box1"></div>
<div class="box2"></div>
<div class="box3"></div>
<div class="box4"></div>
</body>
</html>
1.3 背景裁切
1.3.1 background-clip属性
<!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>
.box1{
width: 500px;
height: 300px;
border: 10px dotted rgb(231, 217, 16);
padding: 60px;
background-image: url(images/Bloodborne™\ The\ Old\ Hunters\ Edition_20210515213015.jpg);
margin-bottom: 10px;
}
.box2{
width: 500px;
height: 300px;
border: 10px dotted rgb(226, 248, 25);
padding: 60px;
background-image: url(images/Bloodborne™\ The\ Old\ Hunters\ Edition_20210515213015.jpg);
/* 背景裁切到padding区域,此时就不会在点状线、虚线边框地下渲染 */
background-clip: padding-box;
margin-bottom: 10px;
}
.box3{
width: 500px;
height: 300px;
border: 10px dotted rgb(219, 241, 19);
padding: 60px;
background-image: url(images/Bloodborne™\ The\ Old\ Hunters\ Edition_20210515213015.jpg);
/* 背景裁切到内容区域 */
background-clip: content-box;
}
</style>
</head>
<body>
<div class="box1"></div>
<div class="box2"></div>
<div class="box3"></div>
</body>
</html>
1.4 背景固定
1.4.1 background-attachment属性
1.5 背景图片位置
1.5.1 background-position属性
<!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>
.box1{
width: 500px;
height: 300px;
border: 1px solid #000;
margin-bottom: 10px;
background-image: url(images/Bloodborne™\ The\ Old\ Hunters\ Edition_20210515213015.jpg);
background-repeat: no-repeat;
background-size: 50% auto;
/* 背景位置 */
background-position: center center;
}
</style>
</head>
<body>
<div class="box1"></div>
<div class="box2"></div>
<div class="box3"></div>
</body>
</html>
一般来说center center比较常用,其他属性建议动手敲一敲就都知道了
1.6 CSS精灵
1.7 background综合属性
非常常见!
2.渐变
2.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>
.box1{
width: 200px;
height: 200px;
border: 1px solid #000;
margin-bottom: 10px;
background-image: linear-gradient(to right,blue,red);
}
.box2{
width: 200px;
height: 200px;
border: 1px solid #000;
margin-bottom: 10px;
background-image: linear-gradient(45deg,blue,red);
}
.box3{
width: 200px;
height: 200px;
border: 1px solid #000;
margin-bottom: 10px;
background-image: linear-gradient(to right,yellow 20%,red);
}
.box4{
width: 200px;
height: 200px;
border: 1px solid #000;
margin-bottom: 10px;
background-image: linear-gradient(to right,yellow 80%,red);
}
</style>
</head>
<body>
<div class="box1"></div>
<div class="box2"></div>
<div class="box3"></div>
<div class="box4"></div>
</body>
</html>
2.1.1 浏览器私有前缀
2.2 径向渐变