Add border-bottom to table row <tr>
Asked Answered
H

18

557

I have a table of 3 by 3. I need a way to add a border for the bottom of every row tr and give it a specific color.

First I tried the direct way, i.e.:

<tr style="border-bottom:1pt solid black;">

But that didn't work. So I added CSS like this:

tr {
border-bottom: 1pt solid black;
}

That still didn't work.

I would prefer to use CSS because then I don't need to add a style attribute to every row. I haven't added a border attribute to the <table>. I hope that that is not affecting my CSS.

Houselights answered 6/4, 2012 at 8:9 Comment(3)
As a side note, if inline styling (first example in your question) isn't working then it's unlikely that embedded styling (second example) will work. You should also research the availability of attributes for the element you're attempting to style: w3.org/TR/html-markup/tr.htmlHysteresis
If you want to have a border bottom for table's tr you can follow this jsfiddle.net/7awN4Boarder
Just a note to encourage future searchers to scroll down to @Nathan Manousos's answer, below - it's probably the solution you're looking forFare
S
479

I had a problem like this before. I don't think tr can take a border styling directly. My workaround was to style the tds in the row:

<tr class="border_bottom">

CSS:

tr.border_bottom td {
  border-bottom: 1px solid black;
}
Subtangent answered 6/4, 2012 at 8:26 Comment(6)
Note that using this solution you will most likely have a gap in the border between td's. simoncereska's answer takes care of this but be aware that it may not look nice with dotted or dashed border types (because they're not continuous)Diu
You could also remedy this by adding cellspacing="0" as an attribute to <table> (see this question). Don't know how this will look with dotted or dashed borders, though.Ephemeris
tr can take border styling in the collapsing border model. @Sangram, consider accepting an answer which takes that into account instead of this one, no?Oophorectomy
This will not work if there is padding between the table cells. If there is padding then the border will visibly be split int pieces.Neela
and your table need this style <table style="border-collapse:collapse">Brillatsavarin
This answer feels hacky. The answer by Nathan Manousos is far more elegant, fit for purpose, and should be the accepted answer.Harbison
S
774

Add border-collapse:collapse to your table rule:

table { 
    border-collapse: collapse; 
}

Example

table {
  border-collapse: collapse;
}

tr {
  border-bottom: 1pt solid black;
}
<table>
  <tr><td>A1</td><td>B1</td><td>C1</td></tr>
  <tr><td>A2</td><td>B2</td><td>C2</td></tr>
  <tr><td>A2</td><td>B2</td><td>C2</td></tr>
</table>

Link

Skyline answered 6/4, 2012 at 8:14 Comment(7)
This does not work by itself. You still can't style the row, but you can style the cells.Zinfandel
You are wrong, @Renan . The collapsing border model is exactly what makes row borders stylable. According to CSS sectoin 17.6: In the separate border model “Rows, [...] cannot have borders (i.e., user agents must ignore the border properties for those elements).” “In the collapsing border model, it is possible to specify borders that surround all or part of a cell, row [and] row group [...]” And by the way: it does work in my browser (Chromium 37).Oophorectomy
I think some people might be getting confused (like I did) and aren't noticing that the border-collapse style needs to be set on the table, not the row.Fought
Seems that Chrome does not have this feature, though it support others of border-collapse features.Bing
This removed the padding.Crane
I have been trying to get a solid bottom row line to work and I either lose my padding and margin or I get no border. How do I get both ?Interlaken
This works, but boy is it dumb. I don't understand why you can't style <tr> all by itself. This is the kind of thing that I will never remember because it is completely unintuitive.Kymry
S
479

I had a problem like this before. I don't think tr can take a border styling directly. My workaround was to style the tds in the row:

<tr class="border_bottom">

CSS:

tr.border_bottom td {
  border-bottom: 1px solid black;
}
Subtangent answered 6/4, 2012 at 8:26 Comment(6)
Note that using this solution you will most likely have a gap in the border between td's. simoncereska's answer takes care of this but be aware that it may not look nice with dotted or dashed border types (because they're not continuous)Diu
You could also remedy this by adding cellspacing="0" as an attribute to <table> (see this question). Don't know how this will look with dotted or dashed borders, though.Ephemeris
tr can take border styling in the collapsing border model. @Sangram, consider accepting an answer which takes that into account instead of this one, no?Oophorectomy
This will not work if there is padding between the table cells. If there is padding then the border will visibly be split int pieces.Neela
and your table need this style <table style="border-collapse:collapse">Brillatsavarin
This answer feels hacky. The answer by Nathan Manousos is far more elegant, fit for purpose, and should be the accepted answer.Harbison
S
83

Use border-collapse:collapse on table and border-bottom: 1pt solid black; on the tr

Shelbashelbi answered 23/7, 2014 at 9:50 Comment(3)
setting border on the tr is useless even with border collapsed. you have to set it on the tds of the trCords
@RaduSimionescu Wrong, it works perfectly fine on the tr with border collapsed.Noonday
No effect in FF 45.0.1.Lydell
A
56

Use

border-collapse:collapse as Nathan wrote and you need to set

td { border-bottom: 1px solid #000; }

Altimetry answered 6/4, 2012 at 8:26 Comment(1)
Thats the way I would do it too! Add the border on the td and use border-collapse on the tableBiogeochemistry
Z
42

There are lot of incomplete answers here. Since you cannot apply a border to tr tag, you need to apply it to the td or th tags like so:

td {
  border-bottom: 1pt solid black;
}

Doing this will leave a small space between each td, which is likely not desirable if you want the border to appear as though it is the tr tag. In order to "fill in the gaps" so to speak, you need to utilize the border-collapse property on the table element and set its value to collapse, like so:

table {
  border-collapse: collapse;
}
Zagreus answered 31/8, 2016 at 2:52 Comment(0)
L
19

You can use the box-shadow property to fake a border of a tr element. Adjust Y position of box-shadow (below represented as 2px) to adjust thickness.

tr {
  -webkit-box-shadow: 0px 2px 0px 0px rgba(0,0,0,0.99);
  -moz-box-shadow: 0px 2px 0px 0px rgba(0,0,0,0.99);
  box-shadow: 0px 2px 0px 0px rgba(0,0,0,0.99);
}
Lezlie answered 19/2, 2019 at 17:47 Comment(0)
P
11

I tried adding

    table {
      border-collapse: collapse;
    }   

alongside the

    tr {
      bottom-border: 2pt solid #color;
    }

and then commented out border-collapse to see what worked. Just having the tr selector with bottom-border property worked for me!

No Border CSS ex.

No Border CSS ex.

No Border Photo live

No Border Photo live

CSS Border ex.

CSS Border ex.

Table with Border photo live

Table with Border photo live

Phenylalanine answered 18/3, 2019 at 18:52 Comment(1)
bottom-border is not a valid CSS property. I tried the same experiment toggling the border-collapse property in Chrome 84.0.4147.135 on Windows. The border will only show up when the property exists and is set to collapse.Harbison
T
8

Use

table{border-collapse:collapse}
tr{border-top:thin solid}

Replace "thin solid" with CSS properties.

Tripinnate answered 28/12, 2015 at 19:34 Comment(0)
M
6

Display the row as a block.

tr {
    display: block;
    border-bottom: 1px solid #000;
}

and to display alternate colors simply:

tr.oddrow {
    display: block;
    border-bottom: 1px solid #F00;
}
Musicale answered 23/11, 2012 at 19:17 Comment(2)
Not a good idea to set display: block for tr's. Use tr td { border-bottom: ... } ad tr.oddrow td { border-bottom: ... } insteadPalaeontology
display block might destroy the table layout.border-collapse:collapse on the table itself is definitly the best solutionHerby
F
5

If you don't want to

  • enforce border collapse on the table
  • use the TD elements styling

You can use the ::after selector to add borders to TR :

table tbody tr {
    position : relative; # to contain the ::after element within the table-row
}

table tbody tr td {
    position : relative; # needed to apply a z-index
    z-index : 2; # needs to be higher than the z-index on the tr::after element
}

table tbody tr::after {
    content : '';
    position : absolute;
    z-index : 1; # Add a z-index below z-index on TD so you can still select data from your table rows :)
    top : 0px;
    left : 0px;
    width : 100%;
    height : 100%;
    border : 1px solid green; # Style your border here, choose if you want a border bottom, top, left, etc ...
}

It is a simple trick that I used in a scenario where I had to put spaces between table-rows so I wasn't able to add a border collapse on the table, the end result :

enter image description here

Hope it helps :)

Factional answered 15/6, 2021 at 13:46 Comment(1)
This is the best answer, all other solutions require border-collapse: collapse; which imho is a big limitation.Wordage
M
4

Another solution to this is border-spacing property:

table td {
  border-bottom: 2px solid black;
}

table {
  border-spacing: 0px;
}
<table>
 <tr>
   <td>ABC</td>
   <td>XYZ</td>
</table>
Manno answered 4/9, 2018 at 13:19 Comment(0)
C
3

I found when using this method that the space between the td elements caused a gap to form in the border, but have no fear...

One way around this:

<tr>
    <td>
        Example of normal table data
    </td>

    <td class="end" colspan="/* total number of columns in entire table*/">
        /* insert nothing in here */ 
    </td>
</tr>

With the CSS:

td.end{
    border:2px solid black;
}
Codel answered 14/10, 2013 at 20:37 Comment(0)
U
2

<td style="border-bottom-style: solid; border-bottom: thick dotted #ff0000; ">

You can do the same to the whole row as well.

There is border-bottom-style, border-top-style,border-left-style,border-right-style. Or simply border-style that apply to all four borders at once.

You can see (and TRY YOURSELF online) more details here

Unfamiliar answered 17/8, 2016 at 16:48 Comment(0)
Z
2

Several interesting answers. Since you just want a border bottom (or top) here are two more. Assuming you want a blue border 3px thick. In the style section you could add

.blueB {background-color:blue; height:3px} or
hr {background-color:blue; color:blue height:3px}

In the table code either

<tr><td colspan='3' class='blueB></td></tr> or
<tr><td colspan='3'><hr></td></tr>
Zwieback answered 19/6, 2019 at 16:50 Comment(1)
Adding a whole table row and table cell to insert an <hr>? This is an "absolute last resort" type of an answer.Gelhar
C
1

No CSS border bottom:

<table>
    <thead>
        <tr>
            <th>Title</th>
        </tr>
        <tr>
            <th>
                <hr>
            </th>
        </tr>
    </thead>
</table>
Cheiro answered 19/1, 2019 at 8:0 Comment(1)
using <hr> is surely cheating ;)Marandamarasca
M
0

You can't put a border on a tr element. This worked for me in firefox and IE 11:

<td style='border-bottom:1pt solid black'>
Mcmann answered 31/8, 2017 at 18:37 Comment(2)
you can't put a border on a tr. Edited answer to clarify this.Auschwitz
That's the simplest and most accurate answer.Penalty
D
0

Having the border on the <td> instead of the <tr> is usually the way to go. However, if you're working with <td>'s of different heights and it messes up the layout, you could apply a linear gradient to the <tr> to achieve the same thing

table tr, table {
  border-spacing:0;
}
table td {
  padding:12px;
}

/* conventional method - applied to <td> */
table tr:first-child td {
  border-bottom:1px solid black;
}

/* alternative - applied to <tr>*/
table tr:last-child {
  background: linear-gradient(0deg, black 1px, transparent 1px);
}
<table>
  <tr>
    <td>td border bottom</td>
    <td>(conventional)</td>
  </tr>
  <tr>
    <td>tr gradient background</td>
    <td>(alternative)</td>
  </tr>
</table>
Disavowal answered 22/7, 2023 at 18:15 Comment(0)
A
-4

HTML

<tr class="bottom-border">
</tr>

CSS

tr.bottom-border {
  border-bottom: 1px solid #222;
}
Abridgment answered 1/3, 2016 at 7:18 Comment(1)
useless without border-collapse:collapse;Acrid

© 2022 - 2024 — McMap. All rights reserved.