Sign In | Register Now About Bytes | Help | Site Map
Connecting Tech Pros Worldwide

iframe width cannot be controled ?

Question posted by: mynameisyohann (Newbie) on July 2nd, 2008 01:02 PM
hi guys,

I am trying to make some iframes fit into a div with variable width. The iframe must have a margin, and using a padding in the parent div element is not an option.

Unfortunately, the width:auto for iframe gives a preset width, and does not behave like for the div elements.

As a result, the iframe always seem to resize its container.

See the testcases below :

This is how it behaves when a div contains a div with width:auto > that's the result I want to achieve with an iframe
<div style="width:100px;background-color:blue">Container<div style="width:auto;margin:10px;background-color:white">content</div></div>

This is how it behaves when a div contains an iframe with width:auto
<div style="width:100px;background-color:red">Container<iframe style="width:auto;margin:10px" src=""></iframe></div>

This is how it behaves when a div contains an iframe with width:100% (the container becomes too big)
<div style="width:100px;background-color:red">Container<iframe style="width:100%;margin:10px" src=""></iframe></div>

Any ideas ?
Thanks,

Yohann
dlite922's Avatar
dlite922
Site Addict
555 Posts
July 6th, 2008
08:41 AM
#2

Re: iframe width cannot be controled ?
may I ask why you need to use iFrame? maybe there's a different solution?

-Dan

Reply
hsriat's Avatar
hsriat
Expert
1,466 Posts
July 7th, 2008
04:35 PM
#3

Re: iframe width cannot be controled ?
I tried, but could not get a CSS solution.

You may find this useful:
Expand|Select|Wrap|Line Numbers
  1. window.onload = function () {
  2.     var ifm = document.getElementsByTagName('iframe')[0];
  3.     ifm.style.width = ifm.parentNode.style.width;
  4. }

Reply
Reply
Not the answer you were looking for? Post your question . . .
189,088 Experts ready to help you find a solution.
Sign up for a free account, or Login (if you're already a member).

Latest Articles: Read & Comment
Top HTML / CSS Forum Contributors