移動端字型大小自適應程式碼例項

admin發表於2017-02-20
分享一段程式碼例項,它實現了移動端字型大小自適應效果。

程式碼例項如下:

[HTML] 純文字檢視 複製程式碼
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1, user-scalable=no,">
<meta name="author" content="http://www.softwhy.com/" />
<title>螞蟻部落</title>  
<style type="text/css">
html, body {
  font-size: 10px;
  padding: 0;
  margin: 0;
}
@media screen and (min-width:321px) and (max-width:375px) {
  html {
    font-size: 10px;
  }
}
@media screen and (min-width:376px) and (max-width:414px) {
  html {
    font-size: 10px;
  }
}
@media screen and (min-width:415px) and (max-width:639px) {
  html {
    font-size: 12px;
  }
}
@media screen and (min-width:640px) and (max-width:719px) {
  html {
    font-size: 12px;
  }
}
@media screen and (min-width:720px) {
  html {
    font-size: 16px;
  }
}
@media screen and (min-width:750px) and (max-width:799px) {
  html {
    font-size: 23.5px;
  }
}
@media screen and (min-width:800px)and (max-width:1199px) {
  html {
    font-size: 25px;
  }
}
@media screen and (min-width:1200px) {
  html {
    font-size: 30px;
  }
}
div.box {
  font-size: 2rem;
  height: 10rem;
  background: black;
  color: white;
  padding: 1rem;
}
</style>
</head>
<body>
  <div class="box">
    調整視窗測試效果
    通過適配,然後控制rem的大小。達到字型或者盒子的自適應
  </div>
</body>
</html>

上面的程式碼實現了我們的要求,更多內容可以參閱相關閱讀。

相關閱讀:

(1).@media可以參閱css3 Media Queries媒體查詢一章節。

(2).rem可以參閱CSS3 rem單位一章節。

相關文章