CSS选择< thead>或< tbody>来自< table>

发布于 2025-01-29 23:16:05 字数 494 浏览 6 评论 0原文

我有以下问题。我有很多桌子,但是它们都有不同的结构:一个具有< table; ;

而且我需要将CSS应用于Thead(如果在表中设置)或Toby(始终设置)。我不应该为他们两个设置样式,而只是为第一个追随< table'标签的样式。

因此,如果我有

<table>
 <thead>...</thead>
   <tbody>...<tbody>
</table>

CSS,则应仅适用于Thead。

反之亦然,如果我有:

<table>
  <tbody>...</tbody>
<table>

则应将CSS应用于Tbody。

我希望有类似的东西“如果设置了thead,那么...'

I have the following problem. I have plenty of tables, but they all have different structure: one have <table><thead><tbody> type, while the others have <table><tbody> only.

And I need to apply css for thead (if it set in table) OR to tbody (it is set always). I shouldn't set styles for both of them, only for the first who goes after <table> tag.

So if I have

<table>
 <thead>...</thead>
   <tbody>...<tbody>
</table>

then CSS should be applied only for thead.

And vice versa if I have:

<table>
  <tbody>...</tbody>
<table>

then CSS should be applied for tbody.

I wish there was something like 'if thead is set then...'

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

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

发布评论

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

评论(1

平生欢 2025-02-05 23:16:05

如果是一个选项,则可以使用 :First-Child 选择器。

例如,您将选择其中一个,这是&lt; table; table; gt;的第一个孩子:

table > thead:first-child, table > tbody:first-child{
  /* properties here */
}

在这种情况下,如果存在thead,则将不是第一个孩子。否则,如果没有thead,则tbody将是第一个孩子。

在行动中看到它:

table > thead tr, /* Select the <tr> in a <thead> if present */
table > tbody:first-child tr:first-child /* Select the first <tr> in a <tbody> when the <thead> is not present */
{
  font-weight: bold;
  color: blue;
}

table{
margin-top:1em;
border: 1px solid black;
}
<table>
  <thead>
    <tr><th>This is a table with header</th></tr>
  </thead>
  <tbody>
    <tr><td>This is the body</td></tr>
  </tbody>
</table>

<table>
  <tbody>
    <tr><td>This is a table without header</td></tr>
  </tbody>
  <tbody>
    <tr><td>This is the body</td></tr>
  </tbody>
</table>

If that is an option, you could use a hack with :first-child selector.

For example, you will select either one of them which is the first child of a <table>:

table > thead:first-child, table > tbody:first-child{
  /* properties here */
}

In this case, if a thead is present, tbody will not be the first child. Otherwise, if there is no thead, tbody will be the first child.

See it in action:

table > thead tr, /* Select the <tr> in a <thead> if present */
table > tbody:first-child tr:first-child /* Select the first <tr> in a <tbody> when the <thead> is not present */
{
  font-weight: bold;
  color: blue;
}

table{
margin-top:1em;
border: 1px solid black;
}
<table>
  <thead>
    <tr><th>This is a table with header</th></tr>
  </thead>
  <tbody>
    <tr><td>This is the body</td></tr>
  </tbody>
</table>

<table>
  <tbody>
    <tr><td>This is a table without header</td></tr>
  </tbody>
  <tbody>
    <tr><td>This is the body</td></tr>
  </tbody>
</table>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文