Giving a border to an HTML table row, <tr>

HtmlCssBorderHtml Table

Html Problem Overview


Is it possible to border a table row, <tr> in one go instead of giving a border to individual cells, <td> like,

        <tr>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
        </tr>

        <tr>
            <td style="border-left: thin solid; border-top: thin solid; border-bottom: thin solid;">&nbsp;</td>
            <td style="border-top: thin solid; border-bottom: thin solid;">&nbsp;</td>
            <td style="border-top: thin solid; border-bottom: thin solid; border-right: thin solid;">&nbsp;</td>
        </tr>

        <tr>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
        </tr>
    </tbody>
</table>

This gives a border around the given <tr> but it requires a border around individual cells.

Can we give a border to <tr> only in one go?

ā†’ jsFiddle

Column 1 Column 2 Column 3

Html Solutions


Solution 1 - Html

You can set border properties on a tr element, but according to the CSS 2.1 specification, such properties have no effect in the separated borders model, which tends to be the default in browsers. Ref.: 17.6.1 The separated borders model. (The initial value of border-collapse is separate according to CSS 2.1, and some browsers also set it as default value for table. The net effect anyway is that you get separated border on almost all browsers unless you explicitly specifi collapse.)

Thus, you need to use collapsing borders. Example:

<style>
table { border-collapse: collapse; }
tr:nth-child(3) { border: solid thin; }
</style>

Solution 2 - Html

Absolutely! Just use

<tr style="outline: thin solid">

on which ever row you like. Here's a fiddle.

Of course, as people have mentioned, you can do this via an id, or class, or some other means if you wish.

Solution 3 - Html

Yes. I updated my answer DEMO

table td {
    border-top: thin solid; 
    border-bottom: thin solid;
}

table td:first-child {
     border-left: thin solid;
}

table td:last-child {
     border-right: thin solid;
}

If you want to style only one <tr> you can do it with a class: Second DEMO

Solution 4 - Html

You can use the box-shadow property on a tr element as a subtitute for a border. As a plus, any border-radius property on the same element will also apply to the box shadow.

box-shadow: 0px 0px 0px 1px rgb(0, 0, 0);

Solution 5 - Html

Make use of CSS classes:

tr.border{
    outline: thin solid;
}

and use it like:

<tr class="border">...</tr>

Solution 6 - Html

Left cell:

style="border-style:solid;border-width: 1px 0px 1px 1px;"

midd cell(s):

style="border-style:solid;border-width: 1px 0px 1px 0px;"

right cell:

style="border-style:solid;border-width: 1px 1px 1px 0px;"

Solution 7 - Html

<table cellpadding="0" cellspacing="0" width="100%" style="border: 1px;" rules="none">
    <tbody>
        <tr>
            <th style="width: 96px;">Column 1</th>
            <th style="width: 96px;">Column 2</th>
            <th style="width: 96px;">Column 3</th>
        </tr>

        <tr>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
        </tr>

        <tr>
            <td style="border-left: thin solid; border-top: thin solid; border-bottom: thin solid;">&nbsp;</td>
            <td style="border-top: thin solid; border-bottom: thin solid;">&nbsp;</td>
            <td style="border-top: thin solid; border-bottom: thin solid; border-right: thin solid;">&nbsp;</td>
        </tr>

        <tr>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
        </tr>
    </tbody>
</table>

Solution 8 - Html

adding border-spacing: 0rem 0.5rem; creates a space for each cell (td, th) items on its bottom while leaving no space between the cells

    table.app-table{
        border-collapse: separate;
        border-spacing: 0rem 0.5rem;
    }
    table.app-table thead tr.border-row the,
    table.app-table tbody tr.border-row td,
    table.app-table tbody tr.border-row th{
        border-top: 1px solid #EAEAEA;
        border-bottom: 1px solid #EAEAEA;
        vertical-align: middle;
        white-space: nowrap;
        font-size: 0.875rem;
    }

    table.app-table thead tr.border-row th:first-child,
    table.app-table tbody tr.border-row td:first-child{
        border-left: 1px solid #EAEAEA;
    }

    table.app-table thead tr.border-row th:last-child,
    table.app-table tbody tr.border-row td:last-child{
        border-right: 1px solid #EAEAEA;
    }

Solution 9 - Html

You can try this (Border Just bottom of every row)

  table {
    border-collapse: collapse;
  }

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

Solution 10 - Html

After fighting with this for a long time I have concluded that the spectacularly simple answer is to just fill the table with empty cells to pad out every row of the table to the same number of cells (taking colspan into account, obviously). With computer-generated HTML this is very simple to arrange, and avoids fighting with complex workarounds. Illustration follows:

<h3>Table borders belong to cells, and aren't present if there is no cell</h3>
<table style="border:1px solid red; width:100%; border-collapse:collapse;">
	<tr style="border-top:1px solid darkblue;">
		<th>Col 1<th>Col 2<th>Col 3
	<tr style="border-top:1px solid darkblue;">
		<td>Col 1 only
	<tr style="border-top:1px solid darkblue;">
		<td colspan=2>Col 1 2 only
	<tr style="border-top:1px solid darkblue;">
		<td>1<td>2<td>3

</table>


<h3>Simple solution, artificially insert empty cells</h3>

<table style="border:1px solid red; width:100%; border-collapse:collapse;">
	<tr style="border-top:1px solid darkblue;">
		<th>Col 1<th>Col 2<th>Col 3
	<tr style="border-top:1px solid darkblue;">
		<td>Col 1 only<td><td>
	<tr style="border-top:1px solid darkblue;">
		<td colspan=2>Col 1 2 only<td>
	<tr style="border-top:1px solid darkblue;">
		<td>1<td>2<td>3

</table>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTinyView Question on Stackoverflow
Solution 1 - HtmlJukka K. KorpelaView Answer on Stackoverflow
Solution 2 - HtmltakendarkkView Answer on Stackoverflow
Solution 3 - HtmlItay GalView Answer on Stackoverflow
Solution 4 - HtmlRichard BonneauView Answer on Stackoverflow
Solution 5 - HtmlFanie ReyndersView Answer on Stackoverflow
Solution 6 - HtmlJuergenView Answer on Stackoverflow
Solution 7 - HtmlMalikView Answer on Stackoverflow
Solution 8 - HtmlMessage AkunnaView Answer on Stackoverflow
Solution 9 - HtmlMujahidul IslamView Answer on Stackoverflow
Solution 10 - HtmlDavid CroweView Answer on Stackoverflow