-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path4-百分比padding.html
46 lines (46 loc) · 1.19 KB
/
4-百分比padding.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.box {
width: 300px;
height: 500px;
background-color: #999;
}
.inner {
/* 无论父元素宽度多少,这句保证了子元素宽度为父元素宽度 ,且宽高比为1:1*/
padding-bottom: 100%;
background-color: pink;
}
.banner {
/* 父元素撑起固定宽高比,图片变为弹性布局
15.15% = 1/8
*/
padding: 15.15% 0 0;
position: relative;
}
.banner > img {
position: absolute;
width: 100%;
height: 100%;
left: 0;
top: 0;
}
</style>
</head>
<body>
<h2>demo1</h2>
<div class="box">
<div class="inner">
padding的百分比值是相对于父元素的宽度计算的!
</div>
</div>
<h2>demo2</h2>
<div class="banner">
<img src="https://www.baidu.com/img/bd_logo1.png" alt="">
</div>
</body>
</html>