参考:<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>FengHuang</title> <meta name="viewport" content="initial-scale=1,maximum-scale=1,minimum-scale=1,user-scalable=no,width=device-width,viewport-fit=cover"> <style> body, html { max-width: 100%; } html { background-color: #FFFFFF; padding: 0; } body { background-color: #CCCCCC; margin: 0; padding: 0; } #info { width: 100%; box-sizing: border-box; padding: 20px 10px; background-color: #999999; color: #FFFFFF; font-size: 12px; line-height: 1.5; text-align: center; } html,body {margin: 0 auto;} #blocks { color: #ffffff; line-height: 1rem; vertical-align: middle; text-align: center; } .full-block { width: 10rem; height: 1rem; background-color: blue; font-size: 0.5rem; margin-bottom: 2px; white-space: nowrap; } .half-block { width: 5rem; height: 1rem; background-color: green; font-size: 0.25rem; margin-bottom: 2px; white-space: nowrap; } </style> </head> <body> <div id="info"></div> <div id="blocks"> <div class="full-block">W:10rem, H: 1rem, F: 0.5rem</div> <div class="full-block" style="font-size: 18.75px;">W:10rem, H: 1rem, F: 18.75px</div> <div class="half-block">W:5rem, H: 1rem, F: 0.25rem</div> <div class="half-block" style="font-size: 9.375px;">W:5rem, H: 1rem, F: 9.375px</div> </div> </body> <script> function showWidthInfo() { const infoDom = document.getElementById('info'); const htmlStr = ` window.devicePixelRatio: ${window.devicePixelRatio}<br/> visualViewport.scale: ${visualViewport.scale}<br/> document.documentElement.clientWidth: ${document.documentElement.clientWidth}<br/> document.documentElement.offsetWidth: ${document.documentElement.offsetWidth}<br/> document.documentElement.scrollWidth: ${document.documentElement.scrollWidth}<br/> window.outerWidth: ${window.outerWidth}<br/> window.innerWidth: ${window.innerWidth}<br/> window.screen.width: ${window.screen.width}<br/> document.documentElement.clientHeight: ${document.documentElement.clientHeight}<br/> document.documentElement.offsetHeight: ${document.documentElement.offsetHeight}<br/> document.documentElement.scrollHeight: ${document.documentElement.scrollHeight}<br/> document.documentElement.style.fontSize: ${document.documentElement.style.fontSize}<br/> `; infoDom.innerHTML = htmlStr; } function rem() { function resize() { var width = window.innerWidth; var rem = width / 10; document.documentElement.style.fontSize = rem + 'px'; showWidthInfo() } resize() window.addEventListener('resize', resize) } document.addEventListener('DOMContentLoaded', function () { showWidthInfo(); rem(); }) </script> </html> <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>FengHuang</title> <meta name="viewport" content="initial-scale=1,maximum-scale=1,minimum-scale=1,user-scalable=no,width=device-width,viewport-fit=cover"> <style> body, html { max-width: 100%; } html { background-color: #FFFFFF; padding: 0; } body { background-color: #CCCCCC; margin: 0; padding: 0; } #info { width: 100%; box-sizing: border-box; padding: 20px 10px; background-color: #999999; color: #FFFFFF; font-size: 12px; line-height: 1.5; text-align: center; } html,body {margin: 0 auto;} #blocks { color: #ffffff; line-height: 1rem; vertical-align: middle; text-align: center; } .full-block { width: 10rem; height: 1rem; background-color: blue; font-size: 0.5rem; margin-bottom: 2px; white-space: nowrap; } .half-block { width: 5rem; height: 1rem; background-color: green; font-size: 0.25rem; margin-bottom: 2px; white-space: nowrap; } </style> </head> <body> <div id="info"></div> <div id="blocks"> <div class="full-block">W:10rem, H: 1rem, F: 0.5rem</div> <div class="full-block" style="font-size: 18.75px;">W:10rem, H: 1rem, F: 18.75px</div> <div class="half-block">W:5rem, H: 1rem, F: 0.25rem</div> <div class="half-block" style="font-size: 9.375px;">W:5rem, H: 1rem, F: 9.375px</div> </div> </body> <script> function showWidthInfo() { const infoDom = document.getElementById('info'); const htmlStr = ` window.devicePixelRatio: ${window.devicePixelRatio}<br/> visualViewport.scale: ${visualViewport.scale}<br/> document.documentElement.clientWidth: ${document.documentElement.clientWidth}<br/> document.documentElement.offsetWidth: ${document.documentElement.offsetWidth}<br/> document.documentElement.scrollWidth: ${document.documentElement.scrollWidth}<br/> window.outerWidth: ${window.outerWidth}<br/> window.innerWidth: ${window.innerWidth}<br/> window.screen.width: ${window.screen.width}<br/> document.documentElement.clientHeight: ${document.documentElement.clientHeight}<br/> document.documentElement.offsetHeight: ${document.documentElement.offsetHeight}<br/> document.documentElement.scrollHeight: ${document.documentElement.scrollHeight}<br/> document.documentElement.style.fontSize: ${document.documentElement.style.fontSize}<br/> `; infoDom.innerHTML = htmlStr; } function rem() { function resize() { var width = window.innerWidth; var rem = width / 10; document.documentElement.style.fontSize = rem + 'px'; showWidthInfo() } resize() window.addEventListener('resize', resize) } document.addEventListener('DOMContentLoaded', function () { showWidthInfo(); rem(); }) </script> </html>
参考: