When you open a link from a post in X (formerly Twitter) on an iPhone, you may see an unexpectedly large gap at the top of the page. I ran into this on pages that use viewport-fit=cover and env(safe-area-inset-top) to keep content clear of the status bar and Dynamic Island.

I saw the gap on my Tokyo public bath map and portfolio site, then traced it on a physical iPhone. In the version of X I tested, the web viewport already started below the status bar, yet env(safe-area-inset-top) still returned 54px. Adding that inset in CSS counted the top space twice. Setting a CSS custom property to 0px only in X on iPhone removed the extra gap and kept the intended 8px of padding.

Why the Gap Appears

To extend a page background to the edges of the screen in iOS Safari, a common approach is to set <meta name="viewport" content="..., viewport-fit=cover"> and add env(safe-area-inset-top) to the padding of any UI at the top. For example, a toolbar that needs 8px of breathing room can use calc(8px + env(safe-area-inset-top, 0px)). When the viewport starts at the top of the screen, as it can in Safari, this leaves 8px below the status bar.

In the X in-app browser I tested, the web viewport’s top: 0 was already below the status bar, where the clock appears. Even so, the computed value of env(safe-area-inset-top) was 54px on my iPhone 17 running iOS 27.2 and X 12.40.

The web measurements don’t show how X positions its native view or where that 54px value comes from. They do show what happens on the page: the first element starts below the status bar, then gets another 54px on top of its intended 8px of padding.

The Fix and Code

Define the inset once as a CSS custom property, --safe-area-top, and override it to 0px in X’s in-app browser on iPhone. Safari, Android browsers, and other WebViews keep using env(safe-area-inset-top, 0px).

:root {
  --safe-area-top: env(safe-area-inset-top, 0px);
}

.toolbar {
  padding-top: calc(8px + var(--safe-area-top));
}

The toolbar’s padding is 8px plus whatever inset the browser reports. With the override in X on iPhone, the padding is exactly 8px.

I used Twitter for iPhone/ to identify the browser. The user agent on my iPhone contained Twitter for iPhone/12.40. The check matches the app identifier without tying the fix to that version number.

On my Next.js portfolio site, I put this component in <head> so it can set the override through the CSSOM before the first paint.

// X for iPhone already insets its web viewport below the status bar, but still
// reports the screen's top safe area (54px on iPhone 17, iOS 27.2 / X 12.40).
// Apply before paint via CSSOM so React's server-rendered markup stays intact.
const script = String.raw`if (/Twitter for iPhone\//.test(navigator.userAgent)) { var sheet = document.getElementById('x-safe-area').sheet; if (sheet) sheet.insertRule(':root{--safe-area-top:0px!important}', 0); }`;

export default function InAppSafeArea() {
  return (
    <>
      <style id="x-safe-area">{":root{}"}</style>
      <script dangerouslySetInnerHTML={{ __html: script }} />
    </>
  );
}

Applying the override in a React useEffect can cause the page to shift as the gap shrinks after the initial render. Here, the synchronous script calls sheet.insertRule() on the stylesheet in <head>. That applies the CSS before the first paint and keeps React’s server-rendered markup intact. The public bath map is a single HTML file, so I put the same synchronous script in its <head> and used --safe-area-top for both the top padding and the controls.

Measurements on an iPhone

I published a diagnostic page with position markers and a switch that toggles the safe-area inset on a sample element. I opened it in X’s in-app browser on the iPhone 17 running iOS 27.2 and X 12.40.

At the initial scroll position of 0, the red position: fixed; top: 0 and green position: absolute; top: 0 markers both appear below the clock. The marker at top: env(safe-area-inset-top) appears farther down.

At scroll position 0 with the safe-area inset enabled, both top: 0 markers sit below the clock, and the sample element has an extra gap above it.
At scroll position 0 with the safe-area inset disabled, the sample element keeps 8px of padding and sits below the status bar.

With the inset disabled and only 8px of padding, the sample stays just below the status bar and clear of the clock and status icons.

I then scrolled down 160px (scroll.y = 160) and captured the DOM rectangles and computed styles as JSON with the inset on and off. In both cases, screen measured 402 × 874. Both innerWidth × innerHeight and visualViewport measured 402 × 691, with offsetTop: 0, pageTop: 160, and scale: 1. The top safe-area inset was 54px, and the right, bottom, and left insets were 0px. The 8px-high probes reported top: 0 for fixed, top: -160 for absolute, and top: 54 for safe.

Measurement at scroll.y = 160Inset onInset offChange
padding-top62px8px-54px
Element top-160px-160px0px
Element bottom-21px-75px-54px
Element height139px85px-54px

At the same scroll position, turning the inset off reduced padding-top from 62px (8px + 54px) to 8px. The element became exactly 54px shorter, and its bottom edge moved up by the same amount.

What I Tested

  • X on a physical iPhone. After deploying the fix, I opened the public bath map and the portfolio site in X’s in-app browser on the iPhone 17 and confirmed that the extra gap was gone.
  • Desktop Chrome reproduction. I checked other sites’ source code and reproduced the iPhone’s user agent, 402 × 691 viewport, and 54px top inset in desktop Chrome. I reproduced the gap on the complemEnter landing page and deployed the same fix there. I also deployed it to the staffing management and monthly shift sites, where I switched the top offsets for bars, menus, and dialogs to the shared custom property. Verification for these three additional sites was limited to Chrome and source review.

Common Questions

Can the Map Extend Behind the Notch?

In the version of X I tested, the web viewport begins below the status bar. Page CSS has no way to draw the map behind the clock, notch, or Dynamic Island in that viewport. The fix removes the extra 54px gap below the status bar.

Why Keep viewport-fit=cover?

I measured the 54px inset with viewport-fit=cover already set. Removing it would affect the edge-to-edge background in browsers such as Safari. Keeping it and adjusting the CSS custom property preserves that layout and removes the extra padding in X.

Why Only Override the Inset in X on iPhone?

In Safari and WebViews that start at the top of the screen, removing the inset can put the toolbar under the status bar. The inset can also vary by device and orientation. I kept env(safe-area-inset-top, 0px) as the default and used 0px only when the user agent contains Twitter for iPhone/.