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

Having trouble with Safari on your iPhone? Seeing image outlines peeking through radius borders?

Has anyone encountered a similar issue before? I'm experiencing this problem specifically on Safari for iPhone. Problem: https://i.sstatic.net/ffqWz.jpg Solution: https://i.sstatic.net/z1bUB.png Below is the HTML code snippet: <div class="row"& ...

Removing the dividing line between columns in an Antd table: A simple step-by-step guide

Do you notice the lines dividing each table column heading in the image? I want to get rid of these lines from the table. The table component I am using is Antd table. https://ant.design/components/table#examples https://i.stack.imgur.com/Npx5G.png ...

Exploring ways to personalize the behavior and style of the material-ui Accordion component

There is a div container in gray with an accordion component containing only one AccordionSummary component. Below that is another div in blue. The initial view looks good (see pic #1). When the accordion is expanded: The AccordionDetails component is dis ...

Defining the active element in the menu using JavaScript

I need help with my navigation menu: <ul> <li id="active"><a href="/">Home</a></li> <li><a href="/about/">About Us</a></li> <li><a href="/services/">Our Services</a>< ...

A critical issue occurred: array length is invalid. The attempt to include EJS in the template failed due to

Currently, I am attempting to loop through my JSON data using EJS from Node/Express. However, I need to insert a different pin into the flexbox when it reaches the 6th iteration. Whenever I try to implement this logic, I encounter a severe error that disr ...

Ways to identify if the text entered in a text area is right-to-left justified

Within a textarea, users can input text in English (or any other left-to-right language) or in a right-to-left language. If the user types in a right-to-left language, they must press Right-shift + ctrl to align the text to the right. However, on modern O ...

Looking to integrate the date, month, and year selection tags into the inline format

The Edit User Profile page includes a Birthday field, which is currently displayed vertically. I am looking to change the layout so that the Birthday field appears horizontally. Here is the code I am using: Within visitors/edit.html.erb, <%= simple_f ...

Save room for text that shows up on its own

I am currently dealing with a situation where text appears conditionally and when it does, it causes the rest of the page to be pushed down. Does anyone know the best way to reserve the space for this text even when it's not visible so that I can pre ...

Guide on triggering CSS @keyframes animations upon element visibility while scrolling

I'm struggling with controlling CSS animations that I want to start only when the element becomes visible on the screen. The issue arises because I have a website with a total height of around 8000px and the element with the animation is located far d ...

Eliminate any unnecessary gaps that may exist between the cards within the deck

Utilizing Jinja2 with Flask to render a list on an HTML page, I aim to produce card decks containing data from the list using a Jinja2 loop. This is my current HTML code: <div class="container-fluid"> <div class="row"> <div clas ...

What causes the fixed div to appear when scrolling horizontally?

I have replicated this issue in a live example: http://jsfiddle.net/pda2yc6s When scrolling vertically, a specific div element sticks to the top. However, if the window is narrower than the wrapper's width and you scroll horizontally, the sticky elem ...

Utilize jQuery to automatically assign numbers to <h1-h6> headings

Looking to develop a JavaScript function that can automatically number headings (h1- h6) for multiple projects without relying on CSS. Currently achieving this with CSS: body { counter-reset: h1; } h1 { counter-reset: h2; } h2 { counter-reset: h3; } ... T ...

Sliding Up Transition Effect for Footer with JQuery or CSS3

I am attempting to replicate a sleek slide up footer that caught my eye on The Internship Movie Site. I have created the wireframe layout, but I am struggling to figure out the correct CSS transition effect. Although I have experimented with the "top" and ...

What is the best way to hide a section of an image using the background of a different div?

How can I make the background of div2 cover the image in div1 when setting margin-top=-50px? Code snippet: <html> <head> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"> ...

arrange a div inside another div

I'm struggling to grasp the concept of how divs function in HTML. Below is a snippet of my HTML code: <div id="user_p"> <img src="img/pp/djbaptou.jpg"> <div class="followings"> djbaptou </br> Baptiste Arnaud </br> ...

How to responsively position and scale a background image for a full-width div

Here is the situation: I have an SVG with dimensions of 1920*1920 pixels () This is what I require: I intend to use it as a background-image for a div that spans the full width of the screen. The background image for the div must be responsive. An essent ...

Show a tweet by its identification number

Is there a way to show a tweet by its unique id? For instance, if I have the id 876126300649525249, can I display the actual tweet on my website? Does anyone know if this is achievable? ...

Tips for sending a value or props to a CSS file

Is there a way to dynamically update the x and y position based on user clicks, and then apply these changes to the top and left properties of a div? .dataCardAbsolute { position: absolute; top: 100px; left: 300px; overflow: auto; } React.useEffec ...

What is the definition of "top of the page" within a PHP document?

One source claimed that ob_start() should be placed at the top of the page, while another insisted that session_start() should take precedence. I also came across a guide stating that header() belongs at the beginning of the page. Additionally, I read th ...

Utilize multiple background images in CSS with a set of three unique images

I have inserted three images into the background of my website's body. Two of them are displaying correctly (one with repeat-y), but the third one is not working. I've tried searching a lot but couldn't find a solution. body { backgr ...