1. Add meta, making web browsing normal phone side
html code
<!-- 设置缩放 -->
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no" />
<!-- 可隐藏地址栏,仅针对IOS的Safari(注:IOS7.0版本以后,safari上已看不到效果) -->
<meta name="apple-mobile-web-app-capable" content="yes" />
<!-- 仅针对IOS的Safari顶端状态条的样式(可选default/black/black-translucent ) -->
<meta name="apple-mobile-web-app-status-bar-style" content="black" />
<!-- IOS中禁用将数字识别为电话号码/忽略Android平台中对邮箱地址的识别 -->
<meta name="format-detection" content="telephone=no, email=no" />
复制代码
2. Consider using px, or use rem
For just a small part of the adaptation of mobile devices, and the resolution has little influence on the page, you can use px; if high accuracy is required to restore, fit a variety of mobile phones, it is best to use rem guys
1) Use px layout width used may be retractable percentage, fixed height pixel html code
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no, minimal-ui" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black" />
<meta name="format-detection" content="telephone=no, email=no" />
<title></title>
<style type="text/css">
body{
margin: 0;
padding: 0;
width: 320px;
height: 568px;
background:#fafafa;
border:1px solid #ccc;
}
.div1{
width: 40%;
height: 60px;
background: #FF0000;
float:left;
}
.div2{
width: 60%;
height: 60px;
background: #FF7E00;
float:left;
}
</style>
</head>
<body>
<div class="div1">40%</div>
<div class="div2">60%</div>
</body>
</html>
复制代码
2) Use rem, then there is a need for assistance in order to play high damage; choose one of two commonly used auxiliary in; a secondary use @media, according to automatically adjust the screen size can read this article details "CSS3 of REM set the font size "
Auxiliary Second, using js dynamic calculations, this is simply a very, very easy to use, just perfect to restore the draft design html code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="initial-scale=1, maximum-scale=1,minimum-scale=1, user-scalable=no">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<title>template</title>
<script>
//这个是小米官网的写法
! function(n) {
var e = n.document,
t = e.documentElement,
i = 720, //设计图尺寸
d = i / 100, //1rem = 100px
o = "orientationchange" in n ? "orientationchange" : "resize",
a = function() {
var n = t.clientWidth || 320;n > 720 && (n = 720);
t.style.fontSize = n / d + "px"
};
e.addEventListener && (n.addEventListener(o, a, !1), e.addEventListener("DOMContentLoaded", a, !1))
}(window);
</script>
<style>
*{box-sizing: border-box;}
body{margin: 0;padding: 0;font-size: 16px;}
.block{background: #1e90ff;width: 7.2rem;height: 2rem;}
.block2{background: #ef4437;width: 3.6rem;height: 3.6rem;}
</style>
</head>
<body>
<div class="wrap">
<div class="block">100% 7.2rem 设计图尺寸720,1rem=100px</div>
<div class="block2">50% 3.6rem</div>
</div>
</body>
</html>
复制代码
I commonly used adaptive.js
3. reset the page style, in the end pc or mobile terminal will be used
css code
/*css初始化*/
body,ol,ul,h1,h2,h3,h4,h5,h6,p,th,td,dl,dd,form,fieldset,legend,input,textarea,select{margin:0;padding:0}
body{font-size:16px;font-family: "微软雅黑","microsoft yahei","microsoft sans serif";background: #ededed;color: #313131;}
a,a:hover{text-decoration:none;color:inherit;}
em,i{font-style:normal}
li{list-style:none}
img{border:0;vertical-align:middle}
table{border-collapse:collapse;border-spacing:0}
p{word-wrap:break-word}
input,textarea{outline: none;font-family: "微软雅黑","microsoft yahei";}
*{box-sizing: border-box;} /*使用border-box盒模型使得计算位置、大小更方便*/
input[type='submit'],input[type='button'],input[type='reset']{-webkit-appearance: none;}/*消除iPhone上按钮显示怪异的情况*/
复制代码
4. The use of a screen layout
This is something I see in UI framework found, the entire page three points no more than one screen, header, contaner, footer. Content on the container, more than it overflow-y: scroll; this is actually very easy to use, making the page a clear structure, layout easy html code
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no, minimal-ui" />
<title></title>
<style type="text/css">
body,html{
position: absolute;
margin: 0;
padding: 0;
width: 320px;
height: 100%;
}
*{box-sizing: border-box;}
.g-page{
position: absolute;
top: 0;
width: 100%;
height: 100%;
background: #FAFAFA;
}
.g-header{
position: absolute;
top: 0;
width: 100%;
height: 40px;
line-height: 40px;
background: #EF4437;
color: #fff;
text-align: center;
z-index: 10;
}
.g-content{
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
overflow: hidden;
overflow-y: scroll;
-webkit-overflow-scrolling: touch;
background: #008000;
}
.g-footer{
position: absolute;
bottom: 0;
width: 100%;
height: 40px;
line-height: 40px;
background: #666;
color: #fff;
text-align: center;
}
.g-header ~ .g-content{
top: 40px;
}
.g-footer ~ .g-content{
bottom: 40px;
}
.div1{
height: 300px;
background: #909090;
}
.div2{
height: 300px;
background: #82615f;
}
.div3{
height: 300px;
background: #1e90ff;
}
</style>
</head>
<body>
<div class="g-page">
<div class="g-header">头部</div>
<div class="g-footer">页脚</div>
<div class="g-content">
<div class="div1">
内容
</div>
<div class="div2">
内容
</div>
<div class="div3">
内容
</div>
</div>
</div>
</body>
</html>
复制代码
5. fixed width left and right adaptive layout, we will often use
html code
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no, minimal-ui" />
<style>
body{margin: 0;padding: 0;}
*{box-sizing: border-box;}
.container-1{
display: flex;
height: 150px;
margin-bottom: 40px;
}
.container-1 .left{
width: 150px;
height: 100%;
background: #1E90FF;
}
.container-1 .right{
flex: 1;
height: 100%;
background: #ef4437;
}
.container-2{
position: relative;
width: 100%;
height: 150px;
margin-bottom: 40px;
}
.container-2 .left{
position: absolute;
width: 150px;
height: 100%;
background: #EF2322;
z-index: 2;
}
.container-2 .right{
position: absolute;
left: 0;
width: 100%;
height: 100%;
padding-left: 150px;
background: #1E90FF;
}
.container-3{
overflow: hidden;
height: 150px;
}
.container-3 .left{
float: left;
width: 150px;
height: 100%;
background: #1E90FF;
}
.container-3 .right{
/*width: 100%;*/
height: 100%;
padding-left: 150px;
background: #EF2322;
}
</style>
</head>
<body>
<div style="width: 800px;margin: 0 auto;">
<div class="container-1">
<div class="left"></div>
<div class="right"></div>
</div>
<div class="container-2">
<div class="left"></div>
<div class="right"></div>
</div>
<div class="container-3">
<div class="left">2222222222222</div>
<div class="right">1111111111111111111111111111111111111111111111<br>222222222222222</div>
</div>
</div>
</body>
</html>
复制代码
6. The modular, component-based development, named css prevent duplicate, to improve development efficiency
This is not for my English vocabulary, it is no gospel ah, I used to write css always afraid of the same name, written in a variety of strange names, efficiency is still low, since my mother no longer have to worry about with this "how to write an elegant css code "
7. pixel problem
Write a pixel border on the high score screen, that look just like the show is two pixels, designers are not satisfied, the boss is not satisfied; we can use to solve the pseudo-elements and css3 css code
.item{
width:2rem;
height:0.5rem;
}
.item:after{
content: " ";
position: absolute;
left: 0;
right: 0;
height: 1px;
z-index: 2;
bottom: 0;
border-bottom: 1px solid #D9D9D9;
-webkit-transform-origin: 0 100%;
transform-origin: 0 100%;
-webkit-transform: scaleY(.5);
transform: scaleY(.5);
}
复制代码
Reproduced in: https: //juejin.im/post/5d09d79ce51d4556bb4cd396