How to select first and last TD in a row?

前端 未结 5 1414
谎友^
谎友^ 2020-11-30 23:01

How can you select the first and the last TD in a row?

tr > td[0],
tr > td[-1] {
/* styles */
}
5条回答
  •  盖世英雄少女心
    2020-11-30 23:04

    You could use the :first-child and :last-child pseudo-selectors:

    tr td:first-child{
        color:red;
    }
    tr td:last-child {
        color:green
    }
    

    Or you can use other way like

    // To first child 
    tr td:nth-child(1){
        color:red;
    }
    
    // To last child 
    tr td:nth-last-child(1){
        color:green;
    }
    

    Both way are perfectly working

提交回复
热议问题