Using CSS how to change only the 2nd column of a table

后端 未结 5 1285
悲&欢浪女
悲&欢浪女 2021-01-30 00:20

Using css only, how can I override the css of only the 2nd column of a table.

I can get to all of the columns using:

.countTable table table td
<         


        
相关标签:
5条回答
  • 2021-01-30 00:53

    Try this:

    .countTable table tr td:first-child + td
    

    You could also reiterate in order to style the others columns:

    .countTable table tr td:first-child + td + td {...} /* third column */
    .countTable table tr td:first-child + td + td + td {...} /* fourth column */
    .countTable table tr td:first-child + td + td + td +td {...} /* fifth column */
    
    0 讨论(0)
  • 2021-01-30 01:05

    To change only the second column of a table use the following:

    General Case:

    table td + td{  /* this will go to the 2nd column of a table directly */
    
    background:red
    
    }
    

    Your case:

    .countTable table table td + td{ 
    
    background: red
    
    }
    

    Note: this works for all browsers (Modern and old ones) that's why I added my answer to an old question

    0 讨论(0)
  • 2021-01-30 01:06

    on this web http://quirksmode.org/css/css2/columns.html i found that easy way

    <table>
    <col style="background-color: #6374AB; color: #ffffff" />
    <col span="2" style="background-color: #07B133; color: #ffffff;" />
    <tr>..
    
    0 讨论(0)
  • 2021-01-30 01:10

    You can use the :nth-child pseudo class like this:

    .countTable table table td:nth-child(2)
    

    Note though, this won't work in older browsers (or IE), you'll need to give the cells a class or use javascript in that case.

    0 讨论(0)
  • 2021-01-30 01:16

    You could designate a class for each cell in the second column.

    <table>
    <tr><td>Column 1</td><td class="col2">Col 2</td></tr>
    <tr><td>Column 1</td><td class="col2">Col 2</td></tr>
    <tr><td>Column 1</td><td class="col2">Col 2</td></tr>
    <tr><td>Column 1</td><td class="col2">Col 2</td></tr>
    </table>
    
    0 讨论(0)
提交回复
热议问题