Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS: how do I have a border-bottom on table rows, except for the last row

Tags:

css

I have a variable number of table rows (n), and I would like the border bottom to apply to rows 0..(n-1)

how do I do that?

like image 708
NullVoxPopuli Avatar asked Sep 09 '10 19:09

NullVoxPopuli


People also ask

How do you remove a border from a specific row?

noBorder td to make the border go away for all the <td> s that are inside of <tr> s with the noBorder class by assigning border: 0 . Note that you do not need to provide the unit (i.e. px ) if you set something to 0 as it does not matter anyway. Zero is just zero.

How do I add a border to a row in a table?

If you want to add a border only to the bottom of the table row, you can apply the CSS border-bottom property to <td> elements that are placed within a <tr> tag.


2 Answers

You have two options: (1) adding a specialized class in the HTML to the last row; or (2) using the :last-child pseudo class in your CSS.

Option 1: Specialized Class

If you can apply classes to your HTML, you can add a specialized class to the final row. If your markup is being generated by a server-side script (eg. a PHP script), you will need to edit that script to add similar markup.

HTML:

<table>    <tr>       <td>       </td>    </tr>    <tr>       <td>       </td>    </tr>    <tr class="last">       <td>       </td>    </tr> </table> 

CSS:

table {    border-collapse:collapse; } tr {    border-bottom: 1px solid #000; } tr.last {    border-bottom: none; } 

Option 2: CSS Pseudo Class

The alternative is to use the :last-child CSS pseudo class. Using the :last-child class doesn't require any changes to the HTML and so may be a better choice if you aren't able to change the HTML. The CSS is almost identical to the above:

CSS:

table {    border-collapse:collapse; } tr {    border-bottom: 1px solid #000; } tr:last-child {    border-bottom: none; } 

The drawback of this approach is that versions of Internet Explorer before 9 don't support the :last-child pseudo class.

like image 99
KP. Avatar answered Sep 19 '22 11:09

KP.


I know this is an old question, but it's worth mentioning that now with CSS3 all you have to do is us the not() selector in your CSS, like this:

tr:not(:last-child) {     border-bottom: 1px solid #E3E3E3; } 
like image 26
Nate Avatar answered Sep 21 '22 11:09

Nate