0

I generate an iframe with Vaadin 7 and the result is:

<div class="zoom-to-fit" style="width: 1200px; height: 900px;">
    <iframe src="http://localhost:8090/image.png" frameborder="0" width="100%" height="100%">
    </iframe>
</div>

I can add a style class zoom-to-fit to the iframe, but the iframe is wrapped inside a div.

I am trying to stretch the image to the iframe, but it does not work.

I tried the following:

.zoom-to-fit {
    width: 100%;
    height: 100%;
}
.zoom-to-fit {
    background-size: 100% 100%;
}
.zoom-to-fit > html > body > img {
    width: 100%;
    height: 100%;
}

But they don't work.

I added the last one because the html code in the developer tools looks like:

enter image description here

I guess I cannot modify any styles inside the iframe... but how could I resize the image?

I know that if I go to the image and add the first style (width and height at 100% works) but I can only add or remove styles with the Vaadin framework I am using.

Sebastian D'Agostino
  • 1,575
  • 2
  • 27
  • 44

2 Answers2

0

I saw this from a forum post that might help: https://vaadin.com/forum/thread/393458/embedded-component-image-and-setwidth-resizing-both-component-and-image

also there's this from another SO question: Resizing Vaadin Image

Luis Gtz
  • 23
  • 3
0

So, it is impossible to get to change the style with css, so the solution is a bit nasty (or not very nice).

CSS does not know the structure inside iframe, so that it is why it is not applied as I would have wanted. But since bot the site and the content of the iframe are from the same origin, javascript is safe to use (and the only way to make it work).

Luckily there is a tool in vaadin to execute javascript safely. So I can get the iframe tag and then the img tag.

import com.vaadin.ui.JavaScript;

JavaScript.getCurrent().execute(
    "document.getElementsByTagName('iframe')[1].contentDocument.body.children[0].style=" 
    + "'width: 100%; height: 100%;'");

This is vanilla javascript so it should work in most browsers.

Sebastian D'Agostino
  • 1,575
  • 2
  • 27
  • 44