如何在 CSS 中指定表格填充? (表格,不是单元格,填充)

发布于 2024-08-12 02:58:40 字数 509 浏览 5 评论 0原文

我有一个带有彩色背景的表格,我需要指定表格与其内容(IE 单元格)之间的填充。
表标记似乎不接受填充值。
Firebug 用 padding 0 显示表格和 tbody 的布局,但不接受为它们输入的任何值,所以我猜它们只是没有 padding 属性。
但问题是我希望单元格之间的间隔与顶部单元格与桌面以及底部单元格与表格底部的间隔相同。
再说一遍,我想要的不是单元格填充。

编辑:谢谢,我现在意识到,我真正需要的是边框间距,或者它的 html 等效项,cellspacing。
但由于某种原因,他们没有在我正在处理的网站上执行任何操作。
两者在单独的 HTML 上都可以很好地工作,但在网站中则不然。
我认为这可能是某种样式覆盖属性,但单元格间距和内联边框间距不应该被覆盖,对吗?
(我使用 firefox)

编辑 2:不,TD 填充不是我需要的。使用 TD 填充时,相邻单元格的顶部和底部填充相加,因此两个单元格之间的空间(实际上是填充)是顶部单元格和表格顶部边框之间的两倍。我希望它们之间的距离完全相同。

I have a table with a colored background and I need to specify the padding between the table and it's content, I.E. cells.
The table tag doesn't seem to accept a padding value.
Firebug shows the table and tbody's layout with padding 0 but doesn't accept any value entered for them, so I guess they just don't have the padding property.
But the thing is I want the same separation between cells than the top cells with the table top and the bottom cells with the table's bottom.
Again, what I want is not cell-padding.

EDIT: Thanks, what I really needed, I realize now, was border-spacing, or its html equivalent, cellspacing.
But for some reason, they don't do anything in the site I'm working on.
Both work great on a separate HTML, but in the site they don't.
I thought it could be some style overwriting the property, but cellspacing and an inline border-spacing shouldn't get overwritten, right?
(I use firefox )

EDIT 2: No, TD padding is NOT what I need. With TD padding, top and bottom paddings of adjacent cells sum up, so there's double the space (pad actually) between two cells than between the top cell and the table top border. I want to have exactly the same distance between them.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

稍尽春風 2024-08-19 02:58:40

最简单/最好支持的方法是使用

css 方式:border-spacing (IE 不支持我不认为)

    <!-- works in firefox, opera, safari, chrome -->
    <style type="text/css">
    
    table.foobar {
    	border: solid black 1px;
    	border-spacing: 10px;
    }
    table.foobar td {
    	border: solid black 1px;
    }
    
    
    </style>
    
    <table class="foobar" cellpadding="0" cellspacing="0">
    <tr><td>foo</td><td>bar</td></tr>
    </table>

编辑:如果您只想填充单元格内容,而不是间隔它们,您可以简单地使用

<table cellpadding="10">

OR

td {
    padding: 10px;
}

The easiest/best supported method is to use <table cellspacing="10">

The css way: border-spacing (not supported by IE I don't think)

    <!-- works in firefox, opera, safari, chrome -->
    <style type="text/css">
    
    table.foobar {
    	border: solid black 1px;
    	border-spacing: 10px;
    }
    table.foobar td {
    	border: solid black 1px;
    }
    
    
    </style>
    
    <table class="foobar" cellpadding="0" cellspacing="0">
    <tr><td>foo</td><td>bar</td></tr>
    </table>

Edit: if you just want to pad the cell content, and not space them you can simply use

<table cellpadding="10">

OR

td {
    padding: 10px;
}
坚持沉默 2024-08-19 02:58:40

您可以为表格设置边距。或者,将表格包装在 div 中并使用 div 的填充。

You could set a margin for the table. Alternatively, wrap the table in a div and use the div's padding.

剩余の解释 2024-08-19 02:58:40

关于表格,您应该了解以下内容...它们不是嵌套独立元素的树。它们是单一的复合元素。虽然各个 TD 的行为或多或少类似于 CSS 块元素,但中间内容(TABLE 和 TD 之间的任何内容,包括 TR 和 TBODY)是不可分割的,并且不属于 内联类型阻止。该异维空间中不允许有随机的 HTML 元素,并且该异维空间的大小根本无法通过 CSS 配置。只有 HTML cellspacing 属性才能获取它,而该属性在 CSS 中没有类似的属性。

因此,为了解决您的问题,我建议使用 DIV 包装器(正如另一张海报所建议的那样),或者如果您绝对必须将其包含在表中,那么您将得到这个丑陋的垃圾:

<style>
    .padded tr.first td { padding-top:10px; }
    .padded tr.last td { padding-bottom:10px; }
    .padded td.first { padding-left:10px; }
    .padded td.last { padding-right:10px; }
</style>

<table class='padded'>
    <tr class='first'>
        <td class='first'>a</td><td>b</td><td class='last'>c</td>
    </tr>
    <tr>
        <td class='first'>d</td><td>e</td><td class='last'>f</td>
    </tr>
    <tr class='last'>
        <td class='first'>g</td><td>h</td><td class='last'>i</td>
    </tr>
</table>

Here's the thing you should understand about tables... They're not a tree of nested independent elements. They're a single compound element. While the individual TDs behave more or less like CSS block elements, the intermediate stuff (anything between the TABLE and TD, including TRs and TBODYs) is indivisible and doesn't fall into either inline nor block. No random HTML elements are allowed in that other-dimensional space, and the size of such other-dimensional space isn't configurable at all through CSS. Only the HTML cellspacing property can even get at it, and that property has no analog in CSS.

So, to solve your problem, I'd suggest either a DIV wrapper as another poster suggests, or if you absolutely must keep it contained in the table, you have this ugly junk:

<style>
    .padded tr.first td { padding-top:10px; }
    .padded tr.last td { padding-bottom:10px; }
    .padded td.first { padding-left:10px; }
    .padded td.last { padding-right:10px; }
</style>

<table class='padded'>
    <tr class='first'>
        <td class='first'>a</td><td>b</td><td class='last'>c</td>
    </tr>
    <tr>
        <td class='first'>d</td><td>e</td><td class='last'>f</td>
    </tr>
    <tr class='last'>
        <td class='first'>g</td><td>h</td><td class='last'>i</td>
    </tr>
</table>
谎言月老 2024-08-19 02:58:40

你不能...也许如果你发布了所需效果的图片,那么还有另一种方法可以实现它。

例如,您可以将整个表格包装在 DIV 中,并将填充设置为 div。

You can't... Maybe if you posted a picture of the desired effect there's another way to achieve it.

For example, you can wrap the entire table in a DIV and set the padding to the div.

就此别过 2024-08-19 02:58:40

有趣的是,我昨天正是这样做的。您只需要在 css 文件中添加此内容

.ablock table td {
     padding:5px;
}

,然后将表格包装在合适的 div 中

<div class="ablock ">
<table>
  <tr>
    <td>

Funny, I was doing precisely this yesterday. You just need this in your css file

.ablock table td {
     padding:5px;
}

then wrap the table in a suitable div

<div class="ablock ">
<table>
  <tr>
    <td>
长梦不多时 2024-08-19 02:58:40

还有一个技巧:(

/* Padding on the sides of the table */
table th:first-child, .list td:first-child { padding-left: 28px; }
table th:last-child, .list td:last-child { padding-right: 28px; }

刚刚在我现在的工作中看到)

There is another trick :

/* Padding on the sides of the table */
table th:first-child, .list td:first-child { padding-left: 28px; }
table th:last-child, .list td:last-child { padding-right: 28px; }

(Just saw it at my current job)

臻嫒无言 2024-08-19 02:58:40

使用 CSS,表格可以具有独立于其单元格的填充。

padding 属性不会被其子级继承。

所以,定义:

table {
    padding: 5px;
}

应该有效。您还可以专门告诉浏览器如何填充(或者在本例中,不填充)您的单元格。

td {
    padding: 0px;
}

编辑: IE8 不支持。对不起。

With css, a table can have padding independently of its cells.

The padding property is not inherited by its children.

So, defining:

table {
    padding: 5px;
}

Should work. You could also specifically tell the browser how to pad (or in this case, not pad) your cells.

td {
    padding: 0px;
}

EDIT: Not supported by IE8. Sorry.

谜泪 2024-08-19 02:58:40

您可以尝试 border-spacing 属性。那应该做你想做的事。但您可能想查看这个答案

You can try the border-spacing property. That should do what you want. But you may want to see this answer.

飘逸的'云 2024-08-19 02:58:40

CSS 并不允许您在表格级别上执行此操作。一般来说,当我想达到这种效果时,我会指定cellspacing="3"。显然这不是一个 CSS 解决方案,所以请尊重它的价值。

CSS doesn't really allow you to do this on a table level. Generally, I specify cellspacing="3" when I want to achieve this effect. Obviously not a css solution, so take it for what it's worth.

情归归情 2024-08-19 02:58:40
table {
    background: #fff;
    box-shadow: 0 0 0 10px #fff;
    margin: 10px;
    width: calc(100% - 20px); 
}
table {
    background: #fff;
    box-shadow: 0 0 0 10px #fff;
    margin: 10px;
    width: calc(100% - 20px); 
}
一生独一 2024-08-19 02:58:40
table.foobar
{
   padding:30px; /* if border is not collapsed */
}

or 

table.foobar
{
   border-spacing:0;
}
table.foobar>tbody
{
   display:table;
   border-spacing:0; /* or other preferred */
   border:30px solid transparent; /* 30px is the "padding" */
}

适用于 Firefox、Chrome、IE11、Edge。

table.foobar
{
   padding:30px; /* if border is not collapsed */
}

or 

table.foobar
{
   border-spacing:0;
}
table.foobar>tbody
{
   display:table;
   border-spacing:0; /* or other preferred */
   border:30px solid transparent; /* 30px is the "padding" */
}

Works in Firefox, Chrome, IE11, Edge.

橘香 2024-08-19 02:58:40
table {
    border: 1px solid transparent;
}
table {
    border: 1px solid transparent;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文