tags:

views:

61393

answers:

10

Hi,

I have simple page that has some iFrame sections (to display RSS links), how can I apply the same CSS format for the main page to the page displayed in the iFrame?

Thanks,

+9  A: 

An iframe is usually handled like a different html page by most browsers. If you want to apply the same stylesheet to the content of the iframe, just reference it from the pages used in there.

hangy
usually^h^h^h - *universally*
annakata
+6  A: 

If you control the page in the iframe, as hangy said, the easiest approach is to create a shared CSS file with common styles, then just link to it from your html pages.

Otherwise it is unlikely you will be able to dynamically change the style of a page from an external page in your iframe. This is because browsers have tightened the security on cross frame dom scripting due to possible misuse for spoofing and other hacks.

This tutorial may provide you with more information on scripting iframes in general. About cross frame scripting explains the security restrictions from the IE perspective.

Ash
+12  A: 

If the content of the iframe is not completely under your control or you want to access the content from different pages with different styles you could try manipulating it using JavaScript.

var frm = frames['frame'].document;
var otherhead = frm.getElementsByTagName("head")[0];
var link = frm.createElement("link");
link.setAttribute("rel", "stylesheet");
link.setAttribute("type", "text/css");
link.setAttribute("href", "style.css");
otherhead.appendChild(link);

Note that depending on what browser you use this might only work on pages served from the same domain.

Horst Gutmann
Might be worth noting that the same origin policy will stop this working if the page is on a different domain.
ConroyP
+17  A: 

There are two different things here: the style of the iframe block and the style of the page embedded in the iframe. You can set the style of the iframe block the usual way:

<iframe name='iframe1' id="iframe1" src="empty.htm" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

The style of the page embedded in the iframe must be either set by including it in the child page:

<link type="text/css" rel="Stylesheet" href="Style/simple.css" />

Or it can be loaded from the parent page with Javascript:

var cssLink = document.createElement("link") 
cssLink.href = "style.css"; 
cssLink .rel = "stylesheet"; 
cssLink .type = "text/css"; 
frames['frame1'].document.body.appendChild(cssLink);
DrJokepu
+1  A: 

You will not be able to style the contents of the iframe this way. My suggestion would be to use serverside scripting (PHP, ASP, or a perl script) or find an online service that will convert a feed to a javascript. the only other way to do it would be if you can do a serverside include.

Careful when you say that something cannot be done, when in reality it is just difficult
Lathan
+2  A: 

The above with a little change works:

var cssLink = document.createElement("link") 
      cssLink.href = "pFstylesEditor.css"; 
      cssLink .rel = "stylesheet"; 
      cssLink .type = "text/css"; 

      //Instead of this
      //frames['frame1'].document.body.appendChild(cssLink);
//Do this

      var doc=document.getElementById("edit").contentWindow.document 

      ///If you are doing any dynamic writing do that first
      doc.open;
      doc.write(myData);
      doc.close();

      //Then append child
doc.body.appendChild(cssLink);

Works fine with ff3 and ie8 at least

+1  A: 

If you want to reuse CSS and JavaScript from the main page maybe you should consider replacing <IFRAME> with a Ajax loaded content. This is more SEO friendly now when search bots are able to execute JavaScript.

This is jQuery example that includes another html page into your document. This is much more SEO friendly than iframe. In order to be sure that the bots are not indexing the included page just add it to disallow in robots.txt

<html>
  <header>
    <script src="/js/jquery.js" type="text/javascript">
  </header>
  <body>
    <div id='include-from-outside'></div>
    <script type='text/javascript'>
      $('#include-from-outside').load('http://example.com/included.html');
    </script> 
  </body>
</html>

You could also include jQuery directly from Google: http://code.google.com/apis/ajaxlibs/documentation/ - this means optional auto-inclusion of newer versions and some significant speed increase. Also, means that you have to trust them for delivering you just the jQuery ;)

Sorin Sbarnea
A: 

hi Sorin Sbarnea, can your method capture html generated by javascript in the remote page?