jd6699 jd6699 - 1 year ago 175
CSS Question

CSS override body style for content in iframe?

How can I control the background image and colour of a body element within an iframe? Note, the embedded body element has a class, and the iframe is of page that is part of my site.

The reason I need this is my site has a black background assigned to the body, and then a white background assigned to divs that contain text. A WYSIWYG editor uses an iframe to embed content when editing, but it doesn't include the div, so the text is very hard to read.

The body of the iframe when in the editor has a class that isn't used anywhere else, so Im assuming this was put there so problems like this could be solved. However when I apply styles to class.body they dont override the styles applied to body. The weird thing is that the styles do appear in firbug, so ive no idea whats going on!


UPDATE - Ive tried @mikeq's solution of adding a style to the class that is the body's class. This doesn't work when added to the main page stylesheet, but it does work when added with firebug. Im assuming this is because firebug is applied to all all ements on the page where as the css is not applied within iframes. Does this mean that adding the css after window load with javascript would work?

Answer Source

An iframe is a 'hole' in your page that displays another web page inside of it. The contents of the iframe is not in any shape or form part of your parent page.

As others have stated, your options are:

  • give the file that is being loaded in the iframe the necessary CSS
  • if the file in the iframe is from the same domain as your parent, then you can access the DOM of the document in the iframe from the parent.
  • don't use an iframe (are you SURE you need one?)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download