If you're willing to use an iframe for this, I have a creative solution.
Font size can be changed dynamically relative to the viewport size. You want to resize the container and not the window, so for this to work we'll have to wrap everything with an iframe which has a viewport of its own, and resize the iframe.
The idea is to set the width and height of your element with em units, and set the font size dynamically.
We have 2 scenarios, a wider container or a higher container, to separate it we'll use media query:
max-aspect-ratio: 16/9
1. A higher container
Since we want the div to keep 16:9 ratio we'll set the div's dimensions to
width: 16em;
height: 9em;
In order for it to take 100% width we'll set the the font-size to 100/9 = 11.111
font-size: 11.111vh
2. A wider container
Once our media query kicks in and the container is wider so we'll change the font-size to be based on the viewport width and we want it to take 100% width so -> 100/16 = 6.25
@media (max-aspect-ratio: 16/9) {
.ratio-wrapper {
font-size: 6.25vw;
}
}
Now we can add another div for the content and set whatever font size we want inside.
var iframe = document.getElementById('iframe');
var content = "<html>" +
"<head>" +
"<style>" +
"body," +
"html {" +
" padding: 0;" +
" margin: 0;" +
"}" +
".ratio-wrapper {" +
" background-color: #bada55;" +
" font-size: 11.111vh;" +
" width: 16em;" +
" height: 9em;" +
"}" +
".center {" +
" width: 100%;" +
" height: 100%;" +
" display: flex;" +
" justify-content: center;" +
" align-items: center;" +
"}" +
".content {" +
" height: 100%;" +
" display: flex;" +
" justify-content: center;" +
" align-items: center;" +
" font-size: 20px;" +
"}" +
"@media (max-aspect-ratio: 16/9) {" +
" .ratio-wrapper {" +
" font-size: 6.25vw;" +
" }" +
"}" +
"</style>" +
"</head>" +
"<body>" +
"<div class='center'>" +
"<div class='ratio-wrapper'>" +
"<div class='content'>It Works!</div>" +
"</div>" +
"</div>" +
"</body>" +
"</html>";
iframe.src = 'data:text/html,' + encodeURIComponent(content);
.cont {
width: 300px;
height: 300px;
border: 1px solid black;
resize: both;
overflow: hidden;
padding: 0;
margin: 0;
}
<iframe id="iframe" src="about:blank" class="cont" scrolling="no"></iframe>
Note that I'm only using JS to add the content to the iframe, so it will work here, you can just set a href to your inner content and no JS is needed. I made the iframe resizable, so you can play with it easily, alternatively you can give it a responsive size, it works too.