My solution creates a CSS variable that expresses the height of the container relative to the viewport, in "vh" units, this variable can then be used with the CSS3 "calc" function to calculate font height as a percentage of the height of the container.
the size of the container is measured every time the viewport (window) is resized
<html>
<head>
<style>
.container {
width:100%;
/*
any rules you like to set the dimensions of the container
*/
top:40px;
height:30vh;
border:1px solid red;
white-space:nowrap;
}
</style>
<script>
function setCSSVariableAccordingToElementHeightRelativeToViewPort(elementClassName, cssVariableName, immutableElement)
{
var element
/*
the "immutableElement" parameter is
true when the container is never recreated,
false if its generated dynamicaly
*/
if(immutableElement === true) {
element = document.querySelector("." + elementClassName)
}
var onResize = function() {
if(immutableElement !== true) {
element = document.querySelector("." + elementClassName)
}
if(element != undefined) {
var elementHeight = element.offsetHeight
var elementVH = (elementHeight / window.innerHeight) * 100
element.style.setProperty(cssVariableName, elementVH + "vh")
}
}
onResize()
window.onresize = onResize
}
</script>
</head>
<body>
<div class="container">
<span style="font-size:calc(var(--container-vh) * 0.25)">25%</span>
<span style="font-size:calc(var(--container-vh) * 0.50)">50%</span>
<span style="font-size:calc(var(--container-vh) * 1.00)">100%</span>
</div>
</body>
<script>
setCSSVariableAccordingToElementHeightRelativeToViewPort("container", "--container-vh", true)
</script>
</html>
JSFiddle