Skip to content

HTML Table Border: Add and Style Table Borders with CSS

A new HTML table has no lines at all. Add them with CSS: table, th, td { border: 1px solid #ccc; }, then table { border-collapse: collapse; } so neighboring cells share one line instead of drawing two. Below are the other looks people ask for (lines between rows only, rounded corners, stripes), each as a live example you can edit.

Borders on every cell

Three rules do it. The first draws a line around the table and around every cell. The second merges the lines that touch, and the third gives the text some room so it doesn’t sit right against the border.

A table with borders

Change #ccc to any color, or 1px to 2px, and watch the preview. Header cells (<th>) are bold and centered by default, which is why “Coffee” and “Price” look different from the rest.

The examples run in a locked box: scripts are off, nothing is sent anywhere, and your edits go away when you reload. To keep one, copy it into the HTML Editor.

Why you get double lines

Leave out border-collapse and every cell keeps its own border, with a small gap between them (2px by default). So you see two thin lines next to each other everywhere two cells meet. Here’s the same table both ways:

Without border-collapse
With border-collapse: collapse

border-collapse has two values. separate is the default: boxes with gaps. collapse merges the borders into a single grid of lines, which is what most people want. It goes on the <table>, not on the cells.

When two collapsed borders don’t match, the browser draws one of them. The wider one wins. If they’re the same width, the style decides (solid beats dashed, and dashed beats dotted), and if those match too, the cell’s border beats the row’s, and the row’s beats the table’s.

The old border="1" attribute

You’ll still see this in old tutorials and emails. It works: browsers draw it to this day.

border=&quot;1&quot; on the table

Look closely and it’s the double-line look from above: a 1px raised border around the table, a 1px sunken border around each cell, and a 2px gap between them. It’s also obsolete in HTML, so validators flag it, and you can’t pick a color or a style with it. Use the CSS version instead. The same goes for its old friends: cellpadding is padding on the cells now, cellspacing is border-spacing, and bordercolor is part of border.

One exception: HTML emails. Some mail apps strip <style> blocks, so email templates often keep border="0" and cellpadding on layout tables, plus inline style attributes. For web pages, stick to CSS.

Border color, width and style

border is a shorthand for three things at once: the width, the style and the color, in any order. You can give the table and the cells different borders. Here the table gets a navy double frame and the cells get light blue dashes:

Double outside, dashed inside

Try swapping dashed for dotted, solid or double. A double border needs at least 3px, or there’s no room for two lines and a gap. The other styles are groove, ridge, inset and outset, which fake a 3D edge and look dated, and none, which removes a border. For colors, any CSS color works: #ccc, gray, rgb(0 0 0 / 15%) for a soft see-through line. The color picker helps you find a hex code.

Lines between rows only

This is the clean look most modern sites use: no box, just a thin line under each row. Put a bottom border on the cells, and take it off the last row:

Horizontal lines only

Why on the cells and not on the <tr>? A row border only shows up with border-collapse: collapse. Putting it on th, td works in both modes, so it’s the safer habit. text-align: left lines the headers up with the data under them, and width: 100% stretches the table across its container.

Only an outside border

Want a frame around the whole table and nothing inside? Give the border to the table only:

A frame around the table

This is also the usual surprise when people write table { border: 1px solid } and expect a grid. The table’s border goes around the outside, and the cells stay bare until you give them their own.

Vertical lines only

For columns, use the + selector: td + td means “a cell that comes right after another cell”, so every cell except the first in each row gets a line on its left.

Lines between columns

You could put border-right on every cell instead, but then the last column gets a line on its outer edge too, and you need a :last-child rule to remove it again. The + version avoids that.

Rounded table corners

You add border-radius: 10px to the table from the first example, reload, and the corners are still square. That’s because border-radius does nothing while border-collapse: collapse is on. Browsers ignore it, by design. The version that works switches back to separate, sets the gap to zero, and draws the inside lines by hand:

A table with rounded corners
  • border-collapse: separate and border-spacing: 0 keep the cells touching, and let the radius work.
  • The table draws the outer border and the rounded corners. overflow: hidden clips the blue header background so it doesn’t poke out of the top corners.
  • Cells only get a top or left line (td and th + th, td + td), so no two borders sit side by side and every line stays 1px.

Another way: wrap the table in a <div> with the border, the radius and overflow: hidden, and keep border-collapse: collapse on the table inside. Then remove the table’s own outer borders so they don’t double up with the div’s.

Zebra stripes

Striped rows make long tables easier to follow across. :nth-child(even) picks every second row:

Every other row shaded

The rule targets tbody tr, so the header row is never counted. Use odd instead of even to start the stripes on the first row. Keep the stripe color light, so the text on it stays easy to read. The background color tutorial has more on coloring cells.

Space inside the cells

Borders look cramped when the text touches them. padding on the cells sets the space between the text and the line. The same cell, tight and roomy:

1px 3px vs 10px 16px of padding

Two values mean top and bottom first, then left and right. Something like 6px 12px works for most tables. Padding on the <table> itself doesn’t pad the cells, so put it on th, td.

Put the header cells in <thead> and they’re easy to style on their own. A dark row with a thick colored line under it, and a <caption> that gives the table a title:

A dark header with a lime line

The caption belongs to the table, so screen readers announce it along with the table. If the table has a heading-style title, use a caption rather than a separate paragraph above it.

Gaps between cells with border-spacing

border-spacing sets the gap between cells, and it only works with border-collapse: separate. With a gap and rounded cells, a table turns into a set of tiles:

border-spacing: 6px

One value sets the same gap both ways. Two values set them separately, across first, then down: border-spacing: 12px 4px. It’s the modern version of the old cellspacing attribute.

Wide tables on phones

A table with many columns won’t shrink below the width of its content, so on a phone it pushes the whole page sideways. Wrap it in a <div> that scrolls on its own:

A table that scrolls sideways

overflow-x: auto adds a scrollbar only when the table doesn’t fit. The extra attributes help keyboard users: tabindex="0" lets them focus the box and scroll it with the arrow keys, and role="region" with an aria-label tells screen reader users what they’ve landed on. white-space: nowrap keeps each cell on one line; drop it if you’d rather let long text wrap.

Common mistakes

The one that makes people think borders are broken: a border with a width and a color, but no style. The style defaults to none, so nothing is drawn.

A border without a style
  • Border on the table only. You get a frame and no grid. Add th, td to the rule.
  • Border on the <tr> with no collapse. It isn’t drawn. Turn on border-collapse: collapse, or put the border on the cells.
  • border-collapse on the cells. It only does something on the <table>.
  • Rounded corners with collapse. The radius is ignored. See rounded corners for the version that works.
  • Mixing border="1" with CSS. The attribute’s 3D borders show through wherever your CSS doesn’t override them. Delete the attribute and style everything with CSS.
  • Using a table for page layout. Tables are for data in rows and columns. For columns of content, use divs with flexbox or grid; the div and span tutorial shows how.

If you’d rather not write the table by hand at all, the HTML Table Generator builds one from a grid you type into, and CSV to HTML Table turns a spreadsheet into one, borders included.

Questions people ask

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

Use CSS: table, th, td { border: 1px solid #ccc; } draws a line around the table and every cell, and table { border-collapse: collapse; } merges the lines where cells meet so you get a single grid instead of double lines. Add padding: 6px 12px to th, td so the text doesn't touch the borders.

Why does my table have double borders?

Because border-collapse is separate by default: every cell draws its own border and there is a 2px gap between cells, so two lines sit side by side wherever cells meet. Put border-collapse: collapse on the <table> and the borders merge into one line.

Is border="1" still OK to use?

Browsers still draw it, but the border attribute on a table is obsolete in HTML and validators flag it. It also gives you an old 3D look you can't change. Use CSS instead: table, th, td { border: 1px solid #ccc; } with border-collapse: collapse. The one place you still see it is HTML email, where some mail apps strip style blocks.

How do I show borders only between rows?

Give the cells a bottom border and remove it from the last row: th, td { border-bottom: 1px solid #ddd; } and tr:last-child td { border-bottom: 0; }, with border-collapse: collapse on the table. Putting the border on the cells works in both collapse modes; a border on a <tr> only shows when border-collapse is collapse.

How do I make rounded corners on an HTML table?

border-radius is ignored while border-collapse is collapse. Switch to border-collapse: separate with border-spacing: 0, put the border, the border-radius and overflow: hidden on the table, and draw the inside lines with border-top on td and border-left on th + th, td + td. Or wrap the table in a div with the radius and overflow: hidden.

How do I change the color of a table border?

Change the color in the border rule: th, td { border: 1px solid #2563eb; }. The border shorthand takes a width, a style and a color in any order, and any CSS color works: a hex code, a name like gray, or rgb(0 0 0 / 15%) for a soft see-through line.

Why is my table border not showing?

The most common reasons: the border has no style (border: 1px #ccc draws nothing, because the style defaults to none; add solid), the border is only on the table so the cells have none, or it is on a <tr> while border-collapse is separate. Also check that another rule further down isn't setting border: none.

What is the difference between border-collapse and border-spacing?

border-collapse decides whether cells share their borders (collapse) or keep their own with a gap between them (separate, the default). border-spacing sets the size of that gap, like border-spacing: 6px or 12px 4px for across and down, and it only works with border-collapse: separate.

How do I stop a wide table from breaking the page on a phone?

Wrap it in a div with overflow-x: auto. The table then scrolls sideways inside the div instead of pushing the whole page wider. Add tabindex="0", role="region" and an aria-label to the div so keyboard and screen reader users can reach and scroll it too.

Tools that help