Detailed explanation of HTML5 page rem layout adaptation method
This article mainly introduces the detailed explanation of the HTML5 page rem layout adaptation method. The editor thinks it is quite good. Now I will share it with you and give you a reference. Let’s follow the editor to take a look, I hope it can help everyone.
rem layout adaptation scheme
The main method is:
Dynamic calculation based on the ratio of the design draft to the width of the device And set the font-size size of the html root tag; in
#css, the width, height, relative position, etc. of the design draft elements are converted into values in rem units according to the same proportion. ;
The font in the design draft uses px as the unit and is slightly adjusted through media queries.
1 Dynamically set html tag font-size size
Simplified universal version:
##
!(function(win, doc){ function setFontSize() { // 获取window 宽度 // zepto实现 $(window).width()就是这么干的 var winWidth = window.innerWidth; // doc.documentElement.style.fontSize = (winWidth / 640) * 100 + 'px' ; // 640宽度以上进行限制 需要css进行配合 var size = (winWidth / 640) * 100; doc.documentElement.style.fontSize = (size < 100 ? size : 100) + 'px' ; } var evt = 'onorientationchange' in win ? 'orientationchange' : 'resize'; var timer = null; win.addEventListener(evt, function () { clearTimeout(timer); timer = setTimeout(setFontSize, 300); }, false); win.addEventListener("pageshow", function(e) { if (e.persisted) { clearTimeout(timer); timer = setTimeout(setFontSize, 300); } }, false); // 初始化 setFontSize(); }(window, document));
(function(WIN) { var setFontSize = WIN.setFontSize = function (_width) { var docEl = document.documentElement; // 获取当前窗口的宽度 var width = _width || docEl.clientWidth; // docEl.getBoundingClientRect().width; // 大于 1080px 按 1080 if (width > 1080) { width = 1080; } var rem = width / 10; console.log(rem); docEl.style.fontSize = rem + 'px'; // 误差、兼容性处理 var actualSize = win.getComputedStyle && parseFloat(win.getComputedStyle(docEl)["font-size"]); if (actualSize !== rem && actualSize > 0 && Math.abs(actualSize - rem) > 1) { var remScaled = rem * rem / actualSize; docEl.style.fontSize = remScaled + 'px'; } } var timer; function dbcRefresh() { clearTimeout(timer); timer = setTimeout(setFontSize, 100); } //窗口更新动态改变 font-size WIN.addEventListener('resize', dbcRefresh, false); //页面显示时计算一次 WIN.addEventListener('pageshow', function(e) { if (e.persisted) { dbcRefresh() } }, false); setFontSize(); })(window) //对H5活动推过页面,宽高比例有所要求,可适当调整 function adjustWarp(warpId = '#warp') { const $win = $(window); const height = $win.height(); let width = $win.width(); // 考虑导航栏情况 if (width / height < 360 / 600) { return; } width = Math.ceil(height * 360 / 640); $(warpId).css({ height, width, postion: 'relative', top: 0, left: 'auto', margin: '0 auto' }); // 重新计算 rem window.setFontSize(width); }
2 Set rem through CSS3 media query
Easy to use, lack of flexibility, please see demo you know@media screen and ( min-width: 320px){html{font-size:50px}} @media screen and ( min-width: 360px){html{font-size:56.25px}} @media screen and ( min-width: 375px){html{font-size:58.59375px}} @media screen and ( min-width: 384px){html{font-size:60px}} @media screen and ( min-width: 400px){html{font-size:62.5px}} @media screen and ( min-width: 414px){html{font-size:64.6875px}} @media screen and ( min-width: 424px){html{font-size:66.25px}} @media screen and ( min-width: 480px){html{font-size:75px}} @media screen and ( min-width: 540px){html{font-size:84.375px}} @media screen and ( min-width: 640px){html{font-size:100px}}
javascript uses rem to do responsive development example sharing
Two mobile terminal rem layout implementations Method
Example to explain js implementation of rem automatic matching calculation font-size
The above is the detailed content of Detailed explanation of HTML5 page rem layout adaptation method. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Guide to Table Border in HTML. Here we discuss multiple ways for defining table-border with examples of the Table Border in HTML.

Guide to HTML margin-left. Here we discuss a brief overview on HTML margin-left and its Examples along with its Code Implementation.

This is a guide to Nested Table in HTML. Here we discuss how to create a table within the table along with the respective examples.

Guide to HTML Table Layout. Here we discuss the Values of HTML Table Layout along with the examples and outputs n detail.

Guide to the HTML Ordered List. Here we also discuss introduction of HTML Ordered list and types along with their example respectively

Guide to HTML Input Placeholder. Here we discuss the Examples of HTML Input Placeholder along with the codes and outputs.

Guide to Moving Text in HTML. Here we discuss an introduction, how marquee tag work with syntax and examples to implement.

Guide to HTML onclick Button. Here we discuss their introduction, working, examples and onclick Event in various events respectively.
