Change shadow dom rem size

I am using a shadow DOM for CSS isolation. I want the rem font size to use the HTML element font size in the shadow DOM.

The font in <div class="shadow-div">shadow DOM font (should be 100px)</div> should be 100px but the rem size is still 16px.

Here is a small code snippet demonstrating what I want to do.

  html {
    font-size: 16px;
<div>light dom font</div>

<div id="root"></div>

  const root = document.getElementById('root');
  root.attachShadow({ mode: 'open' });

  const html = document.createElement('html')
  html.innerHTML = `
                    html {
                        font-size: 100px;

                    .shadow-div {
                        font-size: 1rem;
                <div class="shadow-div">shadow dom font (should be 100px)</div>



shadowRoots are not Documents, so don’t have <html><head><body> markup;
shadowRoots are more like DocumentFragments

You style shadow content based from the :host selector; and since shadow DOM is encapsulated, there is less need for classes to target that one DIV

REMs are always based on the html definition; use em instead inside the Component

Also note how inheritable properties, like color ‘trickle down’ into Components and do style content inside shadowDOM (font-size also, but you overload it in the Component in this code)

That simplifies your code to:

  html { font-size: 10px }
  body { font-size: 3rem; color:red }

<div>3rem body font-size = 3 * html font-size</div>
<div id="root"></div>

    .attachShadow({ mode: 'open' })
    .innerHTML = `<style>
                    :host { font-size: 50px }
                    div   { font-size: 2rem }
                    p     { font-size: .5em }
                 50px Component font-size
                 <div>2rem = 2 * html font-size = 20px</div>
                 <p>.5em = .5 * :host font-size = 25px</p>`;
Source: stackoverflow
The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .