🧩 SKILL.md · 前端设计与交互 · MIT
移动端与 iOS Safari 横向超屏、软键盘遮挡与安全区体验精修
使用真实 WebKit 调试命令与 CSS 模式,系统性修复 iOS Safari 横向超屏、100vh/100dvh 视口跳动、安全区适配、软键盘遮挡与 300ms 点击/缩放触控体验问题。
✓ Claude Code✓ Cursor✓ Codex✓ Antigravity
⚡ 选择你的 AI 编程工具 · 一行终端命令直接安装
mkdir -p ~/.claude/skills/mobile-safari-viewport-touch-polisher && curl -fsSL https://ai.yevrix.com/skills/mobile-safari-viewport-touch-polisher/SKILL.md -o ~/.claude/skills/mobile-safari-viewport-touch-polisher/SKILL.md
🛡️ 版权与原创声明:本技能规范由 Yevrix AI 独立原创编写,遵循 MIT 开源协议发布,您可免费用于个人或商业研发团队并自由二次修改。
---
name: mobile-safari-viewport-touch-polisher
description: Diagnose and fix iOS Safari horizontal overflow, 100vh/100dvh viewport jumps, safe-area insets, soft-keyboard occlusion, and touch ergonomics (300ms tap delay, double-tap zoom, tap highlight, overscroll). Use when a mobile web app overflows horizontally, jumps when the URL bar collapses, hides inputs behind the keyboard, or feels laggy/un-tappable on iPhone/iPad.
---
# iOS Safari Viewport Overflow, 100dvh, Safe-Area & Touch Ergonomics Polisher
## When to Trigger
- A page scrolls sideways on iPhone (a single element wider than the viewport, or `100vw` + scrollbar math).
- Layout jumps when Safari's dynamic toolbar collapses/expands; `100vh` is taller than the visible area.
- Bottom nav / sticky CTA sits under the home indicator or is clipped by the notch in landscape.
- Tapping a button requires two taps, or a double-tap zooms the page unexpectedly.
- Focusing an `<input>` pushes content off-screen or the keyboard covers the submit button.
- `position: fixed` elements drift during momentum scroll or when the keyboard opens.
## Step 1 — Reproduce & Capture the Real Viewport
Use real WebKit, not Chrome mobile emulation. Drive Safari via `safaridriver` and inspect computed geometry.
```bash
# Enable the WebKit driver (macOS)
safaridriver --enable
# Serve your build locally
npx serve -l 4173 ./dist
# Open the page in iOS Simulator Safari
xcrun simctl boot "iPhone 15 Pro" 2>/dev/null || true
open -a Simulator
xcrun simctl openurl booted "http://localhost:4173"
```
In Safari Web Inspector (Develop → Simulator → your page), run in the console to find the overflow culprit:
```js
// List every element wider than the viewport
const vw = document.documentElement.clientWidth;
[...document.querySelectorAll('*')]
.map(el => ({ el, r: el.getBoundingClientRect() }))
.filter(({ r }) => r.right > vw + 1 || r.left < -1)
.forEach(({ el, r }) =>
console.log(el.tagName, el.className, 'left', r.left.toFixed(1), 'right', r.right.toFixed(1)));
```
Also log the three viewport heights to confirm the `vh` bug:
```js
console.log({
innerHeight: window.innerHeight,
visualViewport: window.visualViewport?.height,
cssVh: document.documentElement.clientHeight,
});
```
## Step 2 — Kill Horizontal Overflow at the Root
Most iOS overflow comes from `100vw` (includes scrollbar gutter on desktop but not iOS), negative margins, long unbreakable strings, and `min-width: auto` on flex/grid children.
```css
/* Global guard — do NOT use overflow-x: hidden on <body> alone; it breaks position: sticky */
html, body { max-width: 100%; overflow-x: clip; } /* clip > hidden: keeps sticky working */
/* Flex/grid children default to min-width:auto and refuse to shrink */
.flex-child { min-width: 0; }
/* Long tokens (URLs, hashes, emails) */
.breakable { overflow-wrap: anywhere; word-break: break-word; }
/* Replace 100vw with 100% of the containing block */
.full-bleed { width: 100%; } /* not 100vw */
```
Find offenders with ripgrep before touching CSS:
```bash
rg -n --glob '*.{css,scss,tsx,jsx,vue,svelte}' '100vw|width:\s*calc\(100vw|margin-(left|right):\s*-' src/
```
## Step 3 — Fix Viewport Height & Safe Areas
`100vh` on iOS equals the *large* viewport (toolbar hidden), so content is cut off when the toolbar is visible. Use `100dvh` with a `100vh` fallback, and pad for the notch/home indicator with `env(safe-area-inset-*)`.
```html
<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover" />
```
```css
:root { --safe-b: env(safe-area-inset-bottom, 0px); }
/* Full-height app shell */
.app {
min-height: 100vh; /* fallback for old WebKit */
min-height: 100dvh; /* dynamic viewport: tracks toolbar */
}
/* Bottom nav clears the home indicator */
.bottom-nav {
position: fixed; inset: auto 0 0 0;
padding-bottom: calc(8px + var(--safe-b));
}
/* Sticky footer CTA in a scroll container */
.cta { padding-bottom: calc(12px + var(--safe-b)); }
```
`viewport-fit=cover` is mandatory — without it `env(safe-area-inset-*)` resolves to `0px`.
## Step 4 — Soft Keyboard, Fixed Positioning & Touch Ergonomics
**Keyboard occlusion:** iOS does not resize the layout viewport when the keyboard opens; it shrinks `visualViewport`. Anchor inputs to the visual viewport.
```js
const vv = window.visualViewport;
if (vv) {
const sync = () => {
// Distance the keyboard pushes the visual viewport up
const offset = window.innerHeight - vv.height - vv.offsetTop;
document.documentElement.style.setProperty('--kb', `${Math.max(0, offset)}px`);
};
vv.addEventListener('resize', sync);
vv.addEventListener('scroll', sync);
sync();
}
```
```css
.chat-input { transform: translateY(calc(-1 * var(--kb, 0px))); }
```
**Touch ergonomics:**
```css
/* Remove the 300ms double-tap-zoom delay on interactive elements */
button, a, [role="button"] { touch-action: manipulation; }
/* Kill the grey tap flash, keep :active feedback */
* { -webkit-tap-highlight-color: transparent; }
.btn:active { opacity: .7; }
/* Prevent rubber-band scroll chaining on modals */
.modal-body { overscroll-behavior: contain; }
/* Inputs < 16px trigger auto-zoom on focus — force 16px */
input, select, textarea { font-size: max(16px, 1rem); }
```
## High-Risk Anti-Patterns (Bad vs Good)
**1. `100vh` for full-screen layouts**
```css
/* BAD: taller than visible area; footer hidden behind toolbar */
.hero { height: 100vh; }
/* GOOD */
.hero { min-height: 100vh; min-height: 100dvh; }
```
**2. `overflow-x: hidden` on body to mask overflow**
```css
/* BAD: silently breaks position: sticky and hides the real bug */
body { overflow-x: hidden; }
/* GOOD: clip + fix the offending child */
html, body { overflow-x: clip; }
.flex-child { min-width: 0; }
```
**3. Missing `viewport-fit=cover`**
```html
<!-- BAD: env(safe-area-inset-*) is always 0 -->
<meta name="viewport" content="width=device-width, initial-scale=1" />
<!-- GOOD -->
<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover" />
```
**4. `position: fixed` footer without safe-area padding**
```css
/* BAD: CTA sits under the home indicator */
.footer { position: fixed; bottom: 0; }
/* GOOD */
.footer { position: fixed; bottom: 0; padding-bottom: calc(12px + env(safe-area-inset-bottom, 0px)); }
```
**5. Inputs with `font-size: 14px`**
```css
/* BAD: iOS auto-zooms on focus, breaking layout */
input { font-size: 14px; }
/* GOOD */
input { font-size: 16px; }
```
**6. Listening to `window.resize` for keyboard**
```js
// BAD: iOS does not fire resize for the keyboard
window.addEventListener('resize', onKeyboard);
// GOOD
window.visualViewport?.addEventListener('resize', onKeyboard);
```
## Verification Gate Checklist
- [ ] `rg '100vw'` returns zero hits in layout CSS (or each hit is justified).
- [ ] Console overflow scan (Step 1) reports no element with `right > clientWidth + 1`.
- [ ] `viewport-fit=cover` present in the `<meta name="viewport">` tag.
- [ ] `100dvh` used with a `100vh` fallback for full-height shells.
- [ ] Every fixed/sticky bottom element has `padding-bottom: calc(... + env(safe-area-inset-bottom))`.
- [ ] All form controls have `font-size >= 16px`.
- [ ] Interactive elements set `touch-action: manipulation`.
- [ ] Keyboard handling uses `visualViewport` events, not `window.resize`.
- [ ] Tested in iOS Simulator (portrait + landscape) and on a physical device with notch.
- [ ] `npx tsc --noEmit` and `npx stylelint '**/*.css'` pass after edits.
开发者生态推荐
推荐云主机与 AI 算力基础设施
精选适合部署独立站、自动化脚本与大模型工作流的云底座;通过推广链接注册不增加任何费用,同时支持本站永久免费。
📬 每周精选 · 零垃圾邮件
订阅 Yevrix 独立开发者周刊
每周一封邮件:最新上线免费本地工具、云主机/AI API 实测对比、原创 Agent Skill 与 GitHub 飙升项目精选,随时一键退订。