/*dpi*/ /* for 1080+ px width screen */ /* for 1080 px width screen */ /* for 800 px width screen */ /* for 800 px width screen */ @media only screen and (min-width: 751px) { html, body { font-size: 31.25px; } } /* for 800 px width screen */ @media only screen and (max-width: 750px) { html, body { font-size: 31.25px; } } /* for 720 px width screen */ @media only screen and (max-width: 720px) { html, body { font-size: 30px; } } /* for 640 px width screen */ @media only screen and (max-width: 640px) { html, body { font-size: 27px; } } /* for 540 px width screen */ @media only screen and (max-width: 540px) { html, body { font-size: 22.5px; } } /* for 480 px width screen */ @media only screen and (max-width: 480px) { html, body { font-size: 20px; } } /* for 450 px width screen */ @media only screen and (max-width: 450px) { html, body { font-size: 18.9px; } } /* for 414 px width screen */ @media only screen and (max-width: 414px) { html, body { font-size: 17.25px; } } /* for 375 px width screen */ @media only screen and (max-width: 375px) { html, body { font-size: 15.625px; } } /* for 320 px width screen */ @media only screen and (max-width: 320px) { html, body { font-size: 13.5px; } }
特别是若是你须要设置设计响应式的页面,@media 是很是有用的。css
当你重置浏览器大小的过程当中,页面也会根据浏览器的宽度和高度从新渲染页面。html
其实看到这里对于这个概念仍是不明确,而且媒体查询中包含不少关键字,那咱们经常使用的有 and not only浏览器
only关键字防止老旧的浏览器不支持带媒体属性的查询而应用到给定的样式安全
and关键字用于合并多个媒体属性或合并媒体属性与媒体类型app
not关键字应用于整个媒体查询,在媒体查询为假时返回真iphone
举个简单的例子:布局
/媒体查询支持最大宽度为320px应用如下CSS html body 的fontsize设置为13.5px/
@media only screen and (max-width: 320px) { html, body { font-size: 13.5px; } }字体
只需在页面引入这段原生js代码就能够了flex
(function (doc, win) { var docEl = doc.documentElement, //文档根标签 resizeEvt = 'orientationchange' in window ? 'orientationchange' : 'resize', //viewport变化事,获取移动端屏幕是否翻转件源 recalc = function () { //重置方法 var clientWidth = docEl.clientWidth; if (!clientWidth) return; // 改变DOM根节点fontSize大小的值; // (屏幕宽度/设计图宽度) = 缩放或扩大的比例值; if(clientWidth>=640){ docEl.style.fontSize = '100px'; }else{ docEl.style.fontSize = 100 * (clientWidth / 640) + 'px'; } }; if (!doc.addEventListener) return; win.addEventListener(resizeEvt, recalc, false); doc.addEventListener('DOMContentLoaded', recalc, false); })(document, window);
如何使用?
这是rem布局的核心代码,这段代码的大意是:
若是页面的宽度超过了640px,那么页面中html的font-size恒为100px,不然,页面中html的font-size的大小为: 100 * (当前页面宽度 / 640)scala
为何是640px?
设计图通常是640px的,这样至关于100px = 1rem,能够方便计算;
由于是640px因此应限制下页面的大小,因此最外层的盒子应该是:
position: relative;
width: 100%;
max-width: 640px;
min-width: 320px;
margin: 0 auto;
'use strict'; /** * @param {Boolean} [normal = false] - 默认开启页面压缩以使页面高清; * @param {Number} [baseFontSize = 100] - 基础fontSize, 默认100px; * @param {Number} [fontscale = 1] - 有的业务但愿能放大必定比例的字体; */ const win = window; export default win.flex = (normal, baseFontSize, fontscale) => { const _baseFontSize = baseFontSize || 100; const _fontscale = fontscale || 1; const doc = win.document; const ua = navigator.userAgent; const matches = ua.match(/Android[\S\s]+AppleWebkit\/(\d{3})/i); const UCversion = ua.match(/U3\/((\d+|\.){5,})/i); const isUCHd = UCversion && parseInt(UCversion[1].split('.').join(''), 10) >= 80; const isIos = navigator.appVersion.match(/(iphone|ipad|ipod)/gi); let dpr = win.devicePixelRatio || 1; if (!isIos && !(matches && matches[1] > 534) && !isUCHd) { // 若是非iOS, 非Android4.3以上, 非UC内核, 就不执行高清, dpr设为1; dpr = 1; } const scale = normal ? 1 : 1 / dpr; let metaEl = doc.querySelector('meta[name="viewport"]'); if (!metaEl) { metaEl = doc.createElement('meta'); metaEl.setAttribute('name', 'viewport'); doc.head.appendChild(metaEl); } metaEl.setAttribute('content', `width=device-width,user-scalable=no,initial-scale=${scale},maximum-scale=${scale},minimum-scale=${scale}`); doc.documentElement.style.fontSize = normal ? '50px' : `${_baseFontSize / 2 * dpr * _fontscale}px`; }; <!-- 阿里高清方案 -->
<script>!function(e){function t(a){if(i[a])return i[a].exports;var n=i[a]={exports:{},id:a,loaded:!1};return e[a].call(n.exports,n,n.exports,t),n.loaded=!0,n.exports}var i={};return t.m=e,t.c=i,t.p="",t(0)}([function(e,t){"use strict";Object.defineProperty(t,"__esModule",{value:!0});var i=window;t["default"]=i.flex=function(e,t){var a=e||100,n=t||1,r=i.document,o=navigator.userAgent,d=o.match(/Android[Ss]+AppleWebkit/(d{3})/i),l=o.match(/U3/((d+|.){5,})/i),c=l&&parseInt(l[1].split(".").join(""),10)>=80,p=navigator.appVersion.match(/(iphone|ipad|ipod)/gi),s=i.devicePixelRatio||1;p||d&&d[1]>534||c||(s=1);var u=1/s,m=r.querySelector('meta[name="viewport"]');m||(m=r.createElement("meta"),m.setAttribute("name","viewport"),r.head.appendChild(m)),m.setAttribute("content","width=device-width,user-scalable=no,initial-scale="+u+",maximum-scale="+u+",minimum-scale="+u),r.documentElement.style.fontSize=a/2sn+"px"},e.exports=t["default"]}]);
flex(100, 1);</script>