75

I want to style the last TD in a table without using a CSS class on the particular TD.

<table>
  <tbody>
    <tr>
      <td>One</td>
      <td>Two</td>
      <td>Three</td>
      <td>Four</td>
      <td>Five</td>
    </tr>
  </tbody>
</table>

table td 
{ 
  border: 1px solid black;
}

I want the TD containing the text "Five" to not have a border but again, I do not want to use a class.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • What is the reason you don't want to use class? – ya23 Dec 11 '08 at 16:19
  • I want to keep the html static and be able to change the style without adding code or a bunch of css classes and IDs. –  Dec 11 '08 at 16:58
  • You could always just put `class="last"` on all the last cells and then never change it, and use it or not use it as your design calls for. – Nicole Feb 22 '10 at 23:54

15 Answers15

95

The :last-child selector should do it, but it's not supported in any version of IE.

I'm afraid you have no choice but to use a class.

newfurniturey
  • 37,556
  • 9
  • 94
  • 102
yoavf
  • 20,945
  • 9
  • 37
  • 38
  • You spoke too soon, apparently there was a hack to avoid using a class =) – Albert Dec 11 '08 at 22:33
  • 4
    Not entirely true, >IE8 does support this, but is not well adopted yet. If you can live without IE8 support (which OP seems to be fine with), this is the best solution. – Jesse Sep 06 '11 at 20:59
  • 1
    A bit out of date. Works great! – dbrin Dec 19 '13 at 19:49
43

You can use the :last-of-type pseudo-class:

tr > td:last-of-type {
    /* styling here */
}

See the MDN for more info and compatibility with the different browsers.
Check out the W3C CSS guidelines for more info.

Alba Mendez
  • 4,432
  • 1
  • 39
  • 55
Matthew Layton
  • 39,871
  • 52
  • 185
  • 313
39

You can use relative rules:

table td + td + td + td + td {
  border: none;
}

This only works if the number of columns isn't determined at runtime.

sblundy
  • 60,628
  • 22
  • 121
  • 123
  • It has its uses, especially if you, like this guy, don't want to have to change the HTML too much to support the CSS. – sblundy Dec 11 '08 at 16:05
  • That works! Thanks. Yes the HTML is static and I don't want to add a bunch of css classes or id to style the cells. –  Dec 11 '08 at 16:07
  • Amusing trick, but it is less flexible than :last-child and doesn't work better on IE... Still worth remembering! – PhiLho Dec 11 '08 at 16:17
  • 1
    Why wouldn't `:last-child` be the obvious solution if you don't care about IE support? This is just ugly IMO... – Jesse Sep 06 '11 at 20:59
  • What if I have a table with a lot of ``? What if I use php to display data in table from MySQL Database? Do I need to put a lot of `td` to the CSS? – Mai Feb 23 '15 at 02:15
29

you could use the last-child psuedo class

table tr td:last-child {
    border: none;
}

This will style the last td only. It's not fully supported yet so it may be unsuitable

Neil Aitken
  • 7,856
  • 3
  • 41
  • 40
22

try with:

tr:last-child td:last-child{
    border:none;
    /*any other style*/
}

this will only affect the very last td element in the table, assuming is the only one (else, you'll just have to identify your table). Though, is very general, and it will adapt to the last TD if you add more content to your table. So if it is a particular case

td:nth-child(5){
    border:none;
}

should be enough.

Kurt Poehler
  • 321
  • 3
  • 6
  • 1
    Please provide more information and details of how this solves the question. This will help the OP and other future searchers. – SnareChops Jan 29 '16 at 00:39
  • Perfect. Helps a lot with styling responsive tables. tr:last-child td:last-child == last tr in table, last td in tr – André R. Kohl Aug 18 '20 at 11:56
16

If you are already using javascript take a look at jQuery. It supports a browser independent "last-child" selector and you can do something like this.

$("td:last-child").css({border:"none"})
joshperry
  • 41,167
  • 16
  • 88
  • 103
  • 1
    no, last-child means the last child of the parent, so my selector says "give me the last child of the row" – joshperry Sep 18 '09 at 20:13
  • Thanks, I used it to bold a Total line in table. Very easy. – Rick Oct 22 '09 at 14:56
  • 1
    For many cases this works perfectly, but just beware if you are changing classes, remember that this will set the style on that item once, and that property won't change dynamically with CSS class changes anymore. – Nicole Feb 22 '10 at 22:58
  • I think nickf is right. From what I tested and the documentation at http://api.jquery.com/last-child-selector/ . – Code Commander Dec 09 '10 at 20:21
  • @Code, Yes I think you are correct. When I wrote this answer originally the documentation was a little ambiguous. – joshperry Dec 09 '10 at 21:09
4

Javascript is the only viable way to do this client side (that is, CSS won't help you). In jQuery:

$("table td:last").css("border", "none");
nickf
  • 537,072
  • 198
  • 649
  • 721
3

You can use the col element as specified in HTML 4.0 (link). It works in every browser. You can give it an ID or a class or an inline style. only caveat is that it affects the whole column across all rows. Example:

<table>
    <col />
    <col width="50" />
    <col id="anId" />
    <col class="whatever" />
    <col style="border:1px solid #000;" />
    <tbody>
        <tr>
            <td>One</td>
            <td>Two</td>
            <td>Three</td>
            <td>Four</td>
            <td>Five</td>
        </tr>
    </tbody>
</table>
Darko
  • 38,310
  • 15
  • 80
  • 107
3

You can use the :last-of-type to catch last column of your table.

<style>
.table > tbody > tr > td:last-of-type {
    /* Give your style Here; */
}
</style>
Habib_95
  • 51
  • 2
2

In jQuery, provided the table is created either statically or dynamically prior to the following being executed:

$("table tr td:not(:last-child)").css({ "border-right":"1px solid #aaaaaa" });

Just adds a right border to every cell in a table row except the last cell.

1

Not a direct answer to your question, but using <tfoot> might help you achieve what you need, and of course you can style tfoot.

Kevin Le - Khnle
  • 10,579
  • 11
  • 54
  • 80
1

For IE, how about using a CSS expression:

<style type="text/css">
table td { 
  h: expression(this.style.border = (this == this.parentNode.lastChild ? 'none' : '1px solid #000' ) );
}
</style>
Liggy
  • 1,181
  • 2
  • 11
  • 19
1

I was looking for a way to do this too and found this, could be useful for other people:

#table td:last-of-type { border: none; }

Note that it's not supported by IE either.

rachel
  • 377
  • 4
  • 9
0

This is the code that will add border for all the nodes and will remove the border for the last node(TD).

<style type="text/css">
    body {  
        font-family:arial;font-size: 8pt;  
    }  
    table td{
        border-right: #666 1px solid
    }  

    table td {  
        h: expression(this.style.border = (this == this.parentNode.lastChild ? 'none' : 'border-right:  0px solid' ) );  
    }  
</style>
<table>
    <tr>
        <td>Home</td>
        <td>sunil</td>
        <td>Kumar</td>
        <td>Rayadurg</td>
        <td>Five</td>
        <td>Six</td>
    </tr>
</table>

Enjoy ...

I want the same instead of border I wanted it using images ... :-)

Barnee
  • 3,212
  • 8
  • 41
  • 53
0

There is also a different approach.. and this would work for tables that aren't static... basically use <th> instead of <td> for that column:

<style type="text/css">
 table td { border: 1px solid black; }
 table th { border: 0px; }
<style>
<table>
  <tbody>
    <tr>
      <td>One</td>
      <td>Two</td>
      <td>Three</td>
      <td>Four</td>
      <th>Five</th>
    </tr>
    <tr>
      <td>One</td>
      <td>Two</td>
      <td>Three</td>
      <td>Four</td>
      <th>Five</th>
    </tr>
  </tbody>
</table>
Mottie
  • 84,355
  • 30
  • 126
  • 241