0

Say I have the following...

td {
    padding: 8px;
}

 td a {
     display: block;
     padding: 8px;
     width: 100%;
     height: 100%;
 }

I'd like to apply the first css code to all <td> elements that do not have <a> and I'd like to apply the second one to only those that do have <a>.

I tried the following:

td:not(a) {
    padding: 8px;
}

td:not(td a) {
    padding: 8px;
}

How can I make this work without adding classes to the elements?

Howard
  • 3,648
  • 13
  • 58
  • 86

1 Answers1

0

This can be done with JavaScript. Let's say you want any td with an <a> element within (I'll assume directly within for simplicity) to have a yellow background. Here's an example in jQuery:

<table>
  <tr>
    <td>No a elements here</td>
    <td>I have an <a href="#">a</a> element.</td>
  </tr>
</table>

CSS:

td {
  border: 1px solid blue;
}

Script:

$(document).ready(function(){
  $('td a').parent().css('background', 'yellow');
});

Pen at https://codepen.io/wahhabb/pen/QvxEjd

WahhabB
  • 520
  • 3
  • 8