响应式布局和media query语法
程序员文章站
2022-06-01 16:51:57
...
**语法:@media not|only 设备名称[and 设备名称] ***
解释说明:
not|only not:除了某个设备,only:仅匹配某个设备
[and 设备名称可出现多次,对多个设备匹配]
设备名称:screen 代表计算机屏幕
width:可调整的宽度
@media screen and (min-width:1000px){样式说明}
要想实现响应式布局,必须在head加上
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!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>
</head>
<style>
#container{
margin: auto;
width: 750px;
}
#container>div{
border: 1px solid red;
text-align: left;
box-sizing: border-box;
border-radius: 12px;
padding: 4px;
}
div#left{
width: 300px;
height: 260px;
float: left;
}
div#main{
width: 450px;
height: 260px;
float: left;
/* 清除右边的浮动 */
clear: right;
}
div#right{
width: 750px;
float: left;
}
/* 浏览器宽度大于1000px的样式 */
@media screen and (min-width:1000px){
#container{
margin: auto;
width: 960px;
}
#container>div{
border: 1px solid red;
text-align: left;
box-sizing: border-box;
border-radius: 12px;
padding: 4px;
}
div#left{
width: 240px;
height: 260px;
float: left;
}
div#main{
width: 460px;
height: 260px;
float: left;
/* 让左右两边都可以出现float元素 */
clear: none;
}
div#right{
width: 260px;
float: left;
height: 260px;
}
}
/* 浏览器宽度小于480px的样式 */
@media screen and (max-width:480px){
#container{
margin: auto;
width: 450px;
}
#container>div{
border: 1px solid red;
text-align: left;
box-sizing: border-box;
border-radius: 12px;
padding: 4px;
}
div#left{
width: 450px;
height: 150px;
float: left;
}
div#main{
width: 450px;
height: 260px;
float: left;
/* 让左右两边都不可以出现float元素 */
clear: both;
}
div#right{
width: 450px;
float: left;
height: 170px;
}
}
</style>
<body>
<div id="container">
<div id="left">
<h2>wwwwwwwwww</h2>
. . . 省略页面内容
</div>
<div id="main">
<h2>qqqqqqqqqqqqqqqq</h2>
... 省略页而内容
</div>
<div id="right">
<h2>eeeeeeeeeee</h2>
...省略页面内容
</div>
</div>
</body>
</html>
查看响应式布局方法:F12或鼠标右键->检查,调出调式框
1.