
Sudoku 网格中嵌入的 在桌面端正常显示,但在手机端缩放失衡、尺寸过小或完全不可见,根本原因在于传统表格()对表单控件的移动端渲染兼容性差,需改用现代 CSS Grid 布局并配合 viewport 设置与媒体查询实现真正响应式适配。
sudoku 网格中嵌入的 `` 在桌面端正常显示,但在手机端缩放失衡、尺寸过小或完全不可见,根本原因在于传统表格(`
| `)对表单控件的移动端渲染兼容性差,需改用现代 css grid 布局并配合 viewport 设置与媒体查询实现真正响应式适配。 在移动端,尤其是 iOS Safari 和部分 Android 浏览器中, 元素嵌套在
|
内时,常因默认最小尺寸限制(min-width: auto)、字体缩放策略、表格单元格自动收缩行为以及未设置 viewport 导致渲染异常——表现为输入框“消失”、无法点击、文字溢出或高度塌陷(如题图所示)。单纯调整 font-size 或 padding 往往无效,关键在于重构布局模型并显式控制响应行为。 ✅ 推荐方案:弃用
Voicemaker
Voicemaker是一款提供多语言文本转语音和 SSML 控制的在线 AI 语音生成工具。
下载
,采用语义清晰、控制力强的 CSS Grid 布局:
<meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"><title>Sudoku 响应式网格</title><style>
/* 基础重置与视口适配 */
* { box-sizing: border-box; }
.sudoku {
display: grid;
grid-template-columns: repeat(9, 1fr);
gap: 2px;
border: 2px solid #333;
width: 100%;
max-width: 500px;
margin: 1rem auto;
background-color: #333;
padding: 2px;
}
.cell {
aspect-ratio: 1 / 1; /* 保持正方形,关键! */
display: flex;
align-items: center;
justify-content: center;
font-size: clamp(14px, 4vw, 20px); /* 响应式字体:最小14px,最大20px,中间随视口线性缩放 */
background-color: #fff;
position: relative;
}
.cell.empty {
background-color: #f5f5f5;
}
.cell input {
width: 100%;
height: 100%;
border: none;
outline: none;
text-align: center;
font-size: 100%; /* 继承父级 clamp 计算值 */
background: transparent;
color: #222;
font-weight: bold;
-webkit-appearance: none; /* 移除 iOS 默认样式 */
appearance: none;
}
/* 针对小屏设备增强可触控性 */
@media (max-width: 480px) {
.sudoku {
gap: 1px;
max-width: 400px;
}
.cell {
font-size: clamp(16px, 5vw, 22px);
}
.cell input {
min-height: 44px; /* 满足 iOS 最小触控目标建议 */
}
}
/* 可选:高亮宫格边界(3×3 子区域) */
.cell:nth-child(3n) { border-right: 2px solid #333; }
.cell:nth-child(9n) { border-right: none; }
.cell:nth-child(n+19):nth-child(-n+27),
.cell:nth-child(n+46):nth-child(-n+54) {
border-bottom: 2px solid #333;
}
</style><div class="sudoku">
<!-- 第一行 -->
<div class="cell empty"><input type="text" maxlength="1" inputmode="numeric" pattern="[1-9]"></div>
<div class="cell">8</div>
<div class="cell empty"><input type="text" maxlength="1" inputmode="numeric" pattern="[1-9]"></div>
<div class="cell empty"><input type="text" maxlength="1" inputmode="numeric" pattern="[1-9]"></div>
<div class="cell">7</div>
<div class="cell empty"><input type="text" maxlength="1" inputmode="numeric" pattern="[1-9]"></div>
<div class="cell empty"><input type="text" maxlength="1" inputmode="numeric" pattern="[1-9]"></div>
<div class="cell">6</div>
<div class="cell empty"><input type="text" maxlength="1" inputmode="numeric" pattern="[1-9]"></div>
<!-- 后续 8 行同理... -->
</div>
? 关键要点说明:
-
必须存在:禁用用户缩放(user-scalable=no)可避免意外缩放导致输入框挤压;initial-scale=1.0 确保以 1:1 渲染。
-
aspect-ratio: 1 / 1:强制每个单元格为正方形,彻底解决手机端因宽高比失衡导致的输入框拉伸/塌陷。
-
clamp() 响应式字体:比固定 px 或单纯 vw 更健壮,兼顾小屏可读性与大屏清晰度。
-
inputmode="numeric" + pattern:触发数字键盘,提升移动端输入体验,并限制仅输入 1–9。
-
-webkit-appearance: none:消除 iOS 默认输入框内边距与圆角,确保样式完全可控。
-
触控目标最小尺寸:min-height: 44px 符合 Apple Human Interface Guidelines,显著提升点击成功率。
? 额外建议:
若必须保留
|