填充表格行
<html>
<head>
<title>Table Row Padding Issue</title>
<style type="text/css">
tr {
padding: 20px;
}
</style>
</head>
<body>
<table>
<tbody>
<tr>
<td>
<h2>Lorem Ipsum</h2>
<p>Fusce sodales lorem nec magna iaculis a fermentum lacus facilisis. Curabitur sodales risus sit amet
neque fringilla feugiat. Ut tellus nulla, bibendum at faucibus ut, convallis eget neque. In hac habitasse
platea dictumst. Nullam elit enim, gravida eu blandit ut, pellentesque nec turpis. Proin faucibus, sem sed
tempor auctor, ipsum velit pellentesque lorem, ut semper lorem eros ac eros. Vivamus mi urna, tempus vitae
mattis eget, pretium sit amet sapien. Curabitur viverra lacus non tortor luctus vitae euismod purus
hendrerit. Praesent ut venenatis eros. Nulla a ligula erat. Mauris lobortis tempus nulla non
scelerisque.</p>
</td>
</tr>
</tbody>
</table>
</body>
</html>
这是填充的样子。看看里面的 td 是如何不受影响的。解决办法是什么?
<html>
<head>
<title>Table Row Padding Issue</title>
<style type="text/css">
tr {
padding: 20px;
}
</style>
</head>
<body>
<table>
<tbody>
<tr>
<td>
<h2>Lorem Ipsum</h2>
<p>Fusce sodales lorem nec magna iaculis a fermentum lacus facilisis. Curabitur sodales risus sit amet
neque fringilla feugiat. Ut tellus nulla, bibendum at faucibus ut, convallis eget neque. In hac habitasse
platea dictumst. Nullam elit enim, gravida eu blandit ut, pellentesque nec turpis. Proin faucibus, sem sed
tempor auctor, ipsum velit pellentesque lorem, ut semper lorem eros ac eros. Vivamus mi urna, tempus vitae
mattis eget, pretium sit amet sapien. Curabitur viverra lacus non tortor luctus vitae euismod purus
hendrerit. Praesent ut venenatis eros. Nulla a ligula erat. Mauris lobortis tempus nulla non
scelerisque.</p>
</td>
</tr>
</tbody>
</table>
</body>
</html>
Here's what the padding looks like. See how the td inside isn't affected. What's the solution?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
诀窍是在
td
元素上进行填充,但对第一个元素进行例外处理(是的,这很hacky,但有时你必须遵守浏览器的规则):First-child 得到了相对较好的支持: https://developer.mozilla.org/en-US/docs /CSS/:first-child
您可以使用
tr:first-child td
对水平填充使用相同的推理。或者,使用 not 运算符。不过,目前对此的支持并不那么好。
The trick is to give padding on the
td
elements, but make an exception for the first (yes, it's hacky, but sometimes you have to play by the browser's rules):First-child is relatively well supported: https://developer.mozilla.org/en-US/docs/CSS/:first-child
You can use the same reasoning for the horizontal padding by using
tr:first-child td
.Alternatively, exclude the first column by using the
not
operator. Support for this is not as good right now, though.在 CSS 1 和 CSS 2 规范,填充可用于所有元素,包括
< tr>
。然而,对表行填充 () 的支持已在 CSS 2.1 和 CSS 3 规格。我从未找到这个令人讨厌的更改背后的原因,它也会影响边距属性和其他一些表格元素(页眉、页脚和列)。
更新:2015 年 2 月,此帖子位于 < code>[email protected] 讨论有关添加支持的邮件列表表行的填充和边框。这会将标准盒模型也应用于表行和表列元素。它将允许此类示例。该线程似乎表明 CSS 标准中从未存在表行填充支持,因为它会有复杂的布局引擎。在 2014 年 9 月 30 日的 CSS 基本盒模型编辑草案中,所有内容都存在 padding 和 border 属性元素包括表行元素和表列元素。如果它最终成为 W3C 推荐,您的 html+css 示例最终可能会在浏览器中按预期工作。
In CSS 1 and CSS 2 specifications, padding was available for all elements including
<tr>
. Yet support of padding for table-row (<tr>
) has been removed in CSS 2.1 and CSS 3 specifications. I have never found the reason behind this annoying change which also affect margin property and a few other table elements (header, footer, and columns).Update: in Feb 2015, this thread on the
[email protected]
mailing list discussed about adding support of padding and border for table-row. This would apply the standard box model also to table-row and table-column elements. It would permit such examples. The thread seems to suggest that table-row padding support never existed in CSS standards because it would have complicated layout engines. In the 30 September 2014 Editor's Draft of CSS basic box model, padding and border properties exist for all elements including table-row and table-column elements. If it eventually becomes a W3C recommendation, your html+css example may work as intended in browsers at last.选项 1
您还可以通过向行 (tr) 添加透明边框来解决此问题,就像
HTML
CSS
一样,它就像一个魅力,但如果您需要常规边框,那么遗憾的是此方法将不起作用。
选项 2
由于行是对单元格进行分组的一种方式,因此正确的方法是使用
Option 1
You could also solve it by adding a transparent border to the row (tr), like this
HTML
CSS
Works like a charm, although if you need regular borders, then this method will sadly not work.
Option 2
Since rows act as a way to group cells, the correct way to do this, would be to use
给 td 填充
give the td padding
添加一个 before 元素:
在“响应式”表格的上下文中,这对我来说很重要,即
td {display: table-row; }
Add a before element:
It was important to me in the context of 'responsive' tables, ie
td {display: table-row; }
您只需将此样式添加到表中即可。
You could simply add this style to the table.
这是一篇非常旧的帖子,但我想我应该发布我最近遇到的类似问题的解决方案。
答案:我通过将 tr 元素显示为 block 元素解决了这个问题,即指定 CSS display:block > 为 tr 元素。您可以在下面的代码示例中看到这一点。
This is a very old post, but I thought I should post my solution of a similar problem I faced recently.
Answer : I solved this issue by displaying the tr element as a block element i.e. specifying a CSS of display:block for the tr element. You can see this in code sample below.