Constantly changing the size of an IFrame due to CSS View Height

I am facing an unusual issue with the CSS property vh (viewpoint height), causing my iframe to expand uncontrollably. To explain, within my iframe code, I have the following:

function sendPost() {         
                
                setInterval(function(){

                    try {
  

                        let adjust_height = document.body.offsetHeight || document.body.clientHeight;

                        parent.postMessage( JSON.stringify({ command: 'adjust_height', height: adjust_height}), '*');
                      
                    } catch(e) {
                        console.error(e);
                    }

                }, 3000);
}

In short, every 3 seconds, I send the new content height inside the iframe to its parent, which then adjusts the iframe height accordingly to fit the content without scrolling. It works perfectly! If I include something like this:

#mainContainer {
   min-height: 75vh;
}

The HTML inside the iframe can be as simple as:

<html>
   <body>
       <div id="mainContainer"></div>
    </body>
</html>

However, this causes the mainContainer to constantly increase in size. For example:

//1st Interval Height is 500
let adjust_height = document.body.offsetHeight || document.body.clientHeight;

//2nd Interval Height is 700
let adjust_height = document.body.offsetHeight || document.body.clientHeight;

//3rd Interval Height is 900
let adjust_height = document.body.offsetHeight || document.body.clientHeight;

When I remove the VH css property, the height no longer grows endlessly. Why does using VH affect the document height within an iframe?

Answer №1

vh represents a relative size, as explained in more detail at: CSS values and units

The concept of 1vh equates to 1% of the viewport's height. It is worth mentioning that for an iframe, the iframe serves as the viewport itself.

In your scenario, if the content within the iframe expands, the parent element adjusts the iframe's dimensions accordingly, leading to the amplification of 75vh, triggering a continuous loop.

Further information can be found here: Viewport concepts

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Form Field Highlighting

I am currently attempting to eliminate the blue "halo" outline that appears on form elements in Firefox on OS X. I have successfully removed the halo in Safari on OS X using CSS with the following code: input { outline: none; } However, this method doe ...

How can you include a multi-layered array within another multi-layered array using TypeScript?

If we want to extend a two-dimensional array without creating a new one, the following approach can be taken: let array:number[][] = [ [5, 6], ]; We also have two other two-dimensional arrays named a1 and a2: let a1:number[][] = [[1, 2], [3, 4]]; let ...

Combining JSON objects to form a new object within a JSON object using JavaScript

Here is the JSON data that I have: {"rows":[ {"shiftId":1,"shift":"Morning","item":"Tea","value":20}, {"shiftId":1,"shift":"Morning","item":"Coffee","value":30}, {"shiftId":2,"shift":"Evening","item":"Tea","value":40}, {"shiftId":2,"shift" ...

Sorry, an error occurred while loading the audiosprite - Cannot locate module: 'child_process'

Currently, I am attempting to integrate audiosprite into my React application running on a Webpack server. However, encountering the following error: Error: Module 'child_process' not found in C:\Users\BenLi\Desktop\devel ...

Issue: Ajax is not defined

When attempting to call a URL in the background using the following code within a script tag: var request = new Ajax.Request(logoffURL, {method : 'post'}); I encountered a script error stating Ajax is undefined. Is it necessary to include any ...

Divs that are floated and only partially visible at the end

My layout consists of 6 Divs floated left to create 6 columns. The width of all these floats combined may extend beyond the window width for most users, especially with the 6th column included. Is there a way for this 6th column to be partially visible ( ...

Differences in <img> sizing behavior between Chrome and Firefox

Objective: Develop a scrollable image-gallery web component with layouting that functions without script intervention. Specifications: The size of the web component must be fully responsive and/or adjustable. The top main area of the widget is the galle ...

Exploring the power of Angular 2 in conjunction with web APIs

After experimenting with Angular 2 for some time, I decided to explore using web APIs like geolocation (utilizing the navigator object) and browser Notifications for some fun projects. Check out this link about notifications in web APIs Here's a res ...

Eliminate the use of "!important" in bootstrap CSS

I am trying to customize the .text-primary class with a color of my choice. To do this, I added the following code in my override file: @include text-emphasis-variant(".text-primary",$brand-primary, true); The text-emphasis-variant is a mixin from Bootst ...

Using Rails and HAML to incorporate looping HTML5 video tags

I have a question regarding how I'm using Rails video_tag to display a video: = video_tag("SMR_video.mp4", :controls => false, :autobuffer => true, :autoplay => true, :loop => true, :id => "hero-video") After implementing the above co ...

Initiate node.js execution upon system boot

Just diving into node.js. In the process of setting up integration tests for a node.js app using mocha, I found this helpful guide: Here's how I created a server: var http = require('http'); this.server = http.createServer(function (req, ...

Organizing and Arranging List Elements (li)

Imagine having this list: <ul> <li>item1</li> <li>item2</li> <li>item3</li> <li>item4</li> <li>item5</li> <li>item6</li> <li>item7</li> <li>item8</li> ...

Is there a way for me to view the specifics by hovering over a particular box?

Whenever I hover over a specific box, detailed information appears, and when I move the mouse away, the details disappear. HTML <nav> <div> <div class="nav-container"> <a href="./about.html" ...

What is the best way to send JSON data to Sass in ReactJS?

In my current project, I am developing a React/Redux application with Webpack that showcases UI components and enables users to adjust the colors of those components. The styling is being done using CSS Modules with scss. My preference is to keep all the s ...

Triggering an event for a div using Javascript, along with a Rails loop

I have a page showcasing a collection of listings that are displayed on the main page. <% @listings.each do |listing| %> # do something <% end %> Each listing now includes a data-toggle, essentially a button. <a id="chat-menu-toggle" h ...

How can I prevent users from using the inspect element feature in Angular 4?

I have a collection of elements that I need to selectively display on a webpage. To achieve this, I am utilizing the following code snippet: [style.display]="!student.selected?'none':'block'" However, upon inspecting the element, a ...

Creating three-dimensional text in Three.js

My script is based on this documentation and this resource. Here is an excerpt of my code: <script src="https://raw.github.com/mrdoob/three.js/master/build/three.js"></script> <script> var text = "my text", height = 20 ...

What are the steps for creating an HTML table with rowspan and colspan functionalities?

Is it possible to create a table with rowspan and colspan using HTML? https://i.sstatic.net/bKQrJ.jpg I am looking for guidance on how to construct a table with specified row spans and column spans. <link rel="stylesheet" href="https://stackpat ...

securely managing file access through lockfile.locksync in node.js

Currently, I am utilizing lockfile.locksync to secure a file in my node.js application. However, I am interested in understanding the inner workings of this utility in more detail. Despite multiple resources referring to it as a "very polite lock file util ...

The difference between invoking a method directly and utilizing a function to invoke a method

Imagine we have a method inside a class that looks like this class Blog extends Component { postClicked = (id) => { this.setState({selectedPostId: id}) } render () { const newPosts = this.state.posts.map(el => { return & ...