移动端底部导航“凸起”需分层实现:均分用Grid,凸起用position: absolute独立容器;列数不稳定主因是父容器未设width: 100%;safe-area-inset-bottom必须加在直接父容器且仅用padding-bottom。

移动端底部导航栏用 CSS Grid 均分没问题,但“凸起”不是靠 Grid 实现的,而是靠视觉层级 + 安全区预留 + 独立定位容器;均分失效的主因是容器 width 没设满,不是 Grid 写法错了。
为什么 grid-template-columns: repeat(auto-fit, minmax(72px, 1fr)) 在手机上列数忽多忽少
这不是 auto-fit 失效,而是父容器没撑开到全宽。Grid 轨道计算基于容器可用宽度,而 position: fixed 或 display: grid 的 footer 默认 width: auto,不是 100%。
- 必须给底部导航的直接父容器加
width: 100%; max-width: 100%(双保险) - 如果用了
position: fixed,别只写bottom: 0,得配合left: 0; right: 0才能触发 100% 宽度行为 - 避免在父容器上写
display: contents——它会让 Grid 失去计算上下文,子项变成“无父容器”,auto-fit直接退化
如何让中间图标“凸起”且不破坏 Grid 均分逻辑
Grid 本身不支持单个轨道“向上凸出”,强行用 transform: translateY(-12px) 会脱离文档流、遮挡内容、触控热区偏移。正确做法是分层:均分用 Grid,凸起用独立定位容器。
- 把中间图标抽出来,放在导航栏 同级 的一个
position: absolute元素里,用bottom: env(safe-area-inset-bottom)对齐底部基准线 - 均分的 Grid 容器保持完整 5 列(或你设定的列数),中间位置留空(比如用
grid-column: 3 / 4占位但不放内容) - 绝对定位的凸起图标需手动设
left: 50%; transform: translateX(-50%)居中,不能依赖 Grid 的justify-items - 务必给凸起图标加
z-index: 2,并确保其父容器(比如body或main)有position: relative
触控热区小、点击飘移的真正原因和解法
很多人以为 Grid 占了整行,热区就自动满了——错。热区由元素自身盒模型决定,grid-column: 1 / -1 只控制位置,不撑开尺寸。
htmlq 是一个命令行工具,使用 CSS 选择器从 HTML 中提取内容,相当于 jq 的 HTML 版本。该工具使用 Rust 编写,允许你通过 CSS 选择器将 HTML 管道化,直接从终端提取文本、属性和结构化内容。
立即学习“前端免费学习笔记(深入)”;
- 每个导航项必须显式设
min-height: 44px(iOS)或48px(Android),不能只靠文字撑高 - 禁用
display: contents和font-size: 0,它们会让元素“视觉存在但盒模型消失” - 用
padding: 12px 0比height: 44px更安全——它允许文字自然换行,且兼容不同行高 - 凸起图标热区要单独处理:外层包裹
div设width: 64px; height: 64px;,再用flex居中图标,避免靠transform缩放热区
safe-area-inset-bottom 加在哪一层才不翻车
加在 body 上?加在 footer 上?加在导航项上?都不对。必须加在底部导航的 直接父容器,且仅用 padding-bottom,不用 margin-bottom 或 height 计算。
- 错误:
body { padding-bottom: env(safe-area-inset-bottom) }→ 影响全局,滚动时可能拉长页面 - 错误:
.nav-item { padding-bottom: env(safe-area-inset-bottom) }→ 每个项都加,gap 叠加导致总高度失控 - 正确:
.bottom-nav { padding-bottom: env(safe-area-inset-bottom) },且该元素是 Grid 容器的直接父级 - 兼容 Safari 15.4 以下:用
@supports not (padding-bottom: env(safe-area-inset-bottom)) { .bottom-nav { padding-bottom: 24px; } }
最易被忽略的一点:凸起图标的“凸起高度”不能超过 env(safe-area-inset-bottom) 的两倍,否则在部分 Android WebView 中会触发底部裁剪——它不是纯视觉效果,而是真实影响可点击区域的物理空间。

















