Search code examples
htmlcssstylingcss-tablesweb-frontend

How to Change the Background colour of a table-column in html and css?


How to Change the Background colour of a table-column in html and css? I know how to change row colour. But I just can't seem to do this.


Solution

  • Use the :nth-child pseudo selector or with :first-child (in your specific case of first column)

    td:nth-child(1) {
      background: red;
    }
    <table>
      <tr><td>a</td><td>1</td></tr>
      <tr><td>b</td><td>2</td></tr>
    </table>

    In your specific case it's also doable with:

    td:first-child {
       background: red;
    }