iOS Safari: Full-width header with fixed position exceeding viewport width

Encountered an issue specific to Safari on iOS.

Creating a page with a fixed position header that spans the width of the viewport. The content consists of multiple images that should scroll horizontally while the header remains in place during scrolling.

In iOS Safari, the fixed header appears slightly larger than the viewport and scrolls at a different speed compared to the rest of the content.

Tried simplifying the code provided below but still unable to resolve the problem - works fine on all other browsers except for iOS (targeting IE8+).

Example showcasing the issue can be accessed here.

Appreciate any advice or assistance.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>test</title> 
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
<style>
html {
    font-size: 10px;
    height:100%;
    white-space: nowrap;
}
body {
    height:100%;
    padding:0;
    margin:0;
}
#dgs2 {
    height:75%;
    display:inline-block;
}
img{
    height: 100%;
}
#pad{
    height:6em;
    padding-bottom:1px;
}
#header{
    position:fixed;
    width:100%;
    height:6em;
    border-bottom:1px solid;
}
.menuRight{
    float:right;
}
</style>
</head>
<body>
    <div id="header">
        <div class="menuRight"><h2>Menu</h2></div>
        <h1>Testing scroll on iPhone</h1>
    </div>
    <div id="pad"></div>
    <div id="dgs2">
        <img src='img/red.png'/><img src='img/blue.png'/><img src='img/red.png'/><img src='img/blue.png'/><img src='img/red.png'/><img src='img/blue.png'/><img src='img/red.png'/><img src='img/blue.png'/>
    </div>
</body>
</html>

Answer №1

Although this question was asked a year ago, the problem still persists in iOS. I encountered the exact same issue with fixed elements drifting and it was extremely frustrating. The solution is actually quite straightforward: Simply wrap the div #bgs2 (or any element with "white-space:nowrap" on it) with a div called .wrapper (the class name doesn't really matter), and set the overflow to auto:

.wrapper {
    overflow: auto;
    -webkit-overflow-scrolling: touch;
}

I also included webkit-overflow-scrolling to help prevent repaints.

In the future, someone else may come across this strange issue, so hopefully this explanation proves helpful.

Answer №2

If you prefer using jQuery for this task, you can implement the following code:

$(window).ready(function() {
  var bodyWidth = $(window).width();
  $("#header").width(bodyWidth).css('width', bodyWidth);
});

In order to ensure compatibility with all browsers, I have included both the attribute width and the css width. Additionally, updating your meta tag as shown below can also be beneficial:

<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />

Update 1:

Apologies for my oversight in interpreting the CSS styles for the header. The correct styles for the header should be:

#header{
    position:fixed;
    top: 0; // Set these positioning attributes 
    left: 0; // to keep the header at the top left.
    width:100%;
    height:6em;
    border-bottom:1px solid;
}

Update 2:

Another CSS update is required. I observed that your current code includes the following:

html {
    font-size: 10px;
    height:100%;
    white-space: nowrap;
}
body {
    height:100%;
    padding:0;
    margin:0;
}
#dgs2 {
    height:75%;
    display:inline-block;
}

You should modify it to resemble the following:

html {
    font-size: 10px;
    height:100%;
}
body {
    height:100%;
    padding:0;
    margin:0;
}
#dgs2 {
    height:75%;
    display:inline-block;
    white-space: nowrap;
}

To finalize the fix, make sure to move white-space: nowrap; to the #dsg2 div instead of assigning it to the html. Additionally, include the following snippet in your jQuery code:

$(window).resize(function() {
  var bodyWidth = $(window).width();
  $("#header").width(bodyWidth).css('width', bodyWidth);
});

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

How to add data to Laravel after successful ajax request

I have a div containing bids: @foreach($bids as $b) <div class="bids notice notice-lg"> <strong>{{$b->user->name}}</strong> is offering <span style="font-size: 18px" class="label label-success">{{$b->bid}} €</span ...

What steps should I follow to make a sticker adhere to a div?

I'm currently in the process of designing a sticker that attaches to the top of a div, much like this: https://i.sstatic.net/aG329.png <div className="upgrade-premium"> <div className="upgrade-team"> <h1>Prem ...

Is there a way for me to manage the appearance of the printed document's layout?

I have successfully added 3 print buttons to my website, each one designed to print a specific portion (div) of the webpage. Here is the code snippet for this functionality: function printPage(id) { var html="<html>"; //html+="<link rel="st ...

Dealing with checked input type='checkbox' in React - A guide

Having a set of checkboxes, some already checked and some to be updated by the user. The issue here is that while the checkboxes render correctly initially, they do not change upon clicking. The 'checked' value does get updated when onChange is t ...

Is it possible to have Mobile WebKit store the click position?

I am in the process of developing a unique framework specifically designed for WebKit devices. One of the features I have implemented is a series of list views that activate a 'hover' class when users touch them. Below is the jQuery/JavaScript co ...

What is the best way to modify an image in a column when I hover over a table row that was dynamically inserted using a button function?

Is there a way to dynamically change an image in a column when hovering over a table row that was added through a button function? Here is the current code that I am using, but it does not seem to work as intended when I hover over the row. This function ...

Adding a space after a comma automatically upon saving changes in VSCode

Whenever I input code (t,s) into the system and make changes to it, it automatically transforms into (t, s) with an added space after the comma. Is there a way to avoid VScode from adding this extra space on its own? ...

Swiftly accessing information from the internet

Can someone please guide me on how to retrieve data from a website using Swift? My goal is to be able to update the content on the website without having to make changes to the entire application. I believe I just need to fetch text from a server (and I&ap ...

Tips for aligning text in the middle of a customizable and adjustable button with the use of only Bootstrap 4

I am encountering an issue with button text alignment. After customizing the width and height of a button, I noticed that its text does not remain centered on certain screen sizes while in debugging mode. Html <!doctype html> <html lang="en> ...

Styling does not affect an Angular component that is injected into an iframe

I am facing an issue with styling in an iframe when trying to append my own angular component. Despite various attempts, I have been unsuccessful in getting the styling to apply to the component within the iframe. Upon loading the iframe, I use JavaScript ...

PHP: the images uploaded are not located within the designated folder

Having trouble uploading multiple images to a folder and saving them in the database? The images are not moving to the folder, even though the code works on localhost. Here is the code snippet: var abc = 0; // Declaring and defining global incremen ...

Is it possible for the content of the HTML Select to vary from the options displayed in the 'drop-down' menu that the user chooses from?

When a user clicks on a dropdown menu, I want them to see the full state names in the options like this. <select name="address_region" id="address_region"> <option value=1>Alabama</option> <option value=2&g ...

Smooth transitions between points in animations using the D3 easing function

I'm working on an animated line chart that displays data related to play activities. I've been experimenting with changing the animation style between data points on the chart using d3.ease Currently, my code looks like this: path .attr("stro ...

What is the best way to display content next to an image, as well as below the image once it finishes?

Currently, I am working on customizing my blog posts in WordPress. My goal is to have the content of each post displayed next to the post thumbnail, and once the image ends, the content should seamlessly flow underneath it from the left side. I have imple ...

"Step-by-step guide for incorporating a right-to-left (RTL) Bootstrap

Is there a way to make my bootstrap navbar right-to-left (RTL)? I want the logo to be on the right and the links to flow from right to left. I've tried various tricks but none of them seem to work. Here's the code I currently have: <nav class ...

The variability of jQuery values

I have created a website control with jQuery that features a long bar consisting of four colored divs. Users have the ability to drag the join between two divs in order to resize it, all within the confines of the bar itself (keeping the overall width of t ...

Issue with Jenkins displaying parameter values

Objective: Establish a conditional string parameter that spans multiple lines Method: I have set up two different jenkins parameters. Active choices parameter Active choices reactive reference parameter(Choice type: For ...

I am experiencing a CSS display issue on the mobile version of my map leaflet where only the header and footer are visible

I am experiencing a display issue on Android and iPhone due to conflicting css commands. Here's what's happening: I am using React + React Leaflet with a header, main, and footer all set to width 100% within a parent div with body set to width an ...

I am in need of a customized 'container' template that will display MyComponent based on a specific condition known as 'externalCondition'. MyComponent includes the usage of a Form and formValidation functionalities

container.html <div ngIf="externalCondition"> <!--Initially this is false. Later became true --!> <my-component #MyComponentElem > </my-component> <button [disabled]= "!myComponentElemRef.myDetailsF ...

Trouble getting CSS and JavaScript to function properly with HTTPS?

It seems that the css and js files are functioning properly on , but not on . As a novice in web design and javascript, I am struggling to find a solution to this issue. Trying to link the CSS file with "//isdhh.org/css/style.css" has not resolved the pr ...