Understanding Iframes
Iframes can be tricky. In theory, they’re simplicity itself – need to incorporate the content of another page onto your site? Use an iframe! In practice, all sorts of issues pop up. Styling them can be a hassle. Weird and unwanted scroll bars show up, sometimes in multiple places. JavaScript events don’t fire, or fire inconsistently. The iframe, in short, just won’t integrate properly into the rest of your page.
The trick to successfully managing these issues is to change your thinking. The difficulty of integrating the iframe into your page isn’t a bug, it’s the essence of what an iframe is. Once you realize that, it becomes far easier to work with iframes.
Whenever I’m working with an iframe, I pretend that the iframe is simply a brand new browser window with no menu or address bar that happens to be sitting on top of my page. This is, in fact, not much of an imaginative stretch, because that’s basically what an iframe is. Despite the fact that the tag <iframe> is present in your code, the source of that iframe is not actually part of your page.
Here are two screen shots illustrating what I mean. The first screen shot is a page with an iframe. The second is the same page but with a separate browser window sitting on top of it. Conceptually, these amount to the same thing.


If I keep in mind that the iframed content is essentially a separate browser window, managing it become much easier. I don’t expect the CSS or JS in one browser window to have any effect on any other open windows, and so I don’t expect the JS and CSS in my main page to have any effect on my iframe content. If I want my iframe content to be styled in a certain way, then I make the CSS changes in the iframed page’s code, not in the main page’s code. If I need jQuery to be available in my iframed page, then I’d better make sure I’m loading it in my iframed page’s code, regardless of whether or not I’ve already loaded jQuery in my main page. The page visible in my iframe, and the main page, are for all intents and purposes two completely separate pages that simply happen to be simultaneously visible on my screen.
What about scrollbars? Again, thinking of the pages as separate helps. If I open up a new browser window, and shrink it so that it is smaller than the content it is displaying, then I’m going to see scrollbars. If I do not want scrollbars, I need to either make my browser window larger, or shrink my content. It works exactly the same way for an iframe – either make sure that the iframe’s dimensions are large enough to fit your content, or make sure that your content is short and narrow enough to fit inside your iframe.
All of this points toward one more consideration – do you really want an iframe? If you are simply looking to incorporate images and text from a remote site, consider ajax, json, RSS, or some other approach that simply grabs remote content and inserts it into the DOM. This way it actually becomes part of your document and is subject to the same CSS and JS that applies to the rest of your page. Only use iframes when you truly need to display content that is completely separate from the rest of your page.
