{"id":18140846,"url":"https://github.com/gui/postcss-relative-rem","last_synced_at":"2026-01-23T20:40:44.951Z","repository":{"id":57328412,"uuid":"459711426","full_name":"GUI/postcss-relative-rem","owner":"GUI","description":"A postcss plugin to convert rem units to units relative a CSS variable (instead of the root element's font size).","archived":false,"fork":false,"pushed_at":"2024-06-18T19:37:50.000Z","size":61,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-02-03T01:12:03.152Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/GUI.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-02-15T18:59:09.000Z","updated_at":"2024-06-18T19:37:48.000Z","dependencies_parsed_at":"2022-09-21T01:41:40.305Z","dependency_job_id":null,"html_url":"https://github.com/GUI/postcss-relative-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/GUI%2Fpostcss-relative-rem","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GUI%2Fpostcss-relative-rem/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GUI%2Fpostcss-relative-rem/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GUI%2Fpostcss-relative-rem/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/GUI","download_url":"https://codeload.github.com/GUI/postcss-relative-rem/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239217105,"owners_count":19601593,"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":[],"created_at":"2024-11-01T16:07:06.825Z","updated_at":"2026-01-23T20:40:44.918Z","avatar_url":"https://github.com/GUI.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# postcss-relative-rem\n\nA [PostCSS](https://github.com/postcss/postcss) plugin to convert `rem` units to units relative a CSS variable (instead of the root element's font size).\n\nThis will convert something like this:\n\n```css\nh1 {\n  font-size: 2rem;\n  margin-bottom: 0.5rem;\n}\n```\n\nTo:\n\n```css\nh1 {\n  font-size: calc(2 * var(--rem-relative-base));\n  margin-bottom: calc(0.5 * var(--rem-relative-base));\n}\n```\n\n## Why?\n\nThe driver for this was to be able to use `rem` units inside [shadow DOM](https://developer.mozilla.org/en-US/docs/Web/Web_Components/Using_shadow_DOM) stylesheets. The shadow DOM mostly isolates a component's styles, but `rem` units still reference the `\u003chtml\u003e` root's font-size, making `rem` unit sizes unpredictable if your component is injected into pages that may have variable `\u003chtml\u003e` root font sizes defined that are outside of your control.\n\nBy changing the CSS so all `rem` units are relative to another CSS variable, this allows for better control over how to define your own root font-size. You can still use `rem` units in your variable definition, allowing for better accessibility than perhaps other alternatives (like converting everything to pixels).\n\n## Installation\n\n```sh\nnpm install --save-dev postcss postcss-relative-rem\n```\n\n## Usage\n\nAdd this plugin to your `postcss.config.js`:\n\n```js\nmodule.exports = {\n  plugins: [require(\"postcss-relative-rem\")],\n};\n```\n\nThe `--rem-relative-base` CSS variable must then also be defined in some way:\n\n```html\n\u003cstyle\u003e\n  :root {\n    --rem-relative-base: 1.6rem;\n  }\n\u003c/style\u003e\n```\n\n### Shadow DOM Example\n\nFor a more concrete example of how this plugin can be used to dynamically deal with a shadow DOM component being injected into an unknown page (with variable root font size definitions), here's how you could set the `--rem-relative-base` CSS variable based on the component's inherited font size (instead of the root `\u003chtml\u003e` font size):\n\n```js\n// Grab the container and setup the shadow DOM.\nconst containerEl = document.querySelector(\".my-shadow-dom-container\");\ncontainerEl.attachShadow({ mode: \"open\" });\n\n// Note: If the container will set its own font size or reset things, make sure\n// this style is in place before the following font size calculations (if this\n// is applied later by a stylesheet that hasn't yet loaded, then this may cause\n// the calculations to be incorrect).\ncontainerEl.style.setProperty(\"all\", \"initial\");\n\n// Determine the root `\u003chtml\u003e` font size.\nconst rootFontSize = parseFloat(\n  window\n    .getComputedStyle(document.documentElement)\n    .getPropertyValue(\"font-size\"),\n);\n\n// Determine the font size of the shadow DOM container.\nconst containerFontSize = parseFloat(\n  window.getComputedStyle(containerEl).getPropertyValue(\"font-size\"),\n);\n\n// Calculate the relative base font size (to be used by the calculations done\n// by this plugin) based on comparing the root font size to the contianer's\n// font size.\nconst remRelativeBaseSize = `${containerFontSize / rootFontSize}rem`;\n\n// Set the CSS variable on the container which will then be used by the calcs()\n// this plugin provides.\ncontainerEl.style.setProperty(\"--rem-relative-base\", remRelativeBaseSize);\n```\n\n## Options\n\n### `baseCssVariable`\n\nDefault: `--rem-relative-base`\n\nAllows you to set a different CSS variable name to use.\n\n```js\nmodule.exports = {\n  plugins: [\n    require(\"postcss-relative-rem\", {\n      baseCssVariable: \"--my-app-rem-base`,\n    }),\n  ],\n};\n```\n\n## References\n\n- https://discourse.wicg.io/t/hem-rem-for-web-component/2098\n- https://github.com/cuth/postcss-pxtorem\n- https://css-tricks.com/encapsulating-style-and-structure-with-shadow-dom/#aa-what-is-the-shadow-dom\n- https://stackoverflow.com/questions/66147461/change-shadow-dom-rem-size\n- https://stackoverflow.com/questions/24953647/font-size-css-values-based-on-shadow-dom-root\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgui%2Fpostcss-relative-rem","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgui%2Fpostcss-relative-rem","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgui%2Fpostcss-relative-rem/lists"}