一、背景
1、 添加背景图片:
background-image:url(路径);
-如果插入小图会横向,纵向平铺。
-背景图大小合适则正常显示。
-如果插入的图片大,默认显示左上角。
2、添加背景颜色:
background-color:;
3、图片不平铺
backgroud-repeat:;
可选值:
repeat:默认值,背景会双向平铺;
no-repeat:不重复;
repeat-x:在x轴上平铺;
repeat-y:在y轴上平铺;
4、调整背景图片位置(相对于内边距)
如果只写第一个值,第二个值默认是center
第一种写法:默认值是center
background-position:left top;左上方
background-position:right top;右上
background-position:left bottom;左下方
background-position:right bottom;右下方
background-position:center center;居中
第二种写法:
background-position:h v;
参数:
h:水平方向
正值向右,负值向左
v:垂直方向
正值向下,负值向上;
5、设置背景色的范围
background-clip:;
bordrer-box;默认值;背景颜色会出现在边框的下面
padding-box;背景颜色会出现在内边距的下面
content-box;背景颜色会出现在内容的下面
6、设置背景图片偏移量计算的远点
background-origin;(配合position使用)
border-box;以外边框为原点
content-box;以内容区域为原点
padding-box;从内部边距开始计算 默认值
7、设置背景图片的大小
写法一:
background-size:x y;
参数:
x:设置图片宽度
y:设置图片高度
如果只写一个值图片会自适应。
写法二:
contain:图片比例不变,将图片完整显示,(可能不铺满整个元素)
cover:图片比例不变,将元素铺满
8、background简写:
-可以同时设置所有背景相关的样式
-没有顺序要求
-没有数量要求
注意:background-size要写在background-position后面
backrgound: #bfa url(“./img”)conter/200px no-repeat;
二、雪碧图(图片整合技术CSS-Sprite)/精灵图
优点:
将多张图片整合在一张图片里面,减小了内存,浏览器只需要发送一次请求即可,同时加载多张图片
2、使用步骤:
-确定使用图标
-测量图片大小
-根据测量结果创建一个元素
-将雪碧图设置为元素的背景
-设置一个偏移量以显示正确的图片
<!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>
*{
padding: 0px;
margin: 0px;
}
div{
width: 72px;
height: 72px;
border: 1px solid red;
margin: 100px auto;
}
div:nth-of-type(1){
background: url(125.png) no-repeat 0px -82px;
}
div:nth-of-type(2){
background: url(125.png) no-repeat 0px -160px;
}
</style>
</head>
<body>
<div></div>
<div></div>
</body>
</html>
三、渐变色
background-image:linear-gradient();
1、linear-gradient(方位,颜色1,颜色2)
线性渐变,颜色沿着一条直线变化
参数:
方位(默认是to bottom):
写法一
to bottom,to top,to right ,to left,
写法二
xxxdeg表示角度,度数,按顺时针方向
写法三
turn表示圈数一圈360° .5turn
2、repeating-linear-gradient:(颜色1 px ,颜色2 px);重复颜色渐变
px差值循环渐变
3、放射型颜色渐变:
radial-gradient()