Short description of issue:
I am looking for a way to set font size based values (similar to setting some property to an em-value) in an element inside a shadow dom relative to the shadow host, regardless of font size of the parent element in the shadow dom. Something similar to how rem-values work, but with the shadow as root instead of the html root.
The reason for this is to be able to scale content like a widget (that is inserted with shadow dom) in one place. If properties that is based on font size can be scaled this way, a whole widget can be scaled in every context without having to set a lot of css values to make it fit at every location.
Long description of issue:
I am looking for a way to size text (font-size) based on a given element in the DOM, to be able to scale different parts of content inside that element based on this wrapping element (good for widgets and such). This is not only for the actual text, there is often good to base a lot of css values on font-size, so the elements visual layout scale with the size of the text (like padding, border-radius and shadows). If everything is based on em-values, this can get a little messy. If there is multiple levels of inheritance of font-sizes inside the element, and I want to enlarge the first level without changing the second, I would need to enlarge the em of the first level in the dom, while reducing on any second level element (that is calculated based on that first level) to make text of sub-elements remain the same size. This is less than optimal in many cases.
A nice solution is basing stuff on Root EM (rem), so changing one level of the dom does not affect the sub-elements. However, if I want to enlarge/reduce the size of all text inside this wrapping elements (in one place) without affecting the other elements on the same page, I would need to enlarge/reduce the rem-values of all font-sizes for elements inside that wrapping element.
I recently started looking into Web Components with Shadow DOM and templates. In template-tags, css is encapsulated which is great because the design of that box/widget/component can be styled on its own without having to think of the rest of the design, unwanted inherited values and such. Finally a good way to make independent components to build up a webpage. But there would be great if I could set a kind of template root font-size for the template itself. So if I set some element inside the template to be font-size 2rem (or any other similar unit), then the font-size of that element would be 2x the root font size of the template, regardless of the font size in the host element where that template is used, and regardless of the font-size of the root of the page (the html element).
When I tested with rem-values inside a template, it was still based on the page root (html tag) font-size. I have also tested vw-values but this is also based on the whole viewport, and not just the encapsulated area (the shadow root).
I have looked at lots of articles about Shadow DOM, but I have not been able to find any solution on this issue. Any suggestions?
What happens (simplified examples):
<html style="font-size: 16px">
<body style="font-size: 12px">
I am 12px large
<!-- em, based on the body in this case -->
<div style="font-size: 1.5em">
I am 18px large
<div style="font-size: 1.5em">
I am 27px large
</div>
</div>
<!-- rem, based on the styles of the html element -->
<div style="font-size: 1.5rem">
I am 24px large
<div style="font-size: 1.5rem">
I am 24px large
</div>
</div>
</body>
</html>
What I want:
<html style="font-size: 16px">
<body style="font-size: 12px">
I am 12px large
<div style="font-size: 1.5em">
I am 18px large
</div>
<div style="font-size: 1.5rem">
I am 24px large
</div>
<template> <!-- Simulates that it is inserted into the DOM by using shadow elements -->
<style>
:root{ /* Or something like that */
font-size: 20px; /* This is the simulated "template root fontsize" */
}
</style>
<div style="font-size: 1.5trem"> <!-- Based on the template root fontsize -->
I am 30px large
<div style="font-size: 2trem">
I am 40px large
</div>
</div>
</template>
</body>
</html>
This would give one place (the :root part of the example above) to control all relative font-sizes inside that component, and this can then be combined with non-relative font-sizes like px or other normal css values.