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

如何为表格TD标签及TH内所有元素统一设置10px字号?

实现表格TD字号10px并强制覆盖TH子元素字号的简便方法

要完成你需要的效果,只需在现有CSS中添加两行核心规则即可:

/* 给所有TD标签设置字号10px */
td {
  font-size: 10px !important;
}

/* 强制TH标签内所有子元素(无论是什么标签)字号为10px */
th * {
  font-size: 10px !important;
}

关键说明

  • !important 用于覆盖你现有代码中优先级更高的样式(比如 .table_tr td、.table_span span 这类带类选择器的规则),确保最终字号设置生效。
  • th * 是后代通配选择器,* 匹配任意HTML标签,能覆盖TH内部所有层级的子元素。

修改后的完整CSS示例

table {
  border-collapse: collapse;
  width: 100%;
}

th,
td {
  border: 1px solid #ddd;
  padding: 8px;
  text-align: left;
}

th {
  background-color: #f2f2f2;
}

/* 新增的核心规则 */
td {
  font-size: 10px !important;
}

th * {
  font-size: 10px !important;
}

/* 原有样式会被上面的规则强制覆盖 */
.table_tr td {
  font-size: 12px;
}

.table_span span {
  font-size: 14px;
}

.table_div div {
  font-size: 18px;
}

如果不想使用 !important,可以通过提升选择器优先级实现,但需要逐个指定TH内的子元素类,写法繁琐且扩展性差,不如上述方法简便直接。

内容的提问来源于stack exchange,提问作者ɐudɐʍs s

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.28 09:25:30