0

How can I target image inside iframe with CSS?

This doesn't work

iframe img {
    height: auto;
    width: 100%;
}

enter image description here

Ivan Topić
  • 3,064
  • 6
  • 34
  • 47
  • That's not possible with CSS only, check this out: http://stackoverflow.com/questions/583753/using-css-to-affect-div-style-inside-iframe – Chris May 04 '16 at 11:48

4 Answers4

4

try jquery for this

$('iframe').contents().find('img').css({width: '100%', 'height': '100%'});

wrap this in jQuery load callback if iframe loads later after the page loaded

$('iframe').load(function() {
    $(this).contents().find('img').css({width: '100%', 'height': '100%'});
})
Can Celik
  • 2,050
  • 21
  • 30
1
document.querySelector('iframe[name="content2"]').contentWindow.addEventListener('load',function(e){e.target.body.style.textAlign='center'},false) 
0

This is a common problem but the answer is same that you can't access things in <iframe> neither with css nor with jquery.

If you have access to the page you are using in <iframe> go and write css on that page then it will be automatically work in <iframe>. But you cant change anything in <iframe> with external code.

Gaurav Aggarwal
  • 9,809
  • 6
  • 36
  • 74
0

try this under the same domain. otherwise it throws cross origin error.

Update with On load jQuery function.

$('iframe').on("load", function () {

    $(this).contents().find('img').css({width: '100%', 'height': '100%'});

});
Community
  • 1
  • 1
Manoj
  • 2,000
  • 2
  • 16
  • 21