How to apply CSS to iframe?


I have a simple page that has some iframe sections (to display RSS links). How can I apply the same CSS format from the main page to the page displayed in the iframe?

12/16/2019 4:55:34 AM

I met this issue with Google Calendar. I wanted to style it on a darker background and change font.

Luckily, the URL from the embed code had no restriction on direct access, so by using PHP function file_get_contents it is possible to get the entire content from the page. Instead of calling the Google URL, it is possible to call a php file located on your server, ex. google.php, which will contain the original content with modifications:

$content = file_get_contents('');

Adding the path to your stylesheet:

$content = str_replace('</head>','<link rel="stylesheet" href="" /></head>', $content);

(This will place your stylesheet last just before the head end tag.)

Specify the base url form the original url in case css and js are called relatively:

$content = str_replace('</title>','</title><base href="" />', $content);

The final google.php file should look like this:

$content = file_get_contents('');
$content = str_replace('</title>','</title><base href="" />', $content);
$content = str_replace('</head>','<link rel="stylesheet" href="" /></head>', $content);
echo $content;

Then you change the iframe embed code to:

<iframe src="" style="border: 0" width="800" height="600" frameborder="0" scrolling="no"></iframe>

Good luck!

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");

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


var $head = $("#eFormIFrame").contents().find("head");

$head.append($("<link/>", {
    rel: "stylesheet",
    href: url,
    type: "text/css"

Here is how to apply CSS code directly without using <link> to load an extra stylesheet.

var head = jQuery("#iframe").contents().find("head");
var css = '<style type="text/css">' +
          '#banner{display:none}; ' +

This hides the banner in the iframe page. Thank you for your suggestions!


An iframe is universally 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.


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Email: [email protected]