0

I'm styling a table using Sass and I want to customize the styles of an element with a certain class differently depending on this element's tag (either <td /> or <th />).

I thought of using

.foo {
  /* td styles here */
  & th { /* th styles here */ }
}

but that does not work. (it selects the children of ".foo" that have the th tag)

Is there any way that I can use the Sass' "&" (ampersand) to style elements with the same class depending on their tag?

1 Answers1

1

Use :is()

.foo {
   &:is(td) { /* td styles here */ }
   &:is(th) { /* th styles here */ }
}

the compiled SCSS will result in this CSS

.foo:is(td) {
  color: #9bc
}

.foo:is(th) {
  color: #666
}
<table>
  <tr>
    <th class="foo">heading</th>
    <td class="foo">table cell</td>
  </tr>
</table>
Fabrizio Calderan
  • 120,726
  • 26
  • 164
  • 177