30

My table has id="mytable". I'm trying to apply some CSS on the first and last <th> only. I tried this but it doesn't work.

#mytable th:first,
#mytable th:last{
 //css goes here
}
1

2 Answers 2

71
#mytable th:last-child, #mytable th:first-child {
 //css goes here
}
5
  • 2
    And :first-child respectively. +1!
    – jwueller
    Dec 21, 2010 at 11:09
  • 1
    Remember :first-child and :last-child attribute selectors are still not supported for IE6 Dec 21, 2010 at 11:10
  • @Alan Are there any stats on % of browsers that still use IE6?
    – sami
    Dec 21, 2010 at 11:17
  • 1
    If you really need assertion to whether you should or shouldn't use something, check out caniuse.com. Whether you should support ancient browsers or not, depends (imho) entirely on your targeted audience.
    – nikc.org
    Dec 23, 2010 at 21:01
  • this selects all th, not only the fist one codepen.io/serhio/pen/LYmaPmE?editors=1100
    – serge
    Oct 14, 2022 at 12:17
-1

For CSS

#myTable thead tr th:first,
#myTable thead tr th:last{
  /*CSS goes here*/
}

Using Jquery (for last child only):

$("#mytable th:last-child").css({

/*CSS goes here*/

});
1
  • 1
    Nothing in question mentions jQuery
    – Yatrix
    Feb 8, 2017 at 17:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.