Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> HTML

Ngăn chặn zoom (pinch-to-zoom) trên iPhone Web App bằng HTML Meta Tag

Để ngăn người dùng zoom (pinch-to-zoom, double-tap zoom) trên web app iOS, sử dụng viewport meta tag với user-scalable=no.

Meta Tag chuẩn

<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">

Giải thích các giá trị

Giá trịÝ nghĩa
width=device-widthChiều rộng viewport = chiều rộng màn hình thiết bị
initial-scale=1.0Tỷ lệ zoom ban đầu = 100%
minimum-scale=1.0Zoom tối thiểu = 100% (không zoom out)
maximum-scale=1.0Zoom tối đa = 100% (không zoom in)
user-scalable=noVô hiệu hóa pinch-to-zoom hoàn toàn

⚠️ Cảnh báo quan trọng về Accessibility (WCAG)

Vô hiệu hóa zoom vi phạm WCAG 2.1 Success Criterion 1.4.4 (Resize Text) và 1.4.10 (Reflow). Người dùng khiếm thị cần zoom** để đọc nội dung.

  • iOS 10+ bỏ qua user-scalable=no và maximum-scale nếu user bật "Zoom" trong Accessibility settings.
  • Chrome Android cũng bỏ qua từ phiên bản 2017.
  • Chỉ còn hiệu lực trên iOS cũ hoặc webview app không bật accessibility zoom.

Giải pháp thay thế: Ngăn zoom CHỈ trên input/select (Khuyên dùng)

iOS Safari tự zoom khi focus vào <input>, <select> nếu font-size < 16px. Fix bằng CSS:

input, select, textarea {
  font-size: 16px !important; /* Ngăn auto-zoom trên iOS */
}

/* Hoặc media query chính xác cho iPhone */
@media screen and (max-width: 767px) {
  input, select, textarea {
    font-size: 16px;
  }
}

Ví dụ: Chỉ ngăn zoom trên form, cho zoom trên content

<meta name="viewport" content="width=device-width, initial-scale=1.0"> <!-- Không disable zoom -->

<style>
  /* Chỉ input/select font-size >= 16px */
  @media (max-width: 767px) {
    input, select, textarea { font-size: 16px; }
  }
  
  /* Nếu BẮT BUỘC phải disable zoom cho app game/canvas fullscreen */
  .no-zoom {
    touch-action: manipulation; /* Ngăn double-tap zoom trên touch */
  }
</style>

touch-action: manipulation

Thuộc tính CSS hiện đại để kiểm soát touch behavior:

.canvas-game {
  touch-action: none;        /* Ngăn tất cả gesture (pan, zoom, scroll) */
  /* Hoặc */
  touch-action: manipulation; /* Chỉ cho phép pan/scroll, ngăn double-tap zoom */
}

Tóm tắt khuyến nghị

Loại ứng dụngKhuyến nghị
Website công khai, content-heavyKHÔNG disable zoom. Đảm bảo font ≥ 16px trên mobile.
Web App (dashboard, admin)Có thể disable zoom nếu có lý do UX mạnh, nhưng cần accessibility review.
Game HTML5 fullscreenDùng touch-action: none trên canvas container.
PWA (Add to Home Screen)Cần display: standalone trong manifest, viewport chuẩn.