00 Votes

CSS: Hover effect only for filled table cells

Question by Guest | 2017-05-23 at 16:26

I have a table consisting of cells with or without content. Now, I want to implement some hover effect that should only be active when there is content within the <td> element:

CSS

td:hover {
   background-color: red;
}

HTML

<td>
  <p>I am content</p>
</td>
<td>
  <input type="text" value="I am also content" name="test" />
</td>
<td>
  // No hover effect here!
</td>

In this example, the hover effect should only be applied to the first and the second td-cell. The empty third cell should not have any hover color.

Thank you for your help.

ReplyPositiveNegative
0Best Answer0 Votes

You can work with the CSS selector :empty. This selector selects all empty elements and when combined with :not, you get all not empty elements - that are those elements with content.

In your case it would be:

td:not(:empty):hover {
  background-color: red;
}

That should select all non-empty table cells.
2017-05-23 at 17:51

ReplyPositive Negative
Reply

Related Topics

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.

Participate

Ask your own question or write your own article on askingbox.com. That’s how it’s done.