小tips:使用rem+vw实现简单的移动端适配
首先设置meta属性,如下代码:
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
使用如下代码就能实现移动端的适配:
html {
font-size: -webkit-calc(13.33333333vw);
font-size: calc(13.33333333vw);
}
100vw相当于浏览器的window.innerWidth,是浏览器的内部宽度,注意,滚动条宽度也计算在内!那么1vw就是表示1%的屏幕宽度。
其中的13.33333333vw是怎么来的呢?就是你的设计稿是750px,那么设计稿的1px就是0.133333333vw,那么100px就是13.33333333vw。也即是html的font-size设置为100px相当于1rem(设计稿为750px)。那么我们就可以很轻松的换算设计稿中的单位为rem了,比如一个元素宽度为150px,转换为rem就是1.5rem。其他尺寸设计稿的计算方式依次类推。
参考网易新闻移动端的写法:
/**
* view-port list:
320x480
320x568
320x570
360x592
360x598
360x604
360x640
360x720
375x667
375x812
393x699
412x732
414x736
480x854
540x960
640x360
720x1184
720x1280
800x600
1024x768
1080x1812
1080x1920
*/
html {
font-size: -webkit-calc(13.33333333vw);
font-size: calc(13.33333333vw);
}
@media screen and (max-width: 320px) {
html {
font-size: 42.667px;
font-size: -webkit-calc(13.33333333vw);
font-size: calc(13.33333333vw);
}
}
@media screen and (min-width: 321px) and (max-width: 360px) {
html {
font-size: 48px;
font-size: -webkit-calc(13.33333333vw);
font-size: calc(13.33333333vw);
}
}
@media screen and (min-width: 361px) and (max-width: 375px) {
html {
font-size: 50px;
font-size: -webkit-calc(13.33333333vw);
font-size: calc(13.33333333vw);
}
}
@media screen and (min-width: 376px) and (max-width: 393px) {
html {
font-size: 52.4px;
font-size: -webkit-calc(13.33333333vw);
font-size: calc(13.33333333vw);
}
}
@media screen and (min-width: 394px) and (max-width: 412px) {
html {
font-size: 54.93px;
font-size: -webkit-calc(13.33333333vw);
font-size: calc(13.33333333vw);
}
}
@media screen and (min-width: 413px) and (max-width: 414px) {
html {
font-size: 55.2px;
font-size: -webkit-calc(13.33333333vw);
font-size: calc(13.33333333vw);
}
}
@media screen and (min-width: 415px) and (max-width: 480px) {
html {
font-size: 64px;
font-size: -webkit-calc(13.33333333vw);
font-size: calc(13.33333333vw);
}
}
@media screen and (min-width: 481px) and (max-width: 540px) {
html {
font-size: 72px;
font-size: -webkit-calc(13.33333333vw);
font-size: calc(13.33333333vw);
}
}
@media screen and (min-width: 541px) and (max-width: 640px) {
html {
font-size: 85.33px;
font-size: -webkit-calc(13.33333333vw);
font-size: calc(13.33333333vw);
}
}
@media screen and (min-width: 641px) and (max-width: 720px) {
html {
font-size: 96px;
font-size: -webkit-calc(13.33333333vw);
font-size: calc(13.33333333vw);
}
}
@media screen and (min-width: 721px) and (max-width: 768px) {
html {
font-size: 102.4px;
font-size: -webkit-calc(13.33333333vw);
font-size: calc(13.33333333vw);
}
}
@media screen and (min-width: 769px) {
html {
font-size: 102.4px;
font-size: -webkit-calc(13.33333333vw);
font-size: calc(13.33333333vw);
}
}
body {
font-family: "PingFangSC-Regular", "Microsoft YaHei", Helvetica;
color: #333333;
background: #f5f7f9;
}
html a {
color: #333333;
}
这样写法也是为了兼容老版本手机不支持vw以及calc语法。
如果页面使用px单位,怎样进行rem的适配,把px转换为rem单位:
- postcss-pxtorem 是一款 postcss 插件,用于将单位转化为 rem
- lib-flexible 用于设置 rem 基准值
参考地址:
小tips:使用rem+vw实现简单的移动端适配的更多相关文章
- rem简单实现移动端适配
rem:移动web开发 默认字体大小是16px 在<html>中设置字体大小 与em的区别: em是在父级设置字体大小受影响 移动端适配 首先获取屏幕的宽度 计算当前屏幕宽度和640的比例 ...
- 小tips:node起一个简单服务,打开本地项目或文件浏览
1.安装nodejs 2.在项目文件夹目录下创建一个js文件,命名server.js(自定义名称),内容如下 var http = require('http'); var fs = require( ...
- 移动端适配 后篇(rem+vm)
涉及到的一些名词, 详细解释可参考 移动端适配前篇--移动端适配 rem 名词解释 [英寸Inch]英寸表示屏幕斜对角线的长度 [像素Pixel]像素是图像的基本采样单位,它不是一个确定的物理量,因为 ...
- 移动端适配-rem(新)
概念 对于移动端开发来说,无可避免的就是直面各种设备不同分辨率和不同DPR(设备像素比)的问题,在此忽略其他兼容性问题的探讨. 移动端像素 设备像素(dp),也叫物理像素.指设备能控制显示的最小物理单 ...
- 网页常见单位: px em pt % rem vw、vh、vmin、vmax , rem 使用
1.网页常见单位: px em pt vw\vh rem 1.1 px单位名称为像素,相对长度单位,像素(px)是相对于显示器屏幕分辨率而言 (最终解析单位) em单位名称为相对长度 ...
- 简单介绍移动端CSS3单位rem的用法
PC端大部份是用px单位,小部分用em单位,而移动端,请全部用rem单位吧.目前大部份设备,包括但不限于iOS 5+.Android 2.3+.Window Phone 8+都是可以兼容的,具体兼容表 ...
- Windows7驱动调试小Tips
v:* { } o:* { } w:* { } .shape { }p.MsoNormal,li.MsoNormal,div.MsoNormal { margin: 0cm; margin-botto ...
- 你不知道的JavaScript--Item17 循环与prototype最后的几点小tips
1.优先使用数组而不是Object类型来表示有顺序的集合 ECMAScript标准并没有规定对JavaScript的Object类型中的属性的存储顺序. 但是在使用for..in循环对Object中的 ...
- 整理一些《纸书科学计算器》的小Tips
本文最开始是在2016年的文章 Win10应用<纸书科学计算器>更新啦! 发表之后撰写的,当时那篇文章收到了不少人点赞,应用在国内市场的日下载量也突然上涨,让我感到受宠若惊,这里要感谢Wp ...
随机推荐
- BeetleX和Asp.net Core之webapi基础性能对比
本文主要针对BeetleX和Asp.net Core在基础WebApi功能性能对比 测试环境描述 硬件配置:E1230V2 16G内存 10Gb带宽 操作系统:Windows server 2008 ...
- Java进阶篇设计模式之八 ----- 责任链模式和命令模式
前言 在上一篇中我们学习了结构型模式的享元模式和代理模式.本篇则来学习下行为型模式的两个模式, 责任链模式(Chain of Responsibility Pattern)和命令模式(Command ...
- RabbitMQ死信队列另类用法之复合死信
前言 在业务开发过程中,我们常常需要做一些定时任务,这些任务一般用来做监控或者清理任务,比如在订单的业务场景中,用户在创建订单后一段时间内,没有完成支付,系统将自动取消该订单,并将库存返回到商品中,又 ...
- 腾讯视频国际版(Android)电量测试方法研究与总结
本文由云+社区发表 作者:腾讯移动品质中心TMQ 1.研究背景: 在2017年Google I/O大会上,Google发布了Google Play管理中心的新功能:Android vitals.当ap ...
- Changes of user relationship in AD can't be correctly synchronized to SCSM
The relationship of users might be not correctly updated if related users were once re-named in AD o ...
- springmvc重定向
String success="07大吉大利25"; @RequestMapping("str") public String test1(){ return ...
- php爬取微信文章内容
php爬取微信文章内容 在做官网升级的时遇到新的需求,需要将公司公众号文章显示在官网的文章模块下.但存在的问题是:微信文章的链接会失效,并且需要对文章部分内容做修改,同时要减少微信运营人员的工作量,避 ...
- 跨域405(Method Not Allowed)问题
zepot post没有问题,用plupload上传出现了这个错误,options过不去.显示Response for preflight has invalid http status code 4 ...
- spring集成shiro登陆流程(上)
上一篇已经分析了shiro的入口filter是SpringShiroFilter, 那么它的doFilter在哪儿呢? 我们看到它的直接父类AbstractShrioFilter继承了OncePerR ...
- Oracle和Mysql获取uuid的方法对比
场景:orm框架用mybatis,需要往数据库新增一条数据,用Oracle和mysql数据库分别需要怎么实现? mysql方法,用mysql提供的uuid函数 <insert id=" ...