Embed External Content via iframe and div
by Jeff Starr on Tuesday, January 2, 2007 – 27 Responses
By using an <iframe> within a <div>, it is possible to include external web content in most any web document. This method serves as an excellent alternative to actual frames, which are not as flexible and definitely not as popular. Indeed, with CSS, the placement, sizing, and styling of div’s provides endless possibilities for embedding external or even internal web content into pages that would otherwise require the use of frames, Flash, or JavaScript. This method works on any modern browser, as well as any old browser that understands both <div> and <iframe> tags. Simply add the following code to your document and style according to your specific needs:
<div style="position:absolute; left:77; top:77; width:377; height:377; clip:rect(0,381,381,0); background:#FFF;">
<iframe src="http://www.google.com/" width="377" height="377" marginwidth="0" marginheight="0" frameborder="no" scrolling="yes" style="border-width:2px; border-color:#333; background:#FFF; border-style:solid;">
</iframe>
</div>
Here is the code again, this time commented with explanatory information:
<div style="
clip: rect(0,381,381,0); // right-clip equal to div width plus total border width
// bottom-clip equal to div height plus total border height
position:absolute; // used for positioning and may or may not be required
background: #FFF; // background color of div may or may not be seen
height: 377; // height of window (div) that contains the iframe content
width: 377; // width of window (div) that contains the iframe content
left: 77; // absolute position of window (div) from the left edge of browser
top: 77; // absolute position of window (div) from the top edge of browser
">
<iframe
src="http://google.com/" // location of external resource
width="377" // width of iframe should match the width of containing div
height="377" // height of iframe should match the height of containing div
marginwidth="0" // width of iframe margin
marginheight="0" // height of iframe margin
frameborder="no" // frame border preference
scrolling="yes" // instructs iframe to scroll overflow content
style="
border-style: solid; // border style
border-color: #333; // border color
border-width: 2px; // border width
background: #FFF; // background color
">
</iframe>
</div>
Please note that many of the tag attributes (e.g., position, width, height, etc.) may be removed the (X)HTML markup and included externally by adding the appropriate id hooks (e.g., id="division" and id="iframe") and styling via an external CSS document.
Focused on clean code and quality content, Perishable Press is the online home of Jeff Starr, author, artist, designer, developer, and all-around swell guy. 





27 Responses
Jason – #1
Just wanted to say thanks for this article… was just the info I was looking for!
J.Nothing – #2
I wanted to say also.. thanks. I’m doing of all things… a myspace div overlay for someone, and the question of how to implement iframes cleanly and efficiently cropped up. This helped a ton.
Thanks!
Perishable – #3
Thank you for the kind words — it is always great to get positive feedback!
John – #4
Thanks really needed this
Perishable – #5
My pleasure!
Nat – #6
Thank you so much,been searching for a week on codes and random things. Now finaly one works.
This is such a god sent.
Perishable – #7
Glad to be of service, Nat!
Dwayne – #8
I found another way to embed an external document without using iframes. I really do not like them: try this:
In CSS:
/*Changes divs to work as frames*/.frame {overflow: auto; /* allows the div block to be scrollable */font-size: 14px;border:0px;margin:0px;padding:0px;}In HTML:
<a href="index.html" rel="nofollow">now...</a><!-- ... -->Perishable – #9
Dwayne, this looks interesting, however, I think some of the code left in your comment got munged by WordPress.. would you be so kind as to re-comment using
<code>tags to wrap each line of example markup? — Thanks :)lance – #10
I was about ready to give up until I found this tip. Thanks. Unfortunately though, I’m still having trouble getting it to work when the iframe contains a flash movie. I got it working in Safari but the clip seems to be ignored in Firefox when it’s a flash movie. If I switch the src of the iframe to yahoo it works just fine. Any ideas?
Ligon – #11
I was wondering if we could show only a part of the external webpage (lets say bottom part) in the IFrame ?
Perishable – #12
Hi Ligon,
Yes, I am sure it would be possible (although I have never done it), however, you may need to call upon the ancient powers of absolute positioning via CSS for the external page in order to accomplish such a task.. Good luck!
Sander – #13
Lance:
I’m running in exactly the same problem. And guess what… we’re not alone!
If you set the ‘wmode’ property of your flash object to ‘transparent’ the problem seems to be solved. But the truth is even less satisfying. If you right click your flash document (to get the context menu) you’ll see it is totally out of place. Mouse clicks are not captured correctly so buttons (and other input elements) won’t work.
The combination: Div (with position:abosolute) -> Iframe -> Flash Document. simply won’t work in FireFox … for as far as i know…
JustinRyan – #14
I am stuck. I am new to css recently and wnat to learn it but I have a site I still want to use an IFRAME in because I don’t have the knowledge yet to make it work without 1. So to start off with my site redesign I am going with an IFRAME.
If you look at my site http://www.theblueroses.com you will see a css trick I obtained from another site and I love this overlapping images look. The css code allowed for scrollable contant. The problem is I like the ability to target the frame also. So i put an IFRAME in the DIV. it works but leaves a space on the bottom and the left side. Can you help me? Where am I am going wrong? I have tried using auto for the size of the iframe as well as 100% and a couple others. what do you think?
Perishable – #15
@JustinRyan: Many times, layout issues involving
frames,iframes, and scrollabledivs require manipulation of padding and/or margin properties for the framing element/page and/or the framed element/page. It generally takes awhile to tweak things, but eventually everything should gel.webfound – #16
hello
do you know a trick for positioning the framed content? in a case I want to hide the left and right columns of the page inside the frame, so only the center content would remain. Is this possible? thanks.
Perishable – #17
The best advice I can give concerning positioning the framed content is to style the target page directly, as if there were no frames present. Once you have the design you would like to frame, reference it via the
iframeand fine-tune as necessary. I hope that helps!Ali – #18
i have div containging iframe(shoppingcart). now i want to expand div downward as ….my iframe height grow…is this possible? please help me
Jeff Starr – #19
Hi Ali, I have tried doing this on a project once.. I think I ended up solving the issue with a little JavaScript. Unfortunately, I no longer have access to the code.
bee happy! – #20
Thanks for this code. This is really helpful!
Jeff Starr – #21
My pleasure! Glad to be of service :)
sabri – #22
Hey it is true but how can we take a div or table from a site or html
webdev – #23
Hi,
Nice post. I have main concern about showing an external site in an iframe. Is it ok to show like this legally as I am planning to show the external site in a pseudopop-up instead of iframe. Pelase let me know if this is ok to show other sites on our site via iframe/popup.
thanks
Jeff Starr – #24
@webdev: It all depends on the context, how you are using the content, and so forth. Unless the site is something like Google, your safest move would be to contact the owner of the site directly and obtain written permission. Otherwise, you just never know what could happen.
Kev B – #25
Hi,
This looks like a thread I need, but want to do a mixture of iframe and normal frame “look”.
In other words I need to add an external guestbook within a template layout inside a DIV(still with me?). I currently use real frames and load the guestbook as a framepage, but the new DIV layout produces 2 scrollers (one in the iframe and one for the page).
Is there a way to simlaute the real frames method within the webtemplate using DIV/iframe?
Thanks,
Kev
Kim – #26
I am hoping that someone can help me. I am using a fieldset menu and would like to target an iframe in an external page, so that when the user navigates to that page, the iframe will be different depending on which option was chosen. Can that be done? I am guessing I need a bit of javascript. Any ideas would be great. Thank you.
Trackbacks / Pingbacks