首页 > 解决方案 > CSS 颜色父行

问题描述

我是一名 CSS 初学者,我需要对包含<th>具有特定 ID的单元格的 HTML 表格的一行进行着色

如何访问我的父元素th:id=yeah以添加此 css 选项:background-color:red

<table style="width:100%">
  <tr>
    <th id="yeah">Firstname</th>
    <th>Lastname</th>
    <th>Age</th>
  </tr>
  <tr>
    <td id="one">Jill</td>
    <td>Smith</td>
    <td>50</td>
  </tr>
  <tr>
    <td>Eve</td>
    <td>Jackson</td>
    <td>94</td>
  </tr>
</table>

<style>
  #one{
    background-color: red
  }

  tr#yeah{
    background-color: blue
  }

  table.class-name th:first-child {
    background: red;
}
</style>

标签: htmlcss

解决方案


you were so close!

your CSS:

tr#yeah{
background-color: blue;
}

Change:

#yeah{
background-color: blue;
}

Little thing to keep in mind. When styling elements in CSS you don't have to always include the TAG ( )

CSS knows you're styling a specific element by its Tag ID (#yeah) which is why you give your element a ID in html.

Good luck out there :D happy learning.


推荐阅读