1媒体查询
学习媒体查询的目的:
-
了解如何使用媒体查询做响应式页面。
-
为接下来学的 Bootstrap 做铺垫。
媒体查询会写如下代码即可:
body {
background-color: gray;
}
/* 大于等于768px 为粉色 */
@media (min-width: 768px) {
body {
background-color: pink;
}
}
/* 大于等于992px 为蓝色 */
@media (min-width: 992px) {
body {
background-color: skyblue;
}
}
/* 大于等于992px 为绿色 */
@media (min-width: 1200px) {
body {
background-color: green;
}
}
媒体查询使用场景1-
约束移动端不要超过规定大小
body {
max-width: 540px;
margin: 0 auto;
}
/* 大于等于540px 则不允许 html文字大小再改动,强制定为 54px*/
@media (min-width: 540px) {
html {
font-size: 54px !important;
}
}
媒体查询使用场景2-
元素的显示和隐藏
@media (max-width: 800px) {
.box div:nth-child(2) {
display: none;
}
.box div:last-child {
width: 350px;
}
}
媒体查询使用场景3
响应式原理:通过@media媒体标签判断屏幕大小, 让盒子在不同宽度呈现不同的摆放顺序
<!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>
.box {
width: 1000px;
height: 150px;
/* background-color: pink; */
margin: 0 auto;
display: flex;
flex-wrap: wrap;
}
.box div {
width: 25%;
background-color: skyblue;
heigh