最近在学习html5以及css3,在这里记录一下学到的一些新的属性,部分内容来自:http://www.w3school.com.cn

box-sizing盒子尺寸

box-sizing规定了盒子的计算方式,常用于消除hover时,盒子之间的影响,有三个属性:

1
box-sizing: content-box|border-box|inherit

content-box:默认值,表示盒子的尺寸为加上边框以及内边距之后的大小
border-box:规定了盒子的大小就是盒子的width以及height
inherit:表示盒子继承父元素box-sizing的值,示例代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
.box{
width: 200px;
height: 200px;
border: 20px solid red;
padding: 20px;
background-color: yellow;
/*此时盒子尺寸为宽度加高度加内边距加边框,为280x280*/
/*box-sizing: content-box;*/
/*此时盒子尺寸为width以及height的值,为200x200*/
box-sizing: border-box;
}
</style>
</head>
<body>
<div class="box"></div>
</body>
</html>

background-image多背景

background-image可以设置多背景,常用于把一张图切成几张图,当成背景,用于网站图片加载可以减轻网站的访问压力,用法如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<title>多背景</title>
<style>
.box{
width: 623px;
height: 417px;
background-repeat: no-repeat;
/*多张图片用逗号隔开*/
background-image: url(images/bg1.png),
url(images/bg2.png),
url(images/bg3.png),
url(images/bg4.png),
url(images/bg5.png);
background-position: left top,
right top,
right bottom,
left bottom,
center center;
}

</style>

</head>

<body>
<div class="box"></div>
</body>

</html>

background-size背景尺寸

background-size可以实现修改背景的尺寸

1
background-size: length|percentage|cover|contain;

length:表示可以用长度宽度设置背景的尺寸
percentage:表示可以用百分比设置背景的尺寸
以上两种一般是设置两个值,如果只设置一个值,则另一个值为自动
cover:保证图片填充满元素,不管图片是否完整
contain:保证图片完整显示,不保证是否填充满元素,示例代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
.box {
width: 500px;
height: 500px;
box-shadow: 0 0 1px #000;
background-image: url("./images/1.jpg");
background-repeat: no-repeat;
background-size: contain;
}
</style>
</head>
<body>
<div class="box"></div>
</body>
</html>

background-origin背景原点

background-origin规定了background-position定位的位置

1
background-origin: padding-box|border-box|content-box;

border-box:从边框开始定位
padding-box:从内边距开始定位
content-box:从盒子内容开始定位,示例代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
.box {
width: 400px;
height: 400px;
border: 20px solid rgba(255,0,0,0.5);
padding: 20px;
background-repeat: no-repeat;
background-image: url("./images/54.jpg");
background-origin: content-box;
}

</style>
</head>
<body>
<div class="box"></div>
</body>
</html>

background-clip背景裁切

background-clip控制背景显示多大的区域

1
background-clip: border-box|padding-box|content-box;

border-box:控制图片在border以内的区域也显示
padding-box:控制图片在padding以内的区域也显示
content-box:控制图片在内容以内的区域显示,示例代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
.box {
width: 363px;
height: 219px;
border: 20px solid rgba(255, 0, 0, 0.5);
padding: 20px;
background-repeat: no-repeat;
background-origin: border-box;
background-image: url("./images/54.jpg");
background-clip: content-box;
}
</style>
</head>

<body>
<div class="box"></div>
</body>
</html>

transition过渡

过渡: 就是给改变添加过程

1
transition: property duration timing-function delay;

transition-property:规定设置过渡效果的 CSS 属性的名称。
transition-duration:规定完成过渡效果需要多少秒或毫秒。
transition-timing-function:规定速度效果的速度曲线。
transition-delay:定义过渡效果何时开始。代码如下;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>过渡</title>
<style>
.box {
width: 20px;
height: 80px;
background-color: red;
/*简单写法*/
/*transition: all 1s;*/
/*专业写法*/
transition-property:width;
transition-duration: 1s;
transition-delay: 1s;
transition-timing-function: ease-in;
/* linear 线性 匀速运动 */
/* ease 先快后慢 */
/* ease-in-out 先慢 后快 最后慢 */
/* ease-out 越来越慢 */
/* ease-in 越来越快 */
}

.box:hover {
width: 800px;
background-color: green;
}
</style>
</head>
<body>
<div class="box"></div>
</body>
</html>

transform 2D转换

transform方法有很多,下面介绍几种最基础的:

1
2
3
4
5
transform: translate(x,y);位置偏移,参数为偏移的距离
transform: rotate(0deg);角度偏移,参数为旋转角度,正负对应顺逆时针
transform: scale(0.5);缩放转换,参数大于1则放大,小于1则缩小
transform-origin:left top;设置转换原点,参数可以是px或者百分比
transform: skew(x, y);参数分别问沿着x轴以及y轴倾斜的角度

示例代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
.box {
width: 200px;
height: 200px;
background-color: red;
transform:rotate(100deg);
}
.box2 {
width: 200px;
height: 200px;
background-color: red;
transform: translate(200px, 300px);
}
.box3 {
width: 200px;
height: 200px;
background-color: red;
transition: all 2s;
}
.box3:hover{
width: 200px;
height: 200px;
background-color: red;
transform: scale(0.5);
}
.box4{
position: absolute;
top: 50%;
left: 50%;
margin-left: -155px;
margin-top: -219px;
width: 310px;
height: 438px;
}
img{
width: 200px;
position: absolute;
bottom: 0;
left: 0;
box-shadow: 0 0 1px #f7f7f7;
transition: all 1s;
transform-origin: left top;
}
.box4:hover img:nth-child(1){
transform: rotate(60deg);
}
.box4:hover img:nth-child(2){
transform: rotate(120deg);
}
.box4:hover img:nth-child(3){
transform: rotate(180deg);
}
.box4:hover img:nth-child(4){
transform: rotate(240deg);
}
.box4:hover img:nth-child(5){
transform: rotate(300deg);
}
.box4:hover img:nth-child(6){
transform: rotate(360deg);
}
.box5{
width: 300px;
height: 300px;
background-color: yellow;
margin-left: 1100px;
transform: skew(152deg,60deg);
}
</style>
</head>
<body>
<div class="box"></div>
<div class="box2"></div>
<div class="box3"></div>
<div class="box4">
<img src="./images/pk1.png" alt="">
<img src="./images/pk1.png" alt="">
<img src="./images/pk1.png" alt="">
<img src="./images/pk1.png" alt="">
<img src="./images/pk1.png" alt="">
<img src="./images/pk1.png" alt="">
</div>
<div class="box5">
66666666666666666666666
</div>
</body>
</html>

动画

@keyframes:mymove 5s infinite 自定义动画名称, animation 将动画与 div 元素绑定, animation-iteration-count,控制动画的次数,infinite无数次, animation-delay 控制动画开始时间, animation-play-state 控制动画状态, paused(暂停) running(继续)
animation-direction: normal|alternate 控制动画播放是否循环, animation-fill-mode:forwards;控制动画结束时的状态 backwards(回到动画的初始状态) forwards(停留在动画的结束状态)
示例代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
.box {
position: relative;
width: 500px;
height: 500px;
border: 2px solid red;
margin: 50px auto;
}

i {
position: absolute;
top: -9px;
left: -9px;
width: 18px;
height: 18px;
background-color: chartreuse;
border-radius: 50%;
animation: move 3s;l

}

@keyframes move{
0% {
transform: translate(0px,0px );
}
100%{
transform: translate(500px,0px );
}
}
</style>
</head>

<body>
<div class="box">

<i></i>
</div>
</body>
</html>