Skip to content

Responsive 布局 Broken

快速摘要

你的 page 做不 adapt 正确地到 不同 screen 大小 — 内容 overflows, 元素是 也小, or 布局 breaks.

为什么发生

  • Missing viewport meta 标签

  • Fixed 像素宽度相反的 相对单元

  • Media 查询 breakpoints 是不正确 or missing

  • 镜像不使用最大值-宽度: 100% overflow 它们的容器

最小示例

✗ 错误代码
<!-- Missing viewport meta tag -->
<head>
  <title>My Page</title>
</head>
<body>
  <div style="width: 1200px;">Fixed width overflows mobile</div>
</body>
✓ 修复代码
<head>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>My Page</title>
</head>
<body>
  <div style="max-width: 1200px; width: 100%;">Responsive width</div>
</body>

viewport meta 标签是 必要 for responsive 设计 on mobile. Fixed 宽度 overflow — use 最大值-宽度使用宽度: 100% 相反.

如何诊断

  • 检查 for viewport meta 标签 in <head>

  • 测试 in DevTools device emulation at 不同宽度

  • Look for fixed-宽度元素那个 overflow on 小 screens

  • 验证 media 查询 breakpoints match 你的设计 specs

如何修复

  • 添加 <meta name='viewport' 内容='宽度=device-宽度, 初始-scale=1'>

  • Use 相对单元 (%, fr, rem, vw/vh) 相反的 fixed px

  • 添加 or adjust media 查询 for problematic screen 大小

  • 添加最大值-宽度: 100% and 高度: auto 到镜像

如何预防

  • 总是包含 viewport meta 标签

  • Use mobile-首先方法使用最小值-宽度 media 查询

  • 测试 at 常见 breakpoints (320px, 768px, 1024px, 1280px)

相关资源

相关术语

相关课程

← 返回语言