I need to represent the current viewport shape in miniature, fitting 100% of a containing element's width or height, as appropriate for the aspect ratio. I want to offer a preview of how an HTML structure will fit into the screen as it is at that moment.
To do this, I believe I need to determine the aspect ratio of the screen. Of course this can be done with JavaScript, but it would be more elegant in my case to do it with CSS.
It might look something like this. Here I'm using the zero height trick with bottom padding providing the aspect ratio. It doesn't work because the calc()
function can't divide by a value with units.
.container {
width: 240px;
height: 160px;
background: #ddd;
}
.vieweport-miniature {
height: 0;
padding-bottom: calc(100vw / 100vh); /* can't divide by viewport units */
padding-bottom: 45%; /* for demonstration purposes only */
max-width: 100%;
max-height: 100%;
background: pink;
}
<div class="container">
<div class="vieweport-miniature"></div>
</div>
There are many questions on SO asking something similar, but they tend to have a predefined aspect ratio. Here I want it to match that of the viewport dynamically.
Thanks!