最简单的方式实现rem布局

加上如下js,px转换成rem需要手动,计算方式:量的大小除以100,就等于rem,例如:量的设计稿元素宽度是120,那么就写成{width: 1.2rem},这样写有什么问题,待研究,也欢迎补充

复制代码
<!DOCTYPE html>
<html lang="en" style="font-size:100px">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
    content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script>
        function resetFontSize() {
            var baseFontSize = 100
            var designWidth = 750
            var width = window.innerWidth
            var currentFontSize = (width / designWidth) * baseFontSize
            document.getElementsByTagName('html')[0].style.fontSize = currentFontSize + 'px'
        }
        window.onresize = function () {
            resetFontSize()
        };
        resetFontSize()
    </script>
    <style>
        *{
            padding:0;
            margin:0;
        }
        body{
            overflow: auto;
            font-size: .28rem;
        }
        .box{
            width:7.5rem;
            height: 1rem;
            background: #000;
        }
    
    </style>
</head>
<body>
    <div class="box"></div>
    <p>42341</p>


</body>
</html>
复制代码

 

posted @   zph前端  阅读(7854)  评论(0)    收藏  举报
编辑推荐:
· 理解 .NET 结构体字段的内存布局
· .NET 9中的异常处理性能提升分析:为什么过去慢,未来快
· 字符集、编码的前世今生
· Web性能优化:从 2 秒到200毫秒
· WPF 使用GDI+提取图片主色调并生成Mica材质特效背景
阅读排行:
· 垃圾qt,毁我青春
· DeepSeek为什么现在感觉不火了?
· 开发十年现状之我的工作经历
· .NET 9中的异常处理性能提升分析:为什么过去慢,未来快
· 一个老程序员, 两个小时能用corsur做出什么样的东西
点击右上角即可分享
微信分享提示