Switching the orientation of an iPhone on Chrome can lead to content being cut off

Currently, I am conducting testing on an iPhone XS running iOS 15.6.1

The website seems to function properly on iOS Safari (although untested on Android), however, a problem arises when using Chrome and rotating the screen from landscape to portrait mode. It appears that the content is cropped to the previous viewport height of landscape mode.

I came across this reported bug on Google Support, but it seems that the examples provided in that thread no longer exhibit the same issue.

My suspicion is that the issue may be related to my use of JavaScript to set the height of the html/body/container elements as a workaround for the known problem with the 100vh viewport issue when the URL bar resizes. While the inline value does update upon resizing the browser window, it might not do so correctly during orientation changes on iOS Chrome.

Should I implement something like the code snippet below, to trigger a script when the device is rotated?

window.addEventListener("orientationchange", (event) => {
  console.log(`the orientation of the device is now ${event.target.screen.orientation.angle}`);
});

const documentHeight = () => {
  const doc = document.documentElement
  doc.style.setProperty('--doc-height', `${window.innerHeight}px`)
}
window.addEventListener('resize', documentHeight)
documentHeight()
/**
 * Base `body` styling.
 *
 * 1. Fix for viewport height/address bar issue on iOS Safari. The height of the 
 *    container is set with javascript.
 * 2. As we have `overflow: hidden` on the `html` element, we allow the page to
 *    overflow on smaller viewports below hiding again on wider viewports.
 */

html {
    height: 100vh;
    height: var(--doc-height); /* [1] */
    overflow: hidden; /* [2] */
}


/**
 * Base `body` styling.
 *
 * 1. Fix for viewport height/address bar issue on iOS Safari. The height of the 
 *    container is set with javascript.
 */

body {
  height: 100vh;
    height: var(--doc-height); /* [1] */
    margin: 0;
    padding: 0;
}

/**
 * Grid
 *
 * 1. Fix for viewport height/address bar issue on iOS Safari. The height of the 
 *    container is set with javascript.
 */

.grid {
  background: red;
  height: 100vh;
  height: var(--doc-height);
}
<html>
  <body>
    <div class="grid"></div>
  </body>
</html>

Answer №1

A solution that proved effective for me was...

setTimeout(()=>{
  // include any code that relies on innerWidth and innerHeight
  // I found that a delay of at least 55 milliseconds was necessary during my local testing
}, 55) 

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

ngClass causing styling issue when applying styles

When I use class names like error and info, the CSS works fine. For example, in this Angular app (latest version) here: https://stackblitz.com/edit/github-i4uqtt-zrz3jb. However, when I try to rename the CSS classes and add more styles, like in the examp ...

What causes the toggle to malfunction when clicked on, specifically when the element is assigned the JS class?

Currently, I am working on animating cards using CSS and JavaScript. Everything seems to be working fine except for one issue – the class does not get removed on the second click on the element when using this.classList.toggle('is-active');. Ca ...

Cloud function -> time stamps evaluation

I've been working on a cloud function to delete items in the "links" collection that have an end time older than the current timestamp. Although my function runs without any errors, it's not deleting the items as expected and is causing me quite ...

Deploying to itunesconnect on Bitrise fails even with valid profiles and certificates

Current Scenario Utilizing Bitrise to deploy a single app on both Android and iOS has been a smooth process. The initial setup was done by a former coworker and I have been maintaining it since. Recently, we made the decision to split our app into two sep ...

Struggling with my HTML table styling

I attempted to construct the table below using two tables side by side, each with a width of 300, but due to the layout, when I attempt to create it as one table (which is my goal), the heights on the left and right become jumbled up because the cells on t ...

Utilizing document.write to switch up the content on the page

Every time I attempt to utilize document.write, it ends up replacing the current HTML page content. For instance, consider this HTML code: <body> <div> <h1>Hello</h1> </div> and this jQuery code: var notif = document. ...

Retrieve the identification number from the specific row chosen in the table

Having some issues with retrieving the correct ID from the selected table row in Datatable. The alert I'm using to check the output of the JS script always shows the same ID, regardless of how many rows are in the table list. Each row has a different ...

Issues with radio buttons not functioning or being able to be selected in Internet Explorer

My radio buttons are functioning perfectly in Chrome, Firefox, and Safari, but for some reason, they are not selectable in Internet Explorer (Version 7). Any suggestions on how I can resolve this issue? HTML: <label> <input type="radio" name ...

Is there a way to create rectangles with a designated percentage of blue color using CSS or Bootstrap 3?

There's an image on my laptop that perfectly illustrates what I mean, but unfortunately, I'm not sure how to share it with you. In essence, I am looking to create a rectangle filled with a specific percentage of the color blue. Could you assist m ...

JavaScript client side event that references the sender object ID in an aspx file

Can someone provide assistance with an issue in the code block below? function(s, e) { form1.hfRaiseEvent.value = s.ID; } This particular function is triggered when the Client Side Click Event occurs. From my understanding, s refers to the Sender Obj ...

Force the page to refresh if the user navigates back using the browser's back button

Similar Question: Cross-browser onload event and the Back button I am looking for a way to automatically refresh a view or page when a user navigates back to it using the browser's back button. Any suggestions? ...

Determining the distance between two points on a map while factoring in the zoom level using

Looking to enhance clustering functionality within a custom subclass of MKMapView. I've been struggling to find a solution for grouping overlapping annotations into a single cluster. While I can calculate the distance between two annotations in meters ...

Can you explain the functionality of that snippet of JavaScript code?

Can you figure out the value of r? How does it relate to Boolean operators and objects? var data = {x:123, y:456}; var r = data && data.x || 0; Update: Upon running the code snippet, I noticed that r consistently equals x. However, the reason behind thi ...

Insert the META tag data specific to Facebook Instant Articles directly within the JavaScript code

My website is running on a custom-built CMS provided by a company, and it seems like a modified version of WordPress. They are unwilling to include a basic meta tag in our HTML code. I am looking for guidance on how I can incorporate Facebook's Insta ...

Is it preferable to retrieve the image data or provide a direct link to the image when loading numerous images via network requests?

My iOS application showcases local places with pictures, text, and subtext in a table view. The detail view for each place includes more pictures and detailed information. JSON is used as the interchange format. Currently, I am sending image data as bit b ...

Enhancing User Experience: Creating a Vue Button Component for Adding Items to Cart with the Power of Axios and Laravel Backend Integration

I have recently developed a Vue3 shopping cart with an "Add One" button feature. When the user clicks this button, it updates an input field of type "number" and sends a request to update the database using Axios along with Laravel's createOrUpdate me ...

Utilize an object model property as an array key for filtering in the AngularJS and MEANJS framework

I am currently working on a MEANJS application that includes a CRUD module for countries. When I select a country from the default list view, it redirects me to the view-country.client.view page. In order to enhance my model, I have introduced a field name ...

Adjust the transparency of an image within an overlay when clicked on the overlay

Having an overlay on a Google map with two images contained within it, I am looking to adjust the opacity of the images when a user clicks on the overlay or on top of an image within the overlay. I attempted to utilize domlistener, but it did not yield the ...

Exploring the possibilities for dynamic functionality in Vue

Hey there! I have a method that adjusts the state of Vuex by either adding or subtracting from it. The main purpose of this method is to manipulate the state in Vuex. setStep(state, type) { state.currentStep++; } However, I need to be able to pass the ...

Ending a tag every x cycle of iteration within Svelte

I am currently developing an application that requires Svelte to close the Row and start a new one every time there are 3 bookmarks. The components I am using are from sveltestrap, including tags such as "Row", "Col", and "Card". <Row> {#each boo ...