媒体查询的方式可以说是我早期采用的布局方式, 它主要是通过查询设备的宽度来执行不同的 css 代码,最终达到界面的适配。
媒体查询、伸缩布局、Bootstrap 来实现响应式站点如下我将会给出一个示例布局代码,就是来演示一下移动端常用的布局方式当中的通过媒体查询实现的方式:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>移动端常用适配方案一</title>
<style>
* {
margin: 0;
padding: 0;
}
.top {
position: relative;
}
.top > img {
width: 100%;
height: auto;
}
.top > p {
font-size: 16px;
color: #fff;
position: absolute;
left: 50%;
transform: translateX(-50%);
top: 33px;
}
.middle, .bottom {
position: relative;
height: 124px;
}
.main {
border: 1px dashed #0d7efb;
border-radius: 5px;
padding: 4px;
display: inline-block;
position: absolute;
left: 50%;
transform: translateX(-50%);
}
.main > img:nth-of-type(1) {
width: 175px;
height: 116px;
vertical-align: bottom;
}
.main > img:nth-of-type(2) {
width: 35px;
height: 35px;
position: absolute;
left: 50%;
transform: translateX(-50%);
top: 25px;
}
.bottom {
margin-top: 14px;
}
/*
iPhone 6/7/8
*/
@media screen and (min-width: 375px) {
.top > p {
font-size: 18px;
top: 40px;
}
.middle, .bottom {
height: 143px;
}
.main > img:nth-of-type(1) {
width: 206px;
height: 135px;
}
.main > img:nth-of-type(2) {
width: 42px;
height: 42px;
top: 30px;
}
.bottom {
margin-top: 17px;
}
}
/*
iphone 6/7/8Plus
*/
@media screen and (min-width: 414px) {
.top > p {
font-size: 20px;
top: 43px;
}
.middle, .bottom {
height: 160px;
padding: 5px;
}
.main > img:nth-of-type(1) {
width: 227px;
height: 150px;
}
.main > img:nth-of-type(2) {
width: 45px;
height: 45px;
top: 35px;
}
.bottom {
margin-top: 19px;
}
}
</style>
</head>
<body>
<div class="top">
<img src="images/bg.png" alt="">
<p>实名认证</p>
</div>
<div class="middle">
<div class="main">
<img src="images/back.png" alt="">
<img src="images/add.png" alt="">
</div>
</div>
<div class="bottom">
<div class="main">
<img src="images/back.png" alt="">
<img src="images/add.png" alt="">
</div>
</div>
</body>
</html>如上代码当中的素材以及测试效果博主这里就只是提供对应的素材文件,至于测试效果自行测试验证即可,其实通过媒体查询的方式进行实现在这里博主是不怎么推荐的,就因如上它所包含的那些劣势,就足以不推荐所以说,在下一篇文章当中我会在介绍另外一种,就是 PC 端一套代码,移动端一套代码的方式。
素材下载地址:https://wwe.lanzouo.com/ikJioxjcl0f
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。