6

Using a iframe where I call a site from my webspace, Using one css file with body {background-color: #222}.

The iframe src also use this CSS file. The Problem is that the body in the iframe need another background-colour.

tried

iframe>body { background-color: #other }

Any idea or suggestions?

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Denny Mueller
  • 3,505
  • 5
  • 36
  • 67

6 Answers6

7

I assume the iframe takes up a smaller portion of your site. In that case I would advice you to simply use a div with the same size of the iframe is loaded, and giving this div the proper background-color.

OR: include a style property in the source of the iframe page:

<head>
    <style>body {background-color:#COLOR}</style>
</head>
Daan Twice
  • 337
  • 1
  • 3
  • 15
5

This question is similar to How to apply css to iframe content?. Basically you can use jQuery to change the CSS properties in the iframe

$('#yourIframe').contents().find('body').css({
    background-color: '#333333'
});

Edit: You may need to add that when the iframe loads:

$('#yourIframe').load(function(){ 
    $(this).contents().find('body').css({
        background-color: '#333333'
    });
});
Community
  • 1
  • 1
walmik
  • 1,440
  • 2
  • 13
  • 30
  • Only works if the iFrame is from the same parent domain – Nah Feb 09 '18 at 12:13
  • That is because of the same origin policy. It s a critical security mechanism to prevent someone loading up just about any site in a iframe and changing stuff there to serve a ulterior motive. – walmik Feb 25 '18 at 06:04
4

Remember that a Javascript can modify only properties of iframe with the same origin of the site, otherwise you'll get a Security Error.

Protocols, domains and ports must match.

Vito Schiavo
  • 223
  • 1
  • 2
  • 8
2

ive used this and it works for me. (and its sooooo easy!)

in your CSS file put:

iframe { background-color:orange; }

Just change the "orange" to whatever color you need. Thats it!

Enjoy!

Jeff
  • 21
  • 1
0

In the page that the iframe contains, link another stylesheet and then change all the CSS styles that you wish to.

Eric Frick
  • 847
  • 1
  • 7
  • 18
0

using Javascript We can add Background Color to Iframe

 var x = document.getElementById("myframe");
    var y = (x.contentWindow || x.contentDocument);
    if (y.document)y = y.document;
    y.body.style.backgroundColor = "red";

Reference w3schools

stalin wesley
  • 165
  • 5
  • 13