td在元素右侧添加额外的边距或填充

时间:2014-01-28 15:58:38

标签: html css html-table

在我的老师的作业中,我们制作了一个表格,其中我希望每个td元素的宽度为200px,总共5个单元格,等于1000px(包装器)。然而,当我这样做的时候,每个tr中的最后一个td会跳到下一行,因为它们应该正确排列而没有任何问题。

编辑:解决了! (阅读评论)

这是我的表格代码:

    <table id="assignment-table">
        <tr>
            <th>Assignment</th>
            <th>Fall 2013</th>
            <th>Responsive?</th>
            <th>Winter 2014</th>
            <th>Responsive?</th>
        </tr>

        <tr>
            <td>A1</td>
            <td>Code Provided Site</td>
            <td>No</td>
            <td>Designasaurus</td>
            <td>Yes</td>
        </tr>

        <tr>
            <td>A2a</td>
            <td>Restaurant Mock-up</td>
            <td>N/a</td>
            <td>TBA</td>
            <td>-</td>
        </tr>

        <tr>
            <td>A2b</td>
            <td>Restaurant Static</td>
            <td>No</td>
            <td>TBA</td>
            <td>-</td>
        </tr>

        <tr>
            <td>A3a</td>
            <td>Adaptive Excercise</td>
            <td>No</td>
            <td>TBA</td>
            <td>-</td>
        </tr>

        <tr>
            <td>A3b</td>
            <td>Responsive Excercise</td>
            <td>Yes</td>
            <td>TBA</td>
            <td>-</td>
        </tr>

        <tr>
            <td>A4</td>
            <td>Restaurant Final</td>
            <td>Yes</td>
            <td>TBA</td>
            <td>-</td>
        </tr>

        <tr>
            <td>A5a</td>
            <td>Final Mock-up</td>
            <td>N/a</td>
            <td>TBA</td>
            <td>-</td>
        </tr>

        <tr>
            <td>A5b</td>
            <td>Final Site</td>
            <td>Yes</td>
            <td>TBA</td>
            <td>-</td>
        </tr>

         <tr>
            <td class="lastrow">NSCC GRAPHIC DESIGN | 2012-14</td>
        </tr>
    </table>

</div>

然后是CSS:

#content {
width: 100%;
color: white;
}

#assignment-table {
font-size: 16px;
border-spacing: 0!important;
border-collapse: collapse;
}

#assignment-table th {
display: inline-block;
padding: 25px 0;
width: 200px;
text-align: center;
}   

#assignment-table tr {
display: block;
border-bottom: 1px solid #ffffff;
}

#assignment-table tr:last-child {
width: 1000px;
border-bottom: none!important;
}

#assignment-table td {
display: inline-block;
padding: 25px 0;
width: 200px;
text-align: center;
}

.lastrow {
width: 1000px!important;
}

th:nth-child(3) {
background-color: rgba(255,255,255,0.05);
}

td:nth-child(3) {
background-color: rgba(255,255,255,0.05);
font-family: 'source_sans_proitalic';
}

th:nth-child(5) {
background-color: rgba(255,255,255,0.05);
}

td:nth-child(5) {
background-color: rgba(255,255,255,0.05);
font-family: 'source_sans_proitalic';
}

它应该是这样的:

2 个答案:

答案 0 :(得分:1)

您的表格单元格上显示display: inline-block;inline-block会自动在元素之间插入空格。这个空间正在加起来并包裹你的最后一个元素。我会从你的css条目中删除display: inline-block;,因为这是一个它不需要它的表。

CSS:

#assignment-table th {
  //display: inline-block;   <- remove this
  padding: 25px 0;
  width: 200px;
  text-align: center;
}  

#assignment-table td {
  //display: inline-block;  <- remove this
  padding: 25px 0;
  width: 200px;
  text-align: center;
}

<强> EXAMPLE FIDDLE

答案 1 :(得分:0)

您正在使用display: inline-block上的td删除默认表行为。只需将width: 1000px应用于表格td { width: 20%; }

即可

编辑: here你去了。