When a user changes the font size setting in a browser, 1rem
stops being 16px
. For instance, in Chrome “Very large” means 1rem == 24px
, or 150% of normal font size:
I want in this case all <img>
s to be proportionally up-scaled (i.e. 150%), because all my layout is rem
-based. Using JS, I could calc the factor:
const k = parseFloat(getComputedStyle(document.documentElement).fontSize) / 16;
and apply it for each <img>
. But if the user changes the size when my page is already loaded, the page layout will be broken.
Can this be done in pure CSS or, maybe, using attributes, so the browser up-scales the <img>
s automatically?
UPD If there is an event of changing the font size in a browser, I could handle it and set a CSS variable for up-scaling:
img.auto-scale
{
zoom: var(--font-size-factor);
}
2
Answers
Try wrapping your
IMG
in aDIV
that is sized inrem
:That doesn’t make sense. Why should an image increase in size, and decrease in quality, because the user asked for larger text? I’d advise against it.
However, if you did really want to do this just absolutely position an image inside an element that has it size based on rem.