提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
一、背景
1.背景颜色
background-color: red;
2.背景图片
background-image: url("img/dog.jpg");
- 可以同时设置背景颜色和背景图片,这样背景颜色就成为了背景图片的背景
- 如果背景图片小于元素,则背景图片会自动在元素中重复平铺将元素占满
- 如果背景图片大于元素,将不完全显示
background-repeat:repeat-x;
background-repeat用来设置背景的重复方式
可选值:
- repeat 默认值,背景会沿着x轴、y轴双方向重复
- repeat-x 沿着x轴方向重复
- repeat-y 沿着y轴方向重复
- no-repeat 不重复
background-position:center;
background-position用来设置背景图片的位置
可选值:
- top、left、right、bottom、center使用方位时必须同时指定两个值,也可只写一个
.box1{
width: 1000px;
height: 1000px;
background-image: url("img/轮播图2.jpg");
background-color: gray;
background-repeat: no-repeat;
background-position: left bottom;
}
还可以用偏移量指定背景图片的位置:
background-position:-50px 150px;
background-clip 设置背景的范围
可选值:
- border-box 默认值,背景会出现在边框的下边
- padding-box 背景不会出现在边框,只会出现在内容区和内边距
- content-box 背景只会出现在内容区
background-origin背景图片的偏移量计算的原点
可选值:
- padding-box 默认值,background-position从内边距开始计算
- content-box 背景图片的偏移量从内容区开始计算
- border-box 背景图片的偏移量从边框处开始计算
background-size设置背景图片的大小
可选值:
- 第一个值表示宽度 第二个值表示高度,如果只写一个另一个值为auto
- cover 图片的比例不变,将元素铺满
- contain 图片比例不变,在元素中完整显示
background-attachment设置背景图片是否跟随元素移动
可选值:
- scroll 默认值,背景图片会随之元素移动而移动
- fixed 背景会固定在页面中,不会随元素移动
background背景相关的简写属性,所有背景的样式都通过该样式设置
注:position要写在size前面,并用/分开;origin要写在clip前面
.box1{
width: 500px;
height: 500px;
border: 10px red double;
padding: 50px;
background: url("img/轮播图4.jpg") yellowgreen no-repeat center center/contain border-box content-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>Document</title>
<style>
a:link{
display: block;
width: 93px;
height: 29px;
background-image: url("img/link.png");
}
a:hover{
background-image: url("img/hover.png");
}
a:active{
background-image: url("img/active.png");
}
</style>
</head>
<body>
<a href="#"></a>
</body>
</html>
图片闪烁问题:
- 图片属于网页中的外部资源,外部资源都需要浏览器单独发送请求加载
解决方法
浏览器加载外部资源时是需要加载的,用则加载,不用就不加载- 可以将多个小图片统一保存到一个大图片,然后通过调整background-position,这样图片会同时加载到网页中,就可以有效避免出现闪烁
这个方法称为 CSS-Sprite,也称为 雪碧图
a:link{
display: block;
width: 93px;
height: 29px;
background-image: url("img/btn.png");
}
a:hover{
background-position: -93px 0;
}
a:active{
background-position: -186px 0;
}
三、线性渐变
通过渐变可以设置一些复杂的背景颜色,可以实现从一个颜色向其他颜色过渡的效果渐变是图片,需要通过background-image设置
-
线性渐变,颜色沿着一条直线发生变化
-
linear-gradient()
linear-gradient(red,yellow)红色在开头,黄色在结尾,中间是过渡区域.
可以指定线性渐变的方向 to left /to right /to bottom /to top /xxxdeg 表示度数 /to top left /.25 turn 表示圈数
box1{
width: 500px;
height: 500px;
background-image: linear-gradient(.45turn,#e3e4e5,pink);
}
可以写多个颜色,平均分布,也可以手动指定渐变的分布情况
background-image: linear-gradient(#e3e4e5 50px,pink 200px);
repeating-linear-gradient()可以平铺的线性渐变
background-image: repeating-linear-gradient(#e3e4e5,pink 100px);
四、径向渐变
radial-gradient()径向渐变(放射性的效果)
-
默认情况下径向渐变的形状根据元素的形状来计算
- 正方形 - - >圆形
- 长方形 - ->椭圆形 也可以手动指定
-
- radial-gradient(100px 100px,red,yellow);
-
- circle:圆形radial-gradient(circle,red,yellow);
-
- ellipse:椭圆形radial-gradient(ellipse,red,yellow);
-
- 也可以指定渐变的位置radial-gradient(at top left,red,yellow);
也可以指定渐变的位置
语法:
radial-gradient(大小 at 位置 ,颜色 位置 ,颜色 位置 ,颜色 位置)
注:位置前面要用at开头,也可以用像素值,大小位置可省略不写
-
大小:
-
circle 圆形
ellipse 椭圆
closest-side 近边
closest-corner 近角
farthest-side 远边
farthest-corner 远角
位置
- top right left center bottom
background-image: repeating-radial-gradient(100px 100px,pink,#e3e4e5);
结果:
五、电影卡片练习
<!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/reset.css">
<link rel="stylesheet" href="fa/css/all.css">
<style>
.outer{
width: 240px;
margin: 100px auto;
box-shadow: 0 0 10px rgba(0, 0, 0, .3);
}
.img-wrapper img{
width: 100%;
vertical-align: bottom;
}
.info{
margin: 0 18px;
color: #acaaaa;
font-size: 14px;
}
.info .title{
color: #717171;
font-size: 18px;
margin: 13px 0 15px 0;
}
.info .category i{
margin-left: 4px;
margin-right: 7px;
}
.info .intro{
margin: 17px 4px;
line-height: 20px;
}
.star-wrapper{
height: 46px;
border-top: 1px solid #e9e9e9;
line-height: 46px;
color: #ddd;
padding: 0 16px;
}
.star-wrapper .light{
color: #b9cb41;
}
.star{
float: left;
}
.star-wrapper .weibo{
float: right;
}
</style>
</head>
<body>
<div class="outer">
<div class="img-wrapper">
<img src="img/dog.jpg" alt="">
</div>
<div class="info">
<h2 class="title">动画电影</h2>
<h3 class="category">
<i class="fas fa-map-marker-alt"></i>
动画
</h3>
<p class="intro">这是一部迪士尼动画电影,炒鸡炒鸡棒!!!</p>
</div>
<div class="star-wrapper">
<ul class="star">
<li class="fas fa-star light"></li>
<li class="fas fa-star light"></li>
<li class="fas fa-star"></li>
<li class="fas fa-star"></li>
</ul>
<ul class="weibo">
<li class="fab fa-weibo"></li>
</ul>
</div>
</div>
</body>
</html>
结果: