在表格的 `` 元素底部如何创建 `
`?

huangapple go评论49阅读模式
英文:

How can i make div in bottom of table td?

问题

如何在HTML表格的td底部创建一个div?

<table style="width:100px;">
    <thead class="text-center">
        <th>Name</th>
        <th>Marks</th>
    </thead>
    <tbody>
    <tr>
        <td>Nikhil</td>
        <td>60<br><div>平均</div></td>
    </tr>
    <tr>
        <td>Akhil</td>
        <td>90<br><div>优秀</div></td>
    </tr>
    <tr>
        <td>Alan</td>
        <td>40<br><div>差</div></td>
    </tr>
    </tbody>
</table>

附带参考图像。

英文:

How can make a div in bottom of td of table in html?

&lt;table style=&quot;width:100px;&quot; &gt;
    &lt;thead class=&quot;text-center&quot;&gt;
        &lt;th&gt;Name&lt;/th&gt;
        &lt;th&gt;Marks&lt;/th&gt;
    &lt;/thead&gt;
    &lt;tbody&gt;
    &lt;tr&gt;
        &lt;td&gt;Nikhil&lt;/td&gt;
        &lt;td&gt;60&lt;br&gt;Average&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
        &lt;td&gt;Akhil&lt;/td&gt;
        &lt;td&gt;90&lt;br&gt;Excellent&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
        &lt;td&gt;Alan&lt;/td&gt;
        &lt;td&gt;40&lt;br&gt;Poor&lt;/td&gt;
    &lt;/tr&gt;
    &lt;/tbody&gt;
&lt;/table&gt;

For reference i have attached image.

在表格的 `<td>` 元素底部如何创建 `<div>`?

TIA

答案1

得分: 2

更新

从语义上讲,我不建议在一个单元格中混合两个值。但是,您可以使用span标记嵌入您的两个值,然后简单地应用display:block属性。然后,您可以在两个td上应用vertical-align:top属性。

.name,
.rating {
  vertical-align: top;
}

.rating span {
  display: block;
  text-align: right;
}

.rating .excellent {
  color: green;
}

.rating .average {
  color: orange;
}

.rating .poor {
  color: red;
}
<table style="width:100px;">
    <thead class="text-center">
        <th>Name</th>
        <th>Marks</th>
    </thead>
    <tbody>
        <tr>
            <td class="name">Nikhil</td>
            <td class="rating">
                <span>60</span>
                <span class="average">Average</span>
            </td>
        </tr>
        <tr>
            <td class="name">Akhil</td>
            <td class="rating">
                <span>90</span>
                <span class="excellent">Excellent</span>
            </td>
        </tr>
        <tr>
            <td class="name">Alan</td>
            <td class "rating">
                <span>40</span>
                <span class="poor">Poor</span>
            </td>
        </tr>
    </tbody>
</table>

您不需要一个div,您可以使用tfoot,它用于定义汇总表格列的一组行:

<table style="width:100px;">
    <thead class="text-center">
        <th>Name</th>
        <th>Marks</th>
    </thead>
    <tbody>
        <tr>
            <td>Nikhil</td>
            <td>60</td>
        </tr>
    </tbody>
    <tfoot>
        <tr>
            <td></td>
            <td>Average</td>
        </tr>
    </tfoot>
</table>

此外,您还可以使用colspan属性扩展平均行:

<table style="width:100px;">
    <thead class="text-center">
        <th>Name</th>
        <th>Marks</th>
    </thead>
    <tbody>
        <tr>
            <td>Nikhil</td>
            <td>60</td>
        </tr>
    </tbody>
    <tfoot>
        <tr>
            <td colspan="2" style="text-align: right;">Average</td>
        </tr>
    </tfoot>
</table>
英文:

Update

Semantically, I do not recommend mixing two values in one cell. However, you can embed your two values using the span tag on which you simply apply the display:block property. Then you apply the vertical-align:top property on both td.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.name, 
.rating {
  vertical-align: top;
}

.rating span {
  display:block;
  text-align:right;
}

.rating .excellent {
  color: green;

}

.rating .average {
  color: orange;
}

.rating .poor {
  color: red;
}

<!-- language: lang-html -->

&lt;table style=&quot;width:100px;&quot; &gt;
    &lt;thead class=&quot;text-center&quot;&gt;
        &lt;th&gt;Name&lt;/th&gt;
        &lt;th&gt;Marks&lt;/th&gt;
    &lt;/thead&gt;
    &lt;tbody&gt;
    &lt;tr&gt;
        &lt;td class=&quot;name&quot;&gt;Nikhil&lt;/td&gt;
        &lt;td class=&quot;rating&quot;&gt;
          &lt;span&gt;60&lt;/span&gt;
          &lt;span class=&quot;average&quot;&gt;Average&lt;/span&gt;
        &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
        &lt;td class=&quot;name&quot;&gt;Akhil&lt;/td&gt;
        &lt;td class=&quot;rating&quot;&gt;
          &lt;span&gt;90&lt;/span&gt;
          &lt;span class=&quot;excellent&quot;&gt;Excellent&lt;/span&gt;
        &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
        &lt;td class=&quot;name&quot;&gt;Alan&lt;/td&gt;
        &lt;td class=&quot;rating&quot;&gt;
          &lt;span&gt;40&lt;/span&gt;
          &lt;span class=&quot;poor&quot;&gt;Poor&lt;/span&gt;
        &lt;/td&gt;
    &lt;/tr&gt;
    &lt;/tbody&gt;
&lt;/table&gt;

<!-- end snippet -->


You don't need a div, you could just use a tfoot, which is used to define a set of rows summarizing the columns of the table:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

&lt;table style=&quot;width:100px;&quot;&gt;
  &lt;thead class=&quot;text-center&quot;&gt;
    &lt;th&gt;Name&lt;/th&gt;
    &lt;th&gt;Marks&lt;/th&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Nikhil&lt;/td&gt;
      &lt;td&gt;60&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
  &lt;tfoot&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;/td&gt;
      &lt;td&gt;Average&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tfoot&gt;
&lt;/table&gt;

<!-- end snippet -->

In addition, you could also expand the average row with the colspan attribute:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

&lt;table style=&quot;width:100px;&quot;&gt;
  &lt;thead class=&quot;text-center&quot;&gt;
    &lt;th&gt;Name&lt;/th&gt;
    &lt;th&gt;Marks&lt;/th&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Nikhil&lt;/td&gt;
      &lt;td&gt;60&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
  &lt;tfoot&gt;
    &lt;tr&gt;
      &lt;td colspan=&quot;2&quot; style=&quot;text-align: right;&quot;&gt;Average&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tfoot&gt;
&lt;/table&gt;

<!-- end snippet -->

答案2

得分: -1

以下是代码的翻译部分:

<table style="width:100px">
    <thead class="text-center">
        <th>Name</th>
        <th>Marks</th>
    </thead>
    <tbody>
        <tr>
            <td>Nikhil</td>
            <td>60</td>
        </tr>
        <tr>
            <td>Danixal</td>
            <td>71</td>
        </tr>
        <tr>
            <td>Clarke</td>
            <td>39</td>
        </tr>
    </tbody>
    <tfoot>
        <td></td>
        <td><div>Average</div></td>
    </tfoot>
</table>
英文:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

&lt;table style=&quot;width:100px;&quot; &gt;
    &lt;thead class=&quot;text-center&quot;&gt;
        &lt;th&gt;Name&lt;/th&gt;
        &lt;th&gt;Marks&lt;/th&gt;
    &lt;/thead&gt;
    &lt;tbody&gt;
    &lt;tr&gt;
        &lt;td&gt;Nikhil&lt;/td&gt;
        &lt;td&gt;60&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
        &lt;td&gt;Danixal&lt;/td&gt;
        &lt;td&gt;71&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
        &lt;td&gt;Clarke&lt;/td&gt;
        &lt;td&gt;39&lt;/td&gt;
    &lt;/tr&gt;
    &lt;/tbody&gt;
    &lt;tfoot&gt;
        &lt;td&gt;&lt;/td&gt;
        &lt;td&gt;&lt;div&gt;Average&lt;/div&gt;&lt;/td&gt;
    &lt;/tfoot&gt;
&lt;/table&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年3月8日 19:29:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/75672441.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定