{"id":21182301,"url":"https://github.com/tangxiangmin/rem","last_synced_at":"2025-03-14T19:41:58.410Z","repository":{"id":79098621,"uuid":"118714533","full_name":"tangxiangmin/rem","owner":"tangxiangmin","description":"simple rem layout by css","archived":false,"fork":false,"pushed_at":"2018-01-27T06:29:08.000Z","size":3,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-01-21T12:28:09.254Z","etag":null,"topics":["css","rem"],"latest_commit_sha":null,"homepage":null,"language":"CSS","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/tangxiangmin.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-01-24T05:01:26.000Z","updated_at":"2018-01-24T05:13:01.000Z","dependencies_parsed_at":null,"dependency_job_id":"3ff0aeb4-7f21-4702-b304-50d9e9869f30","html_url":"https://github.com/tangxiangmin/rem","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tangxiangmin%2Frem","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tangxiangmin%2Frem/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tangxiangmin%2Frem/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tangxiangmin%2Frem/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tangxiangmin","download_url":"https://codeload.github.com/tangxiangmin/rem/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243638841,"owners_count":20323503,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["css","rem"],"created_at":"2024-11-20T17:56:26.742Z","updated_at":"2025-03-14T19:41:58.404Z","avatar_url":"https://github.com/tangxiangmin.png","language":"CSS","funding_links":[],"categories":[],"sub_categories":[],"readme":"rem布局\n===\n\n在移动端项目中使用rem布局已经由很长一段时间了，现在整理一下。\n\n## CSS实现\n\n```scss\n// rem.scss\nhtml {\n    font-size: -webkit-calc((100vw / 750) * 100);\n    font-size: calc((100vw / 750) * 100);\n}\n\n@media screen and (min-width:800px) {\n    html {\n        font-size: 100px;\n    }\n    body {\n        width: 800px;\n        margin: 0 auto;\n    }\n}\n\n@function rem($px) {\n    @if (unitless($px)) {\n        @return 1rem * ($px/100);\n    } @else {\n        @return 1rem * ($px/100px);\n    }\n}\n```\n\n### 实现方式\n\n通过`calc`函数计算根字体大小，然后所有布局尺寸均使用rem单位即可\n\n为了方便编写，定义了一个SCSS函数，用于将设计图尺寸转换为rem。在小程序中，可以通过将`rem`单位修改为`rpx`，实现样式表的无缝迁移~\n\n样式表中只需要编写`width:rem(200)`即可，其中，`200`为750标准设计图的标注尺寸。这样基本可以完美还原设计图\n\n### PC上浏览移动站 \n\n为了避免在PC站打开浏览器导致字体过大，处理方式是进行媒介查询，限定最大的rem值，这样做的缺点是`fixed`的元素可能会溢出容器。\n\n之前张鑫旭大神在博客中提出了一个更合理的[实践方案](http://www.zhangxinxu.com/wordpress/2016/08/vw-viewport-responsive-layout-typography/)。\n\n```css\nhtml {\n    font-size: 16px;\n}\n\n@media screen and (min-width: 375px) {\n    html {\n        /* iPhone6的375px尺寸作为16px基准，414px正好18px大小, 600 20px */\n        font-size: calc(100% + 2 * (100vw - 375px) / 39);\n        font-size: calc(16px + 2 * (100vw - 375px) / 39);\n    }\n}\n@media screen and (min-width: 414px) {\n    html {\n        /* 414px-1000px每100像素宽字体增加1px(18px-22px) */\n        font-size: calc(112.5% + 4 * (100vw - 414px) / 586);\n        font-size: calc(18px + 4 * (100vw - 414px) / 586);\n    }\n}\n@media screen and (min-width: 600px) {\n    html {\n        /* 600px-1000px每100像素宽字体增加1px(20px-24px) */\n        font-size: calc(125% + 4 * (100vw - 600px) / 400);\n        font-size: calc(20px + 4 * (100vw - 600px) / 400);\n    }\n}\n@media screen and (min-width: 1000px) {\n    html {\n        /* 1000px往后是每100像素0.5px增加 */\n        font-size: calc(137.5% + 6 * (100vw - 1000px) / 1000);\n        font-size: calc(22px + 6 * (100vw - 1000px) / 1000);\n    }\n}\n```\n\n这里的缺点在于：尺寸并不是完全按照设计图的比例还原，如果设计师那边没问题，这种处理方式更优。\n\n## JavaScript实现\n\n上面的实现完全由CSS控制，不需要根据JavaScript来动态计算根字体的大小。但是仍旧需要考虑calc兼容性的问题。这里是关于calc的[兼容列表](https://caniuse.com/#search=calc)，可以看见基本满足生产条件了。\n\n在需要适配某些不支持calc特性的版本下（比如在Android `V4.4.4`以下版本不支持乘法和除法计算），可以使用JavaScript向下兼容（虽然很不情愿这么干~）\n\n```\nvar newRem = function() {\n    var html = document.documentElement\n    // 设计图标准750px\n    var baseWidth = 750\n    // 屏幕最大宽度800px\n    var maxScreenWitdh = 800\n    var screenWidth = Math.min(maxScreenWitdh, html.getBoundingClientRect().width)\n    html.style.fontSize = screenWidth / 750 * 100 + 'px'\n}\n\nwindow.addEventListener('resize', newRem, false)\nwindow.addEventListener('load', newRem)\n```\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftangxiangmin%2Frem","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftangxiangmin%2Frem","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftangxiangmin%2Frem/lists"}