ok, your dynamic text must've come from some place. In my case this looks like:
<div class="large" :data-contentlength="Math.floor(item.name.length/7)">[[ item.name ]]</div>
and my css classes:
.large[data-contentlength="1"]{ font-size: 1.2em; }
.large[data-contentlength="2"]{ font-size: 1.1em; }
.large[data-contentlength="3"]{ font-size: 1.0em; }
.large[data-contentlength="4"]{ font-size: 0.9em; }
.large[data-contentlength="5"]{ font-size: 0.8em; }
.large[data-contentlength="6"]{ font-size: 0.7em; }
.large[data-contentlength="7"]{ font-size: 0.6em; }
I also have classes for "non-large" text:
[data-length="1"]{ font-size: 1.00em; }
...
edit:
this becomes a bit easier when attr() is available in all browsers:
https://developer.mozilla.org/en-US/docs/Web/CSS/attr#browser_compatibility
also, this is could be more dynamic if css could divide 2 unit-values (e.g. px and ch), for now this must be done manually.
see here:
https://jsfiddle.net/qns0pov2/3/
create a 1ch cube and see how large it is in your target unit (in the fiddle its px), calculate the amount of characters per line and use that value to get the perfect font-size for each content length.
the fiddle also shows the problems with that approach: the average character width is less than 1ch
(which is based on 0
) but there are characters like M
which are larger (somewhere around 70%).
so if you wish to guarantee that the characters fit in the space adjust the fiddle such that: --ch-width: calc(8.109 * 1.7);
if you're more interested in the average case: --ch-width: calc(8.109 * 0.92);