html - table 为行中的所有单元格设置相同的 td 宽度

标签 html css

我有一个表格,我希望标题行中的每个单元格都具有相同的宽度。这是我当前的代码:

table {width:100%}
table td{border: 1px solid}
.header td{ }
<html>

	<table>
		<tr class="header"><td colspan="2">header </td><td> header</td></tr>
		<tr><td> content </td><td> content </td><td> content </td></tr>
	</table>
</html>

基本上,我可以用这个得到结果。

	
table td{border: 1px solid}
.header td{ width: 100px}
<html>

	<table>
		<tr class="header"><td colspan="2">header </td><td> header</td></tr>
		<tr><td> content </td><td> content </td><td> content </td></tr>
	</table>
</html>

但是,我想要以下东西:

  • 表格必须支持可变 (%) 宽度。
  • CSS 必须使用可变数量的列(因此将宽度设置为 50% 将不起作用)
  • 如果可能的话,我正在寻找一个只有 css 的解决方案(没有 javascript)

最佳答案

你可能只是寻找 table-layout:fixed; wich spray evenly cols when not width is specified or applyes width specified width is specified without expanding :

table {width:100%; table-layout:fixed;}
table td{border: 1px solid}
.header td{ }
<html>

	<table>
		<tr class="header"><td colspan="2">header </td><td> header</td></tr>
		<tr><td> content </td><td> content </td><td> content </td></tr>
	</table>
</html>

注意使用 th 和/或 theader 来过滤标签/内容

table td, th{
  border: 1px solid;
  table-layout:fixed;/* sprays evenly col width if no width set */
}
.header td{ 
  width:100px; /* table-layout if fixed will follow this value */
}
<html>

	<table>
		<tr class="header"><th colspan="2">header header header </th><th> header</th></tr>
		<tr><td> content </td><td> content </td><td> content  content  content </td></tr>
	</table>
</html>

关于html - table 为行中的所有单元格设置相同的 td 宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28985198/

相关文章:

javascript - 使用 JS 在文本中查找并突出显示单词

jquery - 通过 jQuery 获取 CSS 变量的值

html - 在浏览器中显示时需要 CSS 使所有字段内联

html - 如何在父节点后面放置伪元素?

javascript - 在不需要的字符出现之前从文本输入中删除它们

facebook - 使用 Iframe 将 HTML5 音乐播放器嵌入到 Facebook 帖子中

javascript - ReactJs——在 li 元素的点击事件上,类不会立即添加

css - Content Div 内的 100% Height Div

css - CSS 中的 Bootstrap 焦点输入和图标

jquery - 如何使用 CSS 或 JavaScript 检测手机和平板电脑而不考虑品牌?