1: background-color 设置背景颜色
2:background-image来设置背景图片
- 语法:background-image:url(相对路径);
-可以同时为一个元素指定背景颜色和背景图片,
这样背景颜色将会作为背景图片的底色
-图片在元素中的位置
如果背景图片大于元素,默认会显示图片的左上角
如果背景图片和元素一样大,则会将背景图片全部显示
如果背景图片小于元素大小,则会默认将背景图片平铺以充满元素
3:background-repeat用于设置背景图片的重复方式
可选值:
repeat,默认值,背景图片会双方向重复(平铺)
no-repeat ,背景图片不会重复,有多大就显示多大
repeat-x, 背景图片沿水平方向重复
repeat-y,背景图片沿垂直方向重复
4:background-position可以调整背景图片在元素中的位置
背景图片默认是贴着元素的左上角显示
可选值:
该属性可以使用 top right left bottom center中的两个值
来指定一个背景图片的位置
top left 左上
bottom right 右下
如果只给出一个值,则第二个值默认是center
也可以直接指定两个偏移量,
第一个值是水平偏移量
- 如果指定的是一个正值,则图片会向右移动指定的像素
- 如果指定的是一个负值,则图片会向左移动指定的像素
第二个是垂直偏移量
- 如果指定的是一个正值,则图片会向下移动指定的像素
- 如果指定的是一个负值,则图片会向上移动指定的像素
<style type="text/css">
* {
margin: 0;
padding: 0;
}
.box1 {
height: 500px;
width: 500px;
padding: 20px;
border: 10px red double;
margin: 0 auto;
/*设置一个背景颜色*/
background-color: #bfa;
background-clip: content-box;
/* 引入背景图 */
background-image: url(./img/小图.webp);
/* 设置图片不重复 */
background-repeat: no-repeat;
/* 设置图片的位置 */
background-position: 100px 50px;
/* 设置图片偏移的原点 */
background-origin: border-box;
}
.box2{
width: 100%;
height: 100%;
background-color: rgba(255, 235, 205,.6);
}
</style>
5:background-clip 设置背景色的范围
可选值:
border-box 默认值,背景颜色会出现在边框的下边
padding-box 背景不会出现在边框,只会出现在内容区和内边距
content-box 背景只出现在内容区
6:background-origin
设置背景图片的偏移量计算的原点,配合偏移量使用的
padding-box 从内部距处开始计算 默认值
content-box 背景图片的偏移量从内容区处计算
border-box 从边框开始计算偏移量
7:background-size 设置背景图片的大小
参数:
第一个值:宽度
第二个值:高度
如果只写一个,第二值,默认为auto
cover 图片的比例不变,将元素铺满 (比较常用)
contain 图片比例不变,将图片完整显示
<style type="text/css">
* {
margin: 0;
padding: 0;
}
.box1 {
height: 500px;
width: 500px;
border: 1px solid red;
margin: 50px auto;
/* 引入背景图 */
/* background-image: url(./img/小图.webp); */
background-image: url(./img/大图2.webp);
/* 背景图不重复 */
background-repeat: no-repeat;
/* 设置背景图的大小 */
/* background-size: 300px auto; */
background-size:cover;
}
</style>
</head>
<body>
<div class="box1"></div>
</body>
background
- 通过该属性可以同时设置所有背景相关的样式,
样式值之间用空格隔开即可
- 没有顺序的要求,谁在前谁在后都行
也没有数量的要求,不写的样式就使用默认值
-background-size要写在background-position后面
设置一个背景颜色
background-color: red;
设置一个背景图片
background-image: url(./img/小图.webp);
设置背景不重复
background-repeat: no-repeat;
设置背景图片的位置
background-position: center;
设置图片大小
background-size: 150px;
设置图片偏移的原点
background-origin: border-box;
设置背景色的范围
background-clip: padding-box;
应用实例:去哪儿风景图制作
<!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>
<link rel="stylesheet" href="./css/去哪儿风景.css">
</head>
<body>
<div class="box">
<div class="b1">
<div class="b2">
<div class="b3">
<p class="a1"> 合肥-丽江</p>
<p class="a2">6天跟团游</p>
</div>
</div>
</div>
<div class="b4">
<p class="a3">含往返飞机票+5晚住宿 </p>
<p class="a4">¥3894</p>
</div>
</div>
<div class="box ">
<div class="b6">
<div class="b5">
<div class="b3">
<p class="a1"> 合肥-稻城亚丁</p>
<p class="a2">7天跟团游</p>
</div>
</div>
</div>
<div class="b4">
<p class="a3">含往返飞机票+6晚住宿 </p>
<p class="a4">¥4256</p>
</div>
</div>
<div class="box">
<div class="b7">
<div class="b2">
<div class="b3">
<p class="a1"> 合肥-青海湖</p>
<p class="a2">9天跟团游</p>
</div>
</div>
</div>
<div class="b4">
<p class="a3">含往返飞机票+8晚住宿 </p>
<p class="a4">¥5063</p>
</div>
</div>
<div class="box">
<div class="b8">
<div class="b2">
<div class="b3">
<p class="a1"> 合肥-三亚</p>
<p class="a2">5天自由行</p>
</div>
</div>
</div>
<div class="b4">
<p class="a3">含往返飞机票(不含税)+4晚住宿 </p>
<p class="a4">¥3170</p>
</div>
</div>
<div class="box">
<div class="b9">
<div class="b2">
<div class="b3">
<p class="a1"> 合肥-黄山</p>
<p class="a2">4天跟团游</p>
</div>
</div>
</div>
<div class="b4">
<p class="a3">含往返火车票+3晚住宿 </p>
<p class="a4">¥2380</p>
</div>
</div>
<div class="box">
<div class="b10">
<div class="b2">
<div class="b3">
<p class="a1"> 合肥-杭州</p>
<p class="a2">5天跟团游</p>
</div>
</div>
</div>
<div class="b4">
<p class="a3">含往返火车票+4晚住宿 </p>
<p class="a4">¥2070</p>
</div>
</div>
</body>
</html>
CSS
* {
margin: 0;
padding: 0;
list-style: none;
text-decoration: none;
}
body{
width: 370px;
}
.b1{
width: 370px;
height: 25px;
border: 2px;
border-style: solid;
border-color:#D4D4D9;
margin-top: 20px;
margin-bottom: 20px;
}
.box{
width:370px ;
height: 170px;
}
.b2{
width: 121px;
height: 130px;
background-image:url(../image/3.gif) ;
background-repeat: no-repeat;
background-position: center;
border: 2px;
border-style: solid;
border-color:#D4D4D9;
}
.box>.b3{
width: 200px;
height: 130px;
float: right;
margin-top: -130px;
margin-left: -40px;
padding-right: 30px;
}
p,li{
line-height: 35px;
font-size: 14px;
color: rgb(88, 85, 85);
}
.b4{
font-size: 14px;
color: rgb(88, 85, 85);
}
ul>li>.b5{
/* margin-right: 50px; */
color:#8C8F92;
float: right;
}
.b6{
margin-left: 15px;
font-weight: bolder;
}
.b7{
margin-left: 15px;
}
.b8{
font-weight: bolder;
color: black;
}
效果图