viewport
写移动端页面时需要加的属性
<meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=yes,maximum-scale=2">
一般写为<meta name="viewport" content="width=device-width, initial-scale=1">
即可
width=device-width 设置布局视口的宽度为设备屏幕宽度
initial-scale=[number] 设置初始的缩放比率
user-scalable=yes | no 默认 yes 是否允许用户缩放
maximum-scale=[number] 允许最大放大比率
minimum-scale=[number] 允许最小缩小比率
设定为1 同 user-scalable=no
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<!-- <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=yes,maximum-scale=2"> -->
<meta name="viewport" content="width=device-width">
<title></title>
<style media="screen">
/*
width=device-width 设置布局视口的宽度为设备屏幕宽度
initial-scale=[number] 设置初始的缩放比率
user-scalable=yes | no 默认 yes 是否允许用户缩放
maximum-scale=[number] 允许最大放大比率
minimum-scale=[number] 允许最小缩小比率
设定为1 同 user-scalable=no
*/
*{margin: 0;padding: 0;}
div{
height:80px;
background: red;
margin-top: 15px;
color: white;
/*font-size: 40px;*/
text-align: right;
}
.box1{
width:100px;
}
.box2{
width: 414px;
}
.box3{
width: 50%;
}
.box4{
width: 100%;
}
</style>
</head>
<body>
<div class="box1">
BOX1
</div>
<div class="box2">
BOX2
</div>
<div class="box3">
BOX3
</div>
<div class="box4">
BOX4
</div>
</body>
<script type="text/javascript">
console.log(1,window.devicePixelRatio);
</script>
</html>
媒体查询
响应式布局所依赖的样式
@media screen and (max-width:[num]px){}
@media screen and (max-width:600px) and (min-width:400px){}