Skip to content

fix(layout): add support for iOS safe area#817

Open
WarningImHack3r wants to merge 12 commits intosveltejs:mainfrom
WarningImHack3r:safari-safe-area
Open

fix(layout): add support for iOS safe area#817
WarningImHack3r wants to merge 12 commits intosveltejs:mainfrom
WarningImHack3r:safari-safe-area

Conversation

@WarningImHack3r
Copy link
Contributor

@WarningImHack3r WarningImHack3r commented Nov 6, 2024

Add support for iOS' safe area environment variables to prevent the navbar from being hidden behind the iPhone's home bar.

It works using the official Webkit guide, which was originally made after the introduction of the iPhone X with iOS 11.
It exposes the env() CSS function with 4 variables (safe-area-inset-{top,left,right,bottom}), requiring viewport-fit=cover in the viewport meta tag to get rid of natural viewport margins (edit: apparently not needed from my testing, removed it - probably only required for the left/right variants? needed for iOS 26.1+ PWAs!).

These environment variables are smartly populated by the system based on the current Safari layout, giving them a value granularly as needed.

Note

You might note my PR doesn't include the outdated constant() function, the previous name of env() between iOS 11.0 and 11.2 beta versions: the already low range of concerned versions combined with the ancient age of this major iOS version isn’t worth the effort, in my opinion.
It can, however, be easily added at will with @supports queries or CSS rules fallbacks after bypassing calc() calls.

On desktop, the env() function is (supposedly) undefined, invalidating the CSS lines using it, leading to no repercussions on desktop browsers or other mobile browsers. I've tested it on Arc / Chrome desktop in responsive mode and noticed no impact.

Demo

before after
Before (old Safari layout/in-app browser) After (old Safari layout/in-app browser)
after old UI regular after new UI
After (old Safari layout/in-app browser - "unscrolled" state) After (new Safari layout - not impacted)

Before submitting the PR, please make sure you do the following

  • It's really useful if your PR references an issue where it is discussed ahead of time.
  • Prefix your PR title with feat:, fix:, chore:, or docs:.
  • This message body should clearly illustrate what problems it solves.

@vercel
Copy link

vercel bot commented Nov 6, 2024

@WarningImHack3r is attempting to deploy a commit to the Svelte Team on Vercel.

A member of the Team first needs to authorize it.

@vercel
Copy link

vercel bot commented Jan 13, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
svelte-dev Ready Ready Preview Feb 24, 2026 10:14am

Request Review

Copy link
Member

@teemingc teemingc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested the changes on an iPhone 16 with iOS26 by adding the site to the home screen as a web app. Sadly, it doesn’t work there. Can you verify this too?

@WarningImHack3r
Copy link
Contributor Author

WarningImHack3r commented Feb 26, 2026

Fixed thanks to this forum answer (for reference)

current and pwa
Left: unchanged iOS 18 inside Safari / Right: iOS 26.2 PWA

And while we're at it, full iOS 26 showcase (unaffected by this last patch):

CleanShot 2026-02-26 at 23 40 31@2x CleanShot 2026-02-26 at 23 40 46@2x
Safari 26 (large) Safari 26 (compact)

@WarningImHack3r WarningImHack3r changed the title feat: Add support for iOS safe area fix(layout): add support for iOS safe area Feb 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants