Change body background color in an iframe using one css file

后端 未结 6 1207
隐瞒了意图╮
隐瞒了意图╮ 2020-12-18 07:51

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 P

相关标签:
6条回答
  • 2020-12-18 08:22

    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'
        });
    });
    
    0 讨论(0)
  • 2020-12-18 08:34

    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!

    0 讨论(0)
  • 2020-12-18 08:39

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

    0 讨论(0)
  • 2020-12-18 08:39

    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

    0 讨论(0)
  • 2020-12-18 08:40

    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>
    
    0 讨论(0)
  • 2020-12-18 08:42

    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.

    0 讨论(0)
提交回复
热议问题