媒体查询 响应式开发

媒体查询:响应式开发

第一种不同终端的宽度自适应代码:

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
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style>
.container {
width: 1200px;
height: 1000px;
margin: 0 auto;
background-color: red;
}
@media screen and (min-width:1200px){
body{
/*当屏幕大小最小满足1200px即大于1200px的时候 */
/*background-color: pink;*/
}
.container {
width: 1170px;
background-color: pink;
}
}
@media screen and (min-width:992px) and (max-width: 1200px){
.container {
width: 970px;
background: green;
}
}
@media screen and (min-width: 768px) and (max-width: 992px){
.container {
width: 750px;
background-color: blue;
}
}
@media screen and (max-width: 768px){
.container {
width: 100%;
background-color: orange;
}
}
</style>
</head>
<body>
<div class="container"></div>
</body>
</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
47
48
49
50
51
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style>
html,body {
margin: 0 ;
padding: 0;
}
.container {
width: 1200px;
height: 1000px;
margin: 0 auto;
background-color: red;
}
/*以覆盖的方式存在*/
@media (max-width: 768px){
.container {
width: 100%;
background-color: orange;
}
}
@media (min-width: 768px) {
.container {
width: 750px;
background-color: blue;
}
}
@media (min-width:992px) {
.container {
width: 970px;
background: green;
}
}
@media (min-width:1200px){
/*当屏幕大小最小满足1200px即大于1200px的时候 */
/*background-color: pink;*/
.container {
width: 1170px;
background-color: pink;
}
}
</style>
</head>
<body>
<div class="container"></div>
</body>
</html>