Html 表格内单个 TD 的 CSS 填充

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9207996/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 22:23:54  来源:igfitidea点击:

CSS padding for single TD inside a table

htmlhtml-tablepadding

提问by wiktus239

I have a table, constructed like this:

我有一张桌子,构造如下:

<table>
<tr>  <td>1</td>  <td>2</td>  <td rowspan="4">3</td></tr>
<tr>  <td>4</td>  <td>5</td>                        </tr>
<tr>  <td>6</td>  <td>7</td>                        </tr>
<tr>  <td>8</td>  <td>9</td>                        </tr>
<tr height="100"><td colspan="2">10</td><td class="eleven">11</td> </tr>
</table>

Now the problem is within the last row. Whole row has a height set to 100px, so there is a plenty of room in TDs. In the very last TD I want to set an individual padding, so only the content "11" is padded from the top:

现在问题出在最后一行。整行的高度设置为 100px,因此 TD 中有足够的空间。在最后一个 TD 中,我想设置一个单独的填充,所以只有内容“11”从顶部填充:

.eleven {
    padding-top:15px;
}

Setting this causes the problem - the first TD in this row also gets padding-top:10px; Why and how to make only the 2nd one padded?

设置这个会导致问题 - 该行中的第一个 TD 也得到 padding-top:10px; 为什么以及如何仅填充第二个?

回答by Navneeth G

Why don't you wrap the content you want to be padded into a <div>(onto which you will be applying the padding style) and put that <div>into the <td>?

为什么不将要填充的内容包装到 a <div>(您将在其上应用填充样式)并将其<div>放入<td>?

<td>
    <div style="padding-top: 15px;"> 
        Content 
    </div> 
</td>

回答by wiktus239

Ok, I found out what caused the problem. It was an entry in a little html5-css-reset snippet I use:

好的,我找到了导致问题的原因。这是我使用的一个小的 html5-css-reset 片段中的一个条目:

vertical-align:baseline;

assigned generally to most of common elements. Having that in mind, now everything works as supposed to.

通常分配给大多数常见元素。考虑到这一点,现在一切都按预期进行。