html - 如果没有 <thead>,则对表的第一列进行不同的样式设置

标签 html css

这就是我要实现的目标:

enter image description here

这是我要设置样式的标记:

  <h2>Table with no header</h2>
  <table>
    <tbody>
      <tr>
        <td>First column - bold</td>
        <td>Second column</td>
      </tr>
      <tr>
        <td>First column - bold</td>
        <td>Second column</td>
      </tr>
    </tbody>
  </table>

  <h2>Table with a header</h2>
  <table>
    <thead>
      <tr>
        <th>Header</th>
        <th>Header</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>First column - not bold</td>
        <td>Second column</td>
      </tr>
      <tr>
        <td>First column - not bold</td>
        <td>Second column</td>
      </tr>
    </tbody>
  </table>

关于如何实现预期结果的任何想法?

最佳答案

这样做,使用直接子选择器 >first-child:not()

table > *:first-child:not(thead) td:first-child {
  font-weight: bold;
}

示例

table > *:first-child:not(thead) td:first-child {
  font-weight: bold;
}
<h2>Table with no header</h2>
<table>
  <tbody>
    <tr>
      <td>First column - bold</td>
      <td>Second column</td>
    </tr>
    <tr>
      <td>First column - bold</td>
      <td>Second column</td>
    </tr>
  </tbody>
</table>

<h2>Table with a header</h2>
<table>
  <thead>
    <tr>
      <th>Header</th>
      <th>Header</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>First column - not bold</td>
      <td>Second column</td>
    </tr>
    <tr>
      <td>First column - not bold</td>
      <td>Second column</td>
    </tr>
  </tbody>
</table>

关于html - 如果没有 <thead>,则对表的第一列进行不同的样式设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40349657/

相关文章:

javascript - 更新一个 JavaScript 对象会更新所有对象

javascript - 将连字符包含到这个正则表达式中,怎么样?

html - 粘性位置会杀死 chrome 77 (osx) 中的滚动条

javascript - 是否有一种跨浏览器的 css 方式来装饰 span block 中的最后一个单词?

javascript (window.open) 在所有浏览器中打开最大化,但在 chrome 中不打开

jquery - 使用 JQuery 将 HTML 插入到 DIV 中

javascript - 将所选行的数据从对话框窗口传递到主页

html - 位置 :fixed menu disappears when using bottom:0

css - Z-Index 结合位置 : fixed and transitions (CSS)

php - php 数组 Pt.2 最后一个元素的不同 css 样式