I'm having a hard time understanding the font scaling issue.
I currently have a website whose body font-size
is 100%. 100% but what? This appears to be calculated at 16 pixels.
I was under the impression that 100% was somehow referring to the size of the browser window, but apparently not, since it is always 16 pixels regardless of whether the window is resized to mobile width or full-blown widescreen desktop.
How to make text on a website scale relative to its container? I tried using em
but that also doesn't scale.
My reasoning is that things like my menu get squished when you resize it, so I need to reduce the px
font of
.menuItem -size
and other elements related to the width of the container. (For example, in a menu on a large desktop, 22px
works well. Moving down to tablet width, 16px
is more appropriate.)
I know I could add breakpoints, but I really want the text to scale nicely as if I had the extra breakpoints, otherwise, for every 100 pixels reduced, I'd end up with hundreds Breakpoints control the width of the text.
This question was answered by Alex in 2507rkt3.
This fact does not mean that
vw
cannot be used to some extent to determine the size of this container. Now to see any changes we have to assume that the container is dimensionally flexible in some way. Either by direct percentagewidth
or by 100% minus margins. This becomes "meaningless" if the container is always set to, say, 200px wide - then just set thefont-size
to fit that width. p>Example 1
However, for containers with flexible widths, it is important to realize that the container still resizes to some extent based on the viewport. Therefore, the
vw
settings need to be adjusted based on the percentage size difference from the viewport, which means taking into account the size of the parent wrapper. Look at this example:Assume here that
div
is a child ofbody
, which is100%
of50%
width, in this basic case Below is the viewport size. Basically, you want to set up avw
that looks good to you. As you can see in the comments above in the CSS content, you can mathematically "think" the entire viewport size, but you don't need to. The text will "bend" with the container because the container bends as the viewport is resized. Here are examples of two different sized containers.Example 2
You can help ensure the viewport size by forcing calculations based on this. Consider this example:
Dimensions are still based on the viewport, but are essentially set based on the container dimensions themselves.
Should the size of the container change dynamically...
If the container element's size ends up changing its percentage relationship dynamically via @media breakpoints or JavaScript, then whatever the underlying "goal" is, it will need to be recalculated to maintain the same "relationship" used to resize the text.
Take example #1 above. If you switch the
div
width to a25%
width via@media
or JavaScript, then alsofont-size
needs to be in the media query or Adjusted via JavaScript to fit the new calculation5vw * .25 = 1.25
. This will make the text the same size it was when the original50%
container's "width" was reduced in half from the viewport size, but now reduced by its own percentage calculation due to the change.challenge
Using CSS
calc()
Function In use, dynamic adjustment becomes difficult, as this function currently cannot be used forfont-size
purposes. So if the width oncalc()
changes, pure CSS adjustments are not possible. Of course, a small adjustment to the margin width may not be enough to warrant any change tofont-size
, so it may not matter.Container queries is probably the best option now depending on your use case and is supported by all major browsers. Please check the support levels here: https://caniuse.com/mdn-css_properties_container
It’s easy to control now