【问题标题】:Force child to inherit parent color on hover强制子级在悬停时继承父级颜色
【发布时间】:2017-10-28 13:44:15
【问题描述】:

我有一个表格,其中每一列的文本部分都有不同的颜色。

我喜欢在行悬停时用背景突出显示,并将所有文本转换为一种颜色,而不是正常显示时它们各自的颜色。

下面是我的代码。你会看到紫色的、很棒的、黑色的文字。然后当它悬停时,只有黑色文本变为红色。

现在我尝试为每个 <td> 列悬停,但没有奏效。悬停颜色只会在鼠标专门悬停在 TD 单元格上时更改文本颜色,而不是当悬停在该特定行的任何单元格上时。

.musicListTracks h5 {
  padding: 0px;
  margin: 0px;
}

.musicListTracks tr:hover {
  background-color: #000000;
  color: red !important;
  cursor: pointer;
}

.musicListTracks tr td:nth-child(1) span {
  color: #8470FF;
  cursor: pointer;
}

.musicListTracks tr td:nth-child(2) h5 {
  color: darkgrey;
}
<table class="musicListTracks" cellpadding=5 cellspacing=0>
  <tr>
    <td><span>cell 1</span></td>
    <td>
      <h5>cell 2</h5>
    </td>
    <td>
      <h5>cell 3</h5>
    </td>
  </tr>
  <tr>
    <td><span>cell 1b</span></td>
    <td>
      <h5>cell 2b</h5>
    </td>
    <td>
      <h5>cell 3b</h5>
    </td>
  </tr>
  <tr>
    <td><span>cell 1c</span></td>
    <td>
      <h5>cell 2c</h5>
    </td>
    <td>
      <h5>cell 3c</h5>
    </td>
  </tr>
</table>

非常感谢任何帮助。

【问题讨论】:

    标签: html css css-selectors


    【解决方案1】:

    您为前两个单元格定义了颜色,但没有为第三个单元格定义颜色。这就是为什么只有第三个单元格在悬停时呈现红色。

    而不是这个:

    .musicListTracks tr:hover {
      background-color: #000000;
      color: red !important;
      cursor: pointer;
    }
    

    试试这个:

    .musicListTracks tr:hover {
      background-color: #000000;
      cursor: pointer;
    }
    
    .musicListTracks tr:hover * {
      color: red !important;
    }
    

    .musicListTracks h5 {
      padding: 0px;
      margin: 0px;
    }
    
    .musicListTracks tr:hover {
      background-color: #000000;
      cursor: pointer;
    }
    
    .musicListTracks tr:hover * {
      color: red !important;
    }
    
    .musicListTracks tr td:nth-child(1) span {
      color: #8470FF;
      cursor: pointer;
    }
    
    .musicListTracks tr td:nth-child(2) h5 {
      color: darkgrey;
    }
    <table class="musicListTracks" cellpadding=5 cellspacing=0>
      <tr>
        <td><span>cell 1</span></td>
        <td>
          <h5>cell 2</h5>
        </td>
        <td>
          <h5>cell 3</h5>
        </td>
      </tr>
      <tr>
        <td><span>cell 1b</span></td>
        <td>
          <h5>cell 2b</h5>
        </td>
        <td>
          <h5>cell 3b</h5>
        </td>
      </tr>
      <tr>
        <td><span>cell 1c</span></td>
        <td>
          <h5>cell 2c</h5>
        </td>
        <td>
          <h5>cell 3c</h5>
        </td>
      </tr>
    </table>

    【讨论】:

    • 哦,Michael_B 非常感谢。我昨晚花了几个小时,今天又花了几个小时试图找到答案......我非常感谢你的时间和你的答案。它就像一个冠军!
    【解决方案2】:

    Michael 的回答是正确的,因为您没有针对 tds 中的所有元素。他的回答使用通配符来定位 td 中的任何元素。如果这是您想要的,那么他的答案将是最好的。如果您只想定位 span 和 h5,可以使用以下内容:

    .musicListTracks tr:hover {
        background-color: #000000;
        cursor: pointer;
    }
    
    .musicListTracks tr:hover td {
        color: red !important;
    }
    
    .musicListTracks tr:hover h5 {
        color: red !important;
    }
    

    【讨论】:

    • 非常感谢您抽出宝贵的时间和您的回答。我确实想针对所有单元格...如果您想分别针对单个单元格,我发现您的解决方案也可以。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-07
    • 2020-07-10
    • 1970-01-01
    • 2015-01-10
    • 1970-01-01
    • 1970-01-01
    • 2022-08-20
    相关资源
    最近更新 更多