Tips for ensuring a static html element remains visible at the bottom of the screen even when the soft keyboard is opened in iOS Safari

Within a webpage, there is an input field and a fixed div positioned at the bottom of the window using CSS properties such as position:fixed; and bottom:0;.

To better illustrate this setup, I have created a Codepen demo which can be viewed here: https://codepen.io/anon/pen/xpQWbb/

When viewed on Chrome for Android, the fixed div remains visible even when the soft keyboard is open:

https://i.stack.imgur.com/6up4x.png

However, when accessed through Safari on iOS, it appears that the soft keyboard covers the fixed element:

https://i.stack.imgur.com/aa5o3.png

(Please note that testing is being done on the iOS simulator on a Macbook, as there is no working iPhone available for testing)

Is there a method to ensure that iOS Safari behaves like Chrome and keeps the element visible even with the soft keyboard open?

Answer №1

Encountering a challenge while developing a chat input that needed to stay fixed at the bottom of the page, I faced an issue with the iOS keyboard overlapping the input field. Determining the exact height of the keyboard proved to be quite tricky. In my quest for a reliable value to use for positioning the chat input container above the keyboard, I sought to find the current "innerHeight" value, representing the visible area of the webpage. Due to the behavior of the iOS keyboard, it seemed that the only way to obtain this value with the keyboard open was by scrolling to the very bottom of the page and taking a sample of "window.innerHeight".

To tackle this problem, I set up an event listener on the input field triggered by a 'click' event (as using 'focus' caused issues). This action opened the keyboard, which took some time. Therefore, I implemented a timeout of 1000ms to ensure that the keyboard was fully open. After the timeout, I scrolled quickly to the bottom of the page using JavaScript, recorded the value of "window.innerHeight" in that state, and then returned to my original position. This method allowed me to capture the actual height of the visible screen area.

It appeared that the browser window remained placed behind the keyboard until reaching the bottom, at which point the whole window would 'scroll up', aligning the bottom with the top of the keyboard view.

With the obtained value, I calculated where to position the chat input by adding the currently scrolled value (window.scrollY) with the saved value and subtracting the height of my absolutely positioned element. To prevent flickering during scrolling, I also chose to hide the input field. One drawback of this approach was a quick flicker of the page when measuring at the bottom.

I encountered difficulty in determining the variable height of the address bar but opted to make the input slightly taller than necessary to accommodate any potential padding required.

Answer №2

Take a look at this discussion, which suggests a workaround that might be more practical from a coding perspective. Essentially, it involves using the height of the keyboard to adjust the content visibility. Although somewhat unconventional, pinpointing the exact height of the keyboard on different devices can be a challenge.

Unfortunately, the iOS Safari keyboard is not included in the browser viewport, making it impossible to reference like standard elements.

@Bhimbim's solution could also be worth trying out.

Best regards, -B

Answer №3

I've encountered this issue in the past and here's what worked for me:

  1. Set up a listener to detect keyboard input.
  2. Adjust the height of your webview when the keyboard appears by calculating the difference between the screen height and the keyboard height.
  3. Make sure your HTML is responsive to make this workaround effective.

If you're interested, I can provide more code related to IOS for implementing this solution. Thank you

Hi there, apologies for the confusion earlier. If you're still looking to manage the keyboard through listeners, I have a workaround that might help. It may not be perfect but it's worth a try:

  1. Add a listener on your webpage to detect when the keyboard shows up, possibly using jQuery events like onkeyup or onfocus on your text fields.
  2. Use this information to determine when the keyboard is active and adjust your layout accordingly with JavaScript.

Hopefully, this insight helps you out. Thank you @Beaniie!

Hello Andreyu! You are correct about the difficulty in determining the keyboard height unlike in the case of WebView where I could easily access it through IOS code. Here's another workaround that might be helpful. Although not ideal, it involves comparing screen sizes from various IOS devices to estimate the keyboard height. Good luck!

Answer №4

I have come up with a solution where myBottomDiv, which is a fixed bottom div for chat with an input, needs to be made absolute and its position changed every time the page moves on Safari for iOS. I am hopeful that Safari will introduce meta interactive-widget=resizes-content feature similar to Chrome in the future.

// Specifically tailored for Safari on iOS
// (use interactive-widget=resizes-content to resolve issues in Chrome)
if (/iPad|iPhone|iPod/.test(navigator.userAgent)) {
    if (navigator.userAgent.indexOf('Chrome') === -1 && navigator.userAgent.indexOf('Safari') > -1) {

        // Set body position to relative
        document.body.style.position = 'relative';
        let marginTop = parseInt(window.getComputedStyle(document.body).marginTop);
    
        // Adjust myBottomDiv positioning (a div containing an input for chat)
        myBottomDiv.style.position = 'absolute';
    
        // Event listeners (using touchmove over scroll event for better performance on mobile)
        window.addEventListener("scroll", resizeHandler);
        window.addEventListener("touchmove", resizeHandler);
        window.visualViewport.addEventListener("resize", resizeHandler);
    
        function resizeHandler() {
            myBottomDiv.style.top = (window.scrollY +  window.visualViewport.height - marginTop - myBottomDiv.offsetHeight) + 'px';
        }
    }
}

Answer №5

To optimize the display, consider using position:absolute and height:100% for the entire page.

When the keyboard appears, it is placed over the app content, which can be managed by embedding both the keyboard and objects within a UIScrollView object or its subclass like UITableView. Keep in mind that UITableViewController will automatically adjust its table view when text fields are being edited inline.

During keyboard display, you can reset the scroll view's content area and move the desired text object into position. In response to a UIKeyboardDidShowNotification, your handler method should:

1. Retrieve the keyboard's size.

2. Modify the bottom content inset of the scroll view to accommodate the keyboard height.

3. Scroll the target text field into view.

For further details, refer to Apple's developer guidelines:https://developer.apple.com/library/content/documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/KeyboardManagement/KeyboardManagement.html

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

Refresh a DIV using two SQL queries in forms

I am encountering an issue with updating a single div element using the results from two different forms on an HTML page. I want either form1 or form2 to display results in the same div element, and it should be updated with one line of content fetched fro ...

Refreshing an HTML table using instance variables from a C# class (utilizing jQuery and AJAX)

Explore <script type="text/javascript"> $(document).ready(function () { $("#viewDetails").click(function () { $.ajax( { type: "POST", url: '@Url.Action("GetDetail", "ControllerName")' ...

The feature of option display is not supported on Safari browser

I am facing an issue with the functionality of two dropdown menus. The options in the second dropdown are supposed to be shown based on the selection made in the first dropdown. While this feature works perfectly in Chrome, it seems to be malfunctioning i ...

The component's width appears to be constrained by the use of display flex

I am currently working on a reactjs application that includes a header and another component. Initially, everything looks fine, but when I apply display: flex to their div, it seems to reduce the width of the header. Here are some images showing the issue: ...

Discover the ins and outs of the "DOM" within a string, treating it as HTML in AngularJS

Looking to extract data from a legal HTML string based on tags and attributes, but want to avoid using jQuery in favor of Angular. Are there built-in functions in Angular for this task? ...

How to position child divs at the center of a parent div

I am looking to set the maximum width of the "circlecontainer" to 300px, but I also want my 4 child divs (each with a max width of 300px) to remain centered in the browser when it exceeds a width of 1200px. Currently, they are aligned to the left side. Be ...

Can you explain the functionality of the combination selector "AB," where "A" and "B" represent any selectors?

I've been delving into the concept of the "combination selector" by referring to resources on MDN. This selector involves selecting elements that match both criteria A and B simultaneously when they are placed together. Can someone shed some light on ...

Conditional formatting for form field styles

Is there a way in Angular to conditionally render a material style? For instance, I am looking to apply the following style only to my Password form field text, and only when both input boxes have content: .mat-form-field-appearance-outline .mat-form-fiel ...

Seeking assistance with implementing Styles using the .css() function in Jquery. Any guidance is appreciated

This particular style is exactly what I am looking for. body{ background: url("img/Background_Home.jpg") no-repeat center center fixed; -webkit-background-size: cover; -moz-background-size: cover; ...

Browsing through dual snap points simultaneously on Google Chrome

I'm currently developing a website that incorporates scroll snapping for 3 viewport-sized <section> elements set up like so: html, body { scroll-behavior: smooth; scroll-snap-type: mandatory; scroll-snap-points-y: repeat(100vh); scrol ...

In situations where there may be a duplicate, what alternative can I utilize in place of the id attribute?

I understand that almost any element in the DOM can have an "id" attribute, and I've used it to track each client in a table of clients. Although ids should not be repeated, my rows are assigned unique identifiers based on each person's "clientId ...

Is there a way to position the text to the right and the image to the left?

I have written the following code: <!DOCTYPE html> <html> <head> <title>UMass Boston Computer Science Club About page</title> <link href='https://fonts.googleapis.com/css?family=Chakra Petch' rel='sty ...

To enhance user experience, consider incorporating a 'Next Page' feature after the completion of every four paragraphs,

Here is a code snippet that can be used to print 'n' number of paragraphs: <% while(rs.next()){ String para=rs.getString("poems"); %> <p> <%=para%> </p> <!-- n number of p tags are printe ...

Aligning Description Item components horizontally in antdLearn how to easily horizontally align Description

Currently, I am utilizing the `antd` Description components. In this scenario, when there is no `title` for the items, the value should be aligned to the left. You can see an example of this alignment in the image below: I have attempted to fix this issu ...

Having difficulty selecting a cloned div using jQuery

I am in the process of creating a dynamic menu that switches out sub-menus. <nav id="main-menu"> <div id="categories"> <a id="snacks" class="categ">Snacks &amp; Sweets</a> <a id="pantry" class="categ"> ...

Toggle between list elements using the inner text of the elements with jQuery

My issue lies in figuring out why my filter function isn't properly working for toggling li elements. JavaScript: $("#searchbox1").on("keyup", function() { var value = $(this).val().toLowerCase(); $("#menulist li") ...

Shifting and implementing various styles across numerous elements at the same time

There is an anchor tag containing two spans... <a class="banner-logo" href="/search"><span id="banner-logo-hello">Hello</span><span id="banner-logo-world">World</span></a> When hovering over the anchor tag, I want to c ...

Problem with Labels Overlapping and Blocking Text Fields

My issue is that when I interact with the text field, the label "Type your New Task here" moves up and gets covered by what looks like a white overlay. How can I fix this so that the label stays visible at all times? Prior to engagement: https://i.stack.i ...

Having trouble retrieving POST data with NodeJS/Express and an HTML form?

I have encountered an issue in my application where I am unable to extract the posted data from req after performing an action pointing to a nodejs endpoint. Despite successfully executing the action, when attempting to access the posted data from req, I a ...

How to Retrieve Data from an HTML Table using the Laravel Framework

I have a unique and interactive Dynamic Sortable Table where I can effortlessly add or delete rows likethis uploaded image here. There is an intriguing loop in my controller that runs precisely 4 times, based on the number of columns in the table. However, ...