PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, August 3, 2022

[FIXED] How can you use css to style all <td> elements in a table when one of the <td> elements has a certain class?

 August 03, 2022     css, html, html-table     No comments   

Issue

I need to highlight an entire row of in a table when one of the elements contains a certain class. Example:

.whole-row-red-background {
  background-color: red;
}
<table>
  <tbody>
    <tr>
      <td></td>
      <td></td>
      <td></td>
    </tr>
    <tr>
      <td class='whole-row-red-background'></td>
      <td></td>
      <td></td>
    </tr>
  </tbody </table>

In the example above, only the first cell (the first td) of the second row in the table is set with a red background. Under this scenario (first <td> in the row has this class) I need all <td> elements in the row to have a red background.


Solution

You could use the general sibling selector. More info here: https://developer.mozilla.org/en-US/docs/Web/CSS/General_sibling_combinator

Here is your updated snippet:

.whole-row-red-background {
  background-color: red;
}

.whole-row-red-background ~ td {
  background-color: red;
}
<table>
  <tbody>
    <tr>
      <td></td>
      <td></td>
      <td></td>
    </tr>
    <tr>
      <td class='whole-row-red-background'></td>
      <td></td>
      <td></td>
    </tr>
  </tbody </table>



Answered By - katjass
Answer Checked By - Katrina (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing