移動端自適應的第一步,是在 `
` 中設置 viewport:<meta name="viewport"
content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
| 屬性 | 說明 | 推薦值 |
|---|---|---|
width | 視口寬度 | device-width (設備寬度) |
initial-scale | 初始縮放比例 | 1.0 (1:1 像素) |
minimum-scale | 最小縮放 | 1.0 (通常禁止縮小) |
maximum-scale | 最大縮放 | 1.0 (禁止放大,或 5.0 允許放大) |
user-scalable | 用戶是否可縮放 | no (App 風格) / yes (無障礙) |
<script>
// 設計稿寬度 (如 750px)
const DESIGN_WIDTH = 750;
function setRem() {
const html = document.documentElement;
const width = html.clientWidth;
// 限制最大最小寬度
const clampedWidth = Math.max(320, Math.min(width, 750));
html.style.fontSize = clampedWidth / DESIGN_WIDTH * 100 + 'px';
}
setRem();
window.addEventListener('resize', setRem);
window.addEventListener('pageshow', e => e.persisted && setRem()); // bfcache 恢復
</script>
CSS 中使用 rem:.title { font-size: 0.32rem; } (對應 32px 設計稿)
/* 設計稿 750px:1vw = 7.5px */
.title { font-size: 4.267vw; } /* 32px */
.container { width: 100vw; padding: 2.667vw; } /* 20px */
.button { height: 10.667vw; } /* 80px */
postcss-px-to-viewport) 自動轉換 px@media (max-width: 374px) { html { font-size: 14px; } }
@media (min-width: 375px) and (max-width: 413px) { html { font-size: 16px; } }
@media (min-width: 414px) { html { font-size: 18px; } }
img { max-width: 100%; height: auto; },配合 `@supports (padding: max(0px)) {
.container {
padding-left: max(16px, env(safe-area-inset-left));
padding-right: max(16px, env(safe-area-inset-right));
padding-bottom: max(16px, env(safe-area-inset-bottom));
}
}
<!-- 1. 禁止雙擊縮放 -->
<meta name="viewport" content="..., user-scalable=no">
<!-- 2. 禁止輸入框聚焦時頁面縮放 (字體≥16px 可避免) -->
<style> input, select, textarea { font-size: 16px; } </style>
<!-- 3. 彈性佈局修正 100vh 包含底部工具欄問題 -->
<style>
.fullscreen { height: 100vh; height: -webkit-fill-available; }
@supports (height: 100dvh) { .fullscreen { height: 100dvh; } } /* 新標準 */
</style>
<!-- 4. 防止橫屏時字體放大 -->
<style> html { -webkit-text-size-adjust: 100%; } </style>
<!DOCTYPE html>
<html lang="zh-Hans">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, viewport-fit=cover">
<meta name="format-detection" content="telephone=no">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
<style>
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html { font-size: 16px; -webkit-text-size-adjust: 100%; }
body { margin: 0; font-family: -apple-system, BlinkMacSystemFont, "Helvetica Neue", sans-serif; }
img, video { max-width: 100%; height: auto; display: block; }
input, textarea, select, button { font: inherit; }
@supports (padding: max(0px)) {
.safe-area { padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left); }
}
</style>
<title>移動端頁面</title>
</head>
<body>
<div class="safe-area">內容</div>
<script>/* 業務代碼 */</script>
</body>
</html>
移動端適配的核心是**確立設計稿基準、選擇適配方案、統一單位體系、處理安全區域**。現代項目首選 VW/VH + PostCSS 或 Rem + 少量 JS,配合 Flex/Grid 佈局,可覆蓋 99% 場景。