You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

移动端/桌面端营业时间表格CSS适配求助

响应式营业时间表布局实现方案

核心思路是利用CSS媒体查询结合Grid布局,针对桌面、平板、移动端的不同屏幕宽度,重新排列表格单元格的位置,彻底避免横向滚动问题。

第一步:基础HTML结构

先确保你的表格结构符合9列需求(示例如下):

<table class="business-hours-table">
  <tbody>
    <tr>
      <td>周一</td>       <!-- Col1:营业日 -->
      <td>营业</td>       <!-- Col2:营业状态 -->
      <td><input type="time"></td> <!-- Col3:开始时间 -->
      <td><button>+</button></td>  <!-- Col4:开始时间+ -->
      <td><button>-</button></td>  <!-- Col5:开始时间- -->
      <td><input type="time"></td> <!-- Col6:结束时间 -->
      <td><button>+</button></td>  <!-- Col7:结束时间+ -->
      <td><button>-</button></td>  <!-- Col8:结束时间- -->
      <td>无</td>         <!-- Col9:备注 -->
    </tr>
    <!-- 周二至周日的行结构完全一致 -->
  </tbody>
</table>

第二步:通用CSS样式

先设置桌面端默认表格样式,保证正常展示:

.business-hours-table {
  width: 100%;
  border-collapse: collapse;
  margin: 16px 0;
}

.business-hours-table td {
  border: 1px solid #e0e0e0;
  padding: 10px;
  text-align: center;
}

/* 优化移动端按钮尺寸,方便触控点击 */
.business-hours-table button {
  padding: 6px 12px;
  font-size: 16px;
  cursor: pointer;
}

第三步:平板端适配(≤1024px)

将每行拆分为两行显示,第一行展示Col1-Col5,第二行展示Col6-Col9,Col1(营业日)跨两行垂直居中:

@media (max-width: 1024px) {
  .business-hours-table tr {
    display: grid;
    grid-template-columns: auto repeat(4, 1fr);
    gap: 8px;
    margin-bottom: 20px;
    border-bottom: 1px solid #e0e0e0;
    padding-bottom: 16px;
  }

  /* 营业日单元格跨两行 */
  .business-hours-table td:nth-child(1) {
    grid-row: 1 / span 2;
    display: flex;
    align-items: center;
    justify-content: center;
    border: none;
  }

  /* 第一行:状态、开始时间及控制按钮 */
  .business-hours-table td:nth-child(2),
  .business-hours-table td:nth-child(3),
  .business-hours-table td:nth-child(4),
  .business-hours-table td:nth-child(5) {
    grid-row: 1;
  }

  /* 第二行:结束时间及控制按钮、备注 */
  .business-hours-table td:nth-child(6),
  .business-hours-table td:nth-child(7),
  .business-hours-table td:nth-child(8),
  .business-hours-table td:nth-child(9) {
    grid-row: 2;
  }

  /* 重置表格默认边框和间距 */
  .business-hours-table {
    border-collapse: separate;
    border-spacing: 0;
  }
}

第四步:移动端适配(≤768px)

将每行拆分为四行显示,Col1(营业日)跨四行,其余每两列占一行:

@media (max-width: 768px) {
  .business-hours-table tr {
    grid-template-columns: auto repeat(2, 1fr);
  }

  /* 营业日单元格跨四行 */
  .business-hours-table td:nth-child(1) {
    grid-row: 1 / span 4;
  }

  /* 第一行:营业状态 + 开始时间 */
  .business-hours-table td:nth-child(2),
  .business-hours-table td:nth-child(3) {
    grid-row: 1;
  }

  /* 第二行:开始时间+ + 开始时间- */
  .business-hours-table td:nth-child(4),
  .business-hours-table td:nth-child(5) {
    grid-row: 2;
  }

  /* 第三行:结束时间 + 结束时间+ */
  .business-hours-table td:nth-child(6),
  .business-hours-table td:nth-child(7) {
    grid-row: 3;
  }

  /* 第四行:结束时间- + 备注 */
  .business-hours-table td:nth-child(8),
  .business-hours-table td:nth-child(9) {
    grid-row: 4;
  }
}

注意事项

  • 可根据实际需求调整媒体查询的断点值(比如把平板端的1024px改成992px)
  • 如果你的9列对应内容和示例不同,只需调整nth-child()的序号即可
  • 若需兼容更老浏览器,可替换为Flex布局,但Grid在响应式排列上更直观高效

内容的提问来源于stack exchange,提问作者Sid Ahmed

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 07:25:27