Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML -- height as a percentage of the width for iframe

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?

like image 584
Revada Avatar asked Apr 10 '14 18:04

Revada


People also ask

Can iframe width be in percentage?

Yes you can provide height and width in % to iframe.

How do I make my iframe height 100%?

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.

How do I set the width and height of an iframe?

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.

How do I set the iframe height to fit content?

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.


2 Answers

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%
}
like image 80
PadreZippo Avatar answered Oct 30 '22 01:10

PadreZippo


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.

like image 31
urbz Avatar answered Oct 30 '22 01:10

urbz