Html 为什么 cellpacing 和 cellpadding 不是 CSS 样式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/339146/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why are cellspacing and cellpadding not CSS styles
提问by Ryan Smith
I don't know why this bothers me so much, but when I create websites, I always try to do all my styling with CSS. However one thing I always have to remember to do when I'm working with tables is add cellspacing="0" and cellpadding="0"
我不知道为什么这让我如此困扰,但是当我创建网站时,我总是尝试使用 CSS 来完成所有样式。然而,当我处理表格时,我总是必须记住要做的一件事是添加 cellpacing="0" 和 cellpadding="0"
Why is there not a CSS property to override these antiquated HTML 4 attributes?
为什么没有 CSS 属性来覆盖这些过时的 HTML 4 属性?
回答by mat
细胞间距:
table { border-collapse: collapse; }
As for cellpadding, you can do
至于cellpadding,你可以做
table tr td, table tr th { padding: 0; }
回答by Ryan Smith
mat already answered, but just for completeness:
mat 已经回答了,但只是为了完整性:
padding
→cellpadding
border-spacing
→cellspacing
border-collapse
→ no HTML equivalent
padding
→cellpadding
border-spacing
→cellspacing
border-collapse
→ 没有等效的 HTML
It's also worth remembering that you can set separate horizontal and vertical values for the CSS ones e.g. border-spacing: 0 1px
.
还值得记住的是,您可以为 CSS 设置单独的水平和垂直值,例如border-spacing: 0 1px
.
回答by Simon_Weaver
Eric Myer's reset stylesheetcontains the following 'reset' style for table :
Eric Myer 的重置样式表包含 table 的以下“重置”样式:
/* tables still need 'cellspacing="0"' in the markup */
table {
border-collapse: collapse;
border-spacing: 0;
}
In addition TD, TR are reset :
另外TD、TR都被复位了:
thead, tr, th, td {
margin: 0;
padding: 0;
border: 0;
outline: 0;
font-size: 100%;
vertical-align: baseline;
background: transparent;
}
The reason I mention this is that he has a comment 'tables still need cellpadding=0'. I assume he put this in here for a reason - probably needed by some old browsers. Judging by the fact that this is one of the very few comments he included I'm guessing its important and that there's a good reason for it.
我提到这一点的原因是他有一条评论“表格仍然需要 cellpadding=0”。我认为他把它放在这里是有原因的——可能是一些旧浏览器需要的。从这是他包含的极少数评论之一这一事实来看,我猜它很重要,并且有充分的理由。
Based on this comment - and this comment alone! - i'm continuing to use cellspacing="0" in the markup unless someone tells me definitively (below) why I dont need to. It could however likely be unnecessary in any modern browser worth supporting these days.
基于此评论 - 仅此评论!- 我将继续在标记中使用 cellpacing="0" 除非有人明确地(下面)告诉我为什么我不需要。然而,在当今值得支持的任何现代浏览器中,它可能是不必要的。
回答by Jimmy
table { border-collapse:collapse; }
回答by Din
I guess somebody considered cell spacing a “bad practice”. How I understand it is equivalent included in CSS2 standard but IE does not support this property. border-collapse allows to set spacing to 0 value. Cell padding may be achieved setting padding property to TD elements of a table.
我猜有人认为单元格间距是“不好的做法”。我如何理解它等同于包含在 CSS2 标准中,但 IE 不支持此属性。border-collapse 允许将间距设置为 0 值。可以通过将填充属性设置为表的 TD 元素来实现单元格填充。