Font scaling based on container size
P粉217784586
P粉217784586 2023-10-08 21:42:41
0
2
729

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.

P粉217784586
P粉217784586

reply all(2)
P粉346326040

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 the font-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:

div {
    width: 50%;
    border: 1px solid black;
    margin: 20px;
    font-size: 16px;
    /* 100 = viewport width, as 1vw = 1/100th of that
       So if the container is 50% of viewport (as here)
       then factor that into how you want it to size.
       Let's say you like 5vw if it were the whole width,
       then for this container, size it at 2.5vw (5 * .5 [i.e. 50%])
    */
    font-size: 2.5vw;
}

Assume here that div is a child of body, which is 100% of 50% width, in this basic case Below is the viewport size. Basically, you want to set up a vw 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:

html {width: 100%;} /* Force 'html' to be viewport width */
body {width: 150%; } /* Overflow the body */

div {
    width: 50%;
    border: 1px solid black;
    margin: 20px;
    font-size: 16px;
    /* 100 = viewport width, as 1vw = 1/100th of that
       Here, the body is 150% of viewport, but the container is 50%
       of viewport, so both parents factor  into how you want it to size.
       Let's say you like 5vw if it were the whole width,
       then for this container, size it at 3.75vw
       (5 * 1.5 [i.e. 150%]) * .5 [i.e. 50%]
    */
    font-size: 3.75vw;
}

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 a 25% width via @media or JavaScript, then also font-size needs to be in the media query or Adjusted via JavaScript to fit the new calculation 5vw * .25 = 1.25. This will make the text the same size it was when the original 50% 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 for font-size purposes. So if the width on calc() changes, pure CSS adjustments are not possible. Of course, a small adjustment to the margin width may not be enough to warrant any change to font-size, so it may not matter.

P粉957723124

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

.module h2 {
  font-size: 1em;
  container-name: sidebar
}

@container sidebar (min-width: 700px) {
  .module h2 {
    font-size: 2em;
  }
}
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template