But what if the container is not the viewport (body)?
This question is asked in a comment by Alex under 2507rkt3's answer.
That fact does not mean vw
cannot be used to some extent to size for that container. Now to see any variation at all one has to be assuming that the container in some way is flexible in size. Whether through a direct percentage width
or through being 100% minus margins. The point becomes "moot" if the container is always set to, let's say, 200px
wide--then just set a font-size
that works for that width.
Example 1
With a flexible width container, however, it must be realized that in some way the container is still being sized off the viewport. As such, it is a matter of adjusting a vw
setting based off that percentage size difference to the viewport, which means taking into account the sizing of parent wrappers. Take 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;
}
Assuming here the div
is a child of the body
, it is 50%
of that 100%
width, which is the viewport size in this basic case. Basically, you want to set a vw
that is going to look good to you. As you can see in my comment in the above CSS content, you can "think" through that mathematically with respect to the full viewport size, but you don't need to do that. The text is going to "flex" with the container because the container is flexing with the viewport resizing. Here's an example of two differently sized containers.
Example 2
You can help ensure viewport sizing by forcing the calculation based off that. 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;
}
The sizing is still based off viewport, but is in essence set up based off the container size itself.
Should the Size of the Container Change Dynamically...
If the sizing of the container element ended up changing dynamically its percentage relationship either via @media
breakpoints or via JavaScript, then whatever the base "target" was would need recalculation to maintain the same "relationship" for text sizing.
Take example #1 above. If the div
was switched to 25%
width by either @media
or JavaScript, then at the same time, the font-size
would need to adjust in either the media query or by JavaScript to the new calculation of 5vw * .25 = 1.25
. This would put the text size at the same size it would have been had the "width" of the original 50%
container been reduced by half from viewport sizing, but has now been reduced due to a change in its own percentage calculation.
A Challenge
With the CSS calc()
function in use, it would become difficult to adjust dynamically, as that function does not work for font-size
purposes at this time. So you could not do a pure CSS adjustment if your width is changing on calc()
. Of course, a minor adjustment of width for margins may not be enough to warrant any change in font-size
, so it may not matter.