I have an embedded sketchfab viewer on the page and want to keep an aspect ratio of 3/2. So as an example if the Height=300px the width = 200px. The width needs to be percentage based (100% of div) and the height calculated as a percentage of the generated width.
Random guess: height:(66% of width); similar to height:(same as width);
is there a way of doing this?
Yes you can provide height and width in % to iframe.
Next, we put the <iframe> inside this box, making it fill the whole box. To size the <iframe> , we ignore the width="560" height="315" element properties, and instead use the CSS properties width:100%;height:100%; . That's it: a full-width <iframe> with fixed aspect ratio. Enjoy.
The height & width of the iframe has to be set to 100% without scroll, except for the scroll that comes for the body when the content is loaded and the size of the content is more. The content of the iframe is another HTML page from the same domain. The iframe also needs to scale according to the responsive HTML page.
You can use the JavaScript contentWindow property to make an iFrame automatically adjust its height according to the contents inside it, so that no vertical scrollbar will appear.
See Sathran's answer here: Height equal to dynamic width (CSS fluid layout)
The HTML:
<div class="model-box">
<iframe class="model" src="https://sketchfab.com/models/442c548d94744641ba279ae94b5f45ec/embed" frameborder="0" allowfullscreen=""></iframe>
</div>
The CSS:
.model-box {
position: relative;
width: 100%;
height: 0;
padding-top: 66.6%; /* This is your aspect ratio */ }
.model {
position: absolute;
top: 0;
left: 0;
bottom: 0;
right: 0;
width: 100%;
height: 100%
}
This can be done by just CSS.
If you want to keep aspect-ratio I suggest adjusting padding-top
percentages relative to the width %.
Demo here
Adjust accordingly until you find a suitable solution for yourself.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With