I’m using the iframe-resizer package to dynamically change the size of an iframe depending on content.
However, before even trying any dynamic resizing, I run into an issue with the basic iframe: it’s always 300px
in width. Whatever content I place inside the iframe, the width is always 300px. Will not move; overflow is hidden if I add something >300px and still takes up 300px if my content is smaller than that.
EDIT: To be clear, My problem is that I want to dynamically change the width of an iframe (cross-domain), but it always renders at 300px and will not change. I’m using the iframe-resizer package to successfully dynamically change the height, but that’s nto working on the width.
In the parent site, I’m embedding the iframe as follows:
<script type="text/javascript"
src="https://cdn.jsdelivr.net/npm/[email protected]/js/iframeResizer.min.js">
</script>
<script type="text/javascript">
var i = new URLSearchParams(window.location.search).get("openWidget"); document.write('<iframe id="inlineFrameExample" title="Inline Frame Example" style="position: fixed; zIndex: 1000; bottom: 0; right: 0" frameBorder="0" scrolling="no" src="http://localhost:3001?openWidget=" + i + ""></iframe>');
window.iFrameResize({ log: true }, '#inlineFrameExample')
</script>
And the content in my for my framed site, I have the following elements and styling:
<div className="App" id="App2">
{openWidget && <div className="button">OK</div>}
{showMessage && <section>This is a message</section>}
<div>
<button className="button" onClick={() => setShowMessage(!showMessage)}>
Msg
</button>
<button className="button" onClick={() => setOpenWidget(!openWidget)}>
{openWidget ? "-" : "+"}
</button>
</div>
</div>
.App {
text-align: center;
float: right;
}
.container {
display: flex;
justify-content: flex-end;
float: right;
}
section {
background-color: mediumseagreen;
color: white;
width: 500px;
}
.button {
width: 100px;
height: 100px;
background-color: mediumseagreen;
color: white;
text-align: center;
float: right;
}
Note that there’s nothing in there about width being 300px. There are other widths, but the iframe seems to ignore them and always set itself to 300px.
I also made two code sandboxes, and embedded one site in the other via an iframe. Unfortunately the iframe is hidden for some reason, but if you inspect and look for the iFrame with id=”inlineFrameExample” (and not code sandbox’s button iframe) you’ll see it’s 300px wide: https://exzts.csb.app/
The code for the parent site is here: https://codesandbox.io/s/eloquent-flower-exzts?file=/index.html
The code for the framed site is here: https://codesandbox.io/s/iframe-test-ss8fs
UPDATE: I also removed all css styling from both the parent site and the framed site. Still always stuck at 300px. I also viewed the CSS specs which says default will be default set to 300px under certain conditions, but I cannot figure out how to dissatisfy those conditions so that the 300px rule doesn’t apply.
7
1 Answer
Not sure if I understand the question. I think you’ve made an App and a page. On the page you want to use an iframe to show your App and the iframe should be scaled by default to the size of the app. Did I get that right?
I think JavaScript might by your solution. Get the content from the iframe, check in the content for a class App en that’s the width you want to use for your iframe.
const iframe = document.querySelector("#inlineFrameExample");
const iWindow = iframe.contentWindow;
const iDocument = iWindow.document;
const iElement = iDocument.querySelector(".App");
iframe.style.width = iElement.style.width;
A problem with this script is that it can only be executed after the content from the iframe was loaded. So make sure you wait for that with a timer,delay, promise, or something. If you don’t want to show the resizing to your visitors make sure the iframe has visibilty: hidden and add visibilty: visible of the iframe to the end of the script above.
T-S is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
2
I don’t think this works, because the app in the iframe isn’t intialised when the parent page loads — but this script also runs when the parent page loads. Tried adding async await in there but also didn’t work (and started to get messy).
–I’ve just read in another comment from you that a client is suppossed to press a button and after that event the iframe is loaded. As the iframe won’t dynamically resize it’s width I still think you could use the above method, but another option would be (but really messy) to do a XMLHttpRequest to the page or stylesheet that will be loaded. Get the responseText and from there get to the width of the content. If you do this request before loading the iframe you’ll know how wide it needs to be and you can set it’s width.
– T-S
Default iframe width is 300px, see stackoverflow.com/questions/5871668/…. Did you try setting width: 100% or something?
@James I did set
width:100%
on the iframe itself (where I embedded it in the parent site), but that caused it to take up the whole 100vw of the parent site. Should I be setting it elsewhere?@tao Edited my post. Also already put those things in the bounty request.
So you want the iframe to set its own width based on its contents? That’s simply not achievable. It’s like you’d expect a webpage to resize the browser. That’s pretty much what the iframe element is for its contents. Also, that’s not a very good place to define the requirement. That text will go away once the bounty expires. And your question doesn’t make much sense without it, hence my question above. I’d move it in the question.
@tao So, if I’m understanding correctly, the iframe can only set its own height based on its contents, and not its own width?