HTML Table Border: Lines, Colors and Height with CSS

November 11, 2026 · 10 min read

HTML Table Border: Lines, Colors and Height with CSS

In modern pages, an HTML table border comes from CSS, not from the table markup. Put a border on the table and on every th and td, then set border-collapse: collapse so neighboring cells share one line. This guide explains the browser defaults behind that fix, then covers line patterns, border colors, row colors and table height.

Why an HTML Table Has No Border by Default

Browsers draw tables with no lines at all. The HTML rendering rules give every table border-spacing: 2px and border-collapse: separate, plus padding: 1px on each cell. No border is set, so you only see text floating in a loose grid.

Here's a plain table with a header row and three body rows. If you'd rather not type the markup, the HTML table generator builds it from a grid you edit in your browser. Our HTML table tutorial explains each tag in the markup below.

<table>
  <thead>
    <tr><th>Plan</th><th>Storage</th><th>Users</th></tr>
  </thead>
  <tbody>
    <tr><td>Starter</td><td>10 GB</td><td>1</td></tr>
    <tr><td>Team</td><td>100 GB</td><td>10</td></tr>
    <tr><td>Business</td><td>1 TB</td><td>50</td></tr>
  </tbody>
</table>

A common first attempt is table { border: 1px solid black; }. That draws a frame around the outside and nothing between the cells, because a border on the table belongs to the table box only. You need three rules to get a proper grid.

table {
  border-collapse: collapse;
  border: 2px solid #334155;
}
th, td {
  border: 1px solid #94a3b8;
  padding: 8px 12px;
}

This renders a dark 2px frame with thin gray lines between every cell. Each line is single, and the padding moves the text away from the lines so the table is easy to scan.

border-collapse vs separate: Single Lines or Double Lines

The border-collapse property picks one of two border models. With separate, the default, every cell keeps its own box and its own border. Adjacent cells sit apart by the border-spacing gap, so two touching cells show two parallel lines. That's the doubled look most people want to get rid of.

The gap itself is adjustable. border-spacing takes one or two lengths, so border-spacing: 4px 8px puts 4px between columns and 8px between rows. A spaced grid like that suits card-style tables, where each cell reads as its own tile.

With collapse, neighboring cells share a single border, as MDN's border-collapse reference describes. Two things change when you switch to collapse:

  • border-spacing stops doing anything, so set gaps with padding instead.
  • border-radius no longer applies to the table, so rounded corners disappear.

To round the corners, stay in the separate model, set the spacing to zero and draw cell lines on one side only. Otherwise each inner line doubles up.

table {
  border-collapse: separate;
  border-spacing: 0;
  border: 1px solid #94a3b8;
  border-radius: 8px;
}
th, td {
  padding: 8px 12px;
  border-bottom: 1px solid #94a3b8;
}
tbody tr:last-child td {
  border-bottom: none;
}

The result is a rounded outline with a single line under each row. If the header row has a background color, give the two top corner cells the same radius so the color doesn't poke past the curve.

How Do You Change the Border Color and Style of an HTML Table?

Use the border shorthand, which takes a width, a style and a color in any order. 1px dashed #2563eb gives a thin blue dashed line. The styles you'll use most are solid, dashed, dotted and double. A double line needs at least 3px of width, because the two strokes and the gap between them share that width.

Defining the color once as a custom property keeps the frame and the inner lines in step.

table {
  --line: #2563eb;
  border-collapse: collapse;
  border: 3px double var(--line);
}
th, td {
  border: 1px solid var(--line);
}

That gives a blue double frame around single blue cell lines. Change --line and every line follows.

In the collapsed model, two borders often meet on one edge, and CSS has a fixed tie-breaker. The CSS 2.1 table rules say the wider border wins. At equal widths, double beats solid, which beats dashed, then dotted. When only the color differs, a cell's border beats its row's, and the row beats the table. That's why a thicker frame on the table still shows around thinner cell borders.

Keep inner lines light but visible. A very pale gray line can vanish on a bright laptop screen, so test your colors on more than one display.

How Do You Add a Border to an HTML Table Without CSS?

The old way is the border attribute: <table border="1">. Browsers still draw borders for it, because the HTML standard tells them how. The rendering rules map the number to the table's border width in pixels, then give the table an outset style and every cell a 1px inset border. Combined with the 2px default spacing, that produces the dated, raised 3D grid you see on old web pages.

Use it if you have no stylesheet at all, but know its status. The HTML standard lists border on tables as a non-conforming feature, and MDN marks the matching DOM property as deprecated. The attribute also can't set a line style. The old bordercolor attribute is on the same non-conforming list, so set colors with CSS instead. Here's the modern version of the same table.

table {
  border-collapse: collapse;
  border: 1px solid;
}
th, td {
  border: 1px solid;
  padding: 4px;
}

Leaving out the color makes each line use the text color, so the borders follow your theme. The two related attributes map the same way: cellspacing becomes border-spacing and cellpadding becomes cell padding.

We prefer CSS even for a one-off table. It's the same amount of typing, you can change the look later from one place, and it passes HTML validation.

Table Lines in HTML: Rows Only, Columns Only, or a Header Rule

You don't have to draw a full grid. Most data tables read better with fewer lines, and each pattern below needs only one or two rules on top of border-collapse: collapse.

To draw horizontal lines between rows, give each cell a bottom border.

th, td {
  border-bottom: 1px solid #cbd5e1;
  padding: 8px 12px;
}

A heavier rule under the header needs one override on the header cells.

thead th {
  border-bottom: 2px solid #0f172a;
}

Vertical lines between columns come from the adjacent-sibling selector. It skips the first cell of each row, so the outer edge stays clean.

th + th, td + td {
  border-inline-start: 1px solid #cbd5e1;
}

These rules go on cells for a reason. In the separated model, browsers ignore borders on tr, thead and tbody, so a tr border only shows once you collapse. Cell borders work in both models. These patterns replace the obsolete rules and frame attributes, which did the same job in HTML 4.

How Do You Color Rows and Cells in an HTML Table?

Set background-color on the element you want to tint. A tinted header, striped body rows and a hover highlight take three rules.

thead th {
  background-color: #1e3a8a;
  color: #ffffff;
}
tbody tr:nth-child(even) {
  background-color: #eff6ff;
}
tbody tr:hover {
  background-color: #dbeafe;
}
td.alert {
  background-color: #fee2e2;
}

This renders a dark blue header with white text and light blue zebra stripes on every second body row. The row under the pointer turns a stronger blue, and any cell with class="alert" gets a red tint.

Table backgrounds stack in layers. A cell's background paints over its row's, and the row's paints over the table's. So the td.alert color shows even inside a striped row. To color a whole row, target the tr. To color a single cell, target the td.

Check text contrast after tinting. WCAG 2.2 Level AA asks for at least 4.5:1 between normal text and its background.

How Do You Set the Height of an HTML Table or Row?

Set height on the table or on a tr, but expect it to act as a minimum. The CSS 2.1 height rules treat any table height other than auto as a minimum. A row's height is the largest of its own height, its cells' heights and what the content needs.

tr {
  height: 48px;
}
td {
  vertical-align: middle;
}

Every row is now at least 48px tall, with text centered vertically. A row holding three lines of text still grows past 48px.

That's the main limitation of HTML table height: you can't force a row shorter than its content. Two habits help. Use cell padding for taller rows, since it always adds space around the text instead of setting a floor that bigger text can pass. To cap the whole table's height, wrap it in a container and let the container scroll.

.table-wrap {
  max-height: 320px;
  overflow: auto;
}
.table-wrap thead th {
  position: sticky;
  top: 0;
  background-color: #ffffff;
}

The table scrolls inside a 320px box, and the sticky header cells stay in view. Give them a background, or the rows scroll visibly behind the header text.

Styling Table Borders When You Write Markdown

Markdown pipe tables have no syntax for borders, colors or row heights. The look comes from whatever displays the file. The Markdown table guide covers the pipe syntax itself.

On GitHub, the rendered HTML goes through a sanitizer. The github/markup README says it strips inline styles, as well as class and id attributes. So none of the CSS in this post shows up in a README, even inside a raw HTML table.

On your own site, you control the CSS. Convert the Markdown with the Markdown to HTML converter, which runs in your browser and returns a full HTML document. Copy the table into your page, then add the border rules from this post to your stylesheet. Our guide to Markdown CSS themes shows how to style the whole converted page.

The demo below holds a raw HTML table with inline styles: a collapsed blue border, a tinted header, a striped row and a taller last row. Edit the colors and widths and watch the preview update. Keep the table free of blank lines, because a blank line ends a raw HTML block in Markdown.

<table style='border-collapse:collapse;border:2px solid #1d4ed8'> <tr><th style='border:1px solid #1d4ed8;padding:6px;background:#dbeafe;color:#111'>Fruit</th><th style='border:1px solid #1d4ed8;padding:6px;background:#dbeafe;color:#111'>Stock</th></tr> <tr><td style='border:1px solid #1d4ed8;padding:6px'>Apple</td><td style='border:1px solid #1d4ed8;padding:6px'>42</td></tr> <tr style='background:#eff6ff;color:#111;height:56px'><td style='border:1px solid #1d4ed8;padding:6px'>Pear</td><td style='border:1px solid #1d4ed8;padding:6px'>17</td></tr> </table>

27 words561 characters5 lines
Markdown

Short Answers on HTML Table Border Problems

An HTML table border takes three CSS rules: a border on the table, a border on the cells, and border-collapse: collapse. From there, the same few properties give you row-only lines, color themes, zebra stripes and minimum row heights. Build the markup with the HTML table generator, add the CSS from this post, and draft the docs around it in the editor.