How to Use HTML Table Border Tag: Complete Guide with Code Examples

HTML Tutorials · HTML Table Borders Tag · Introduction

HTML Table Borders Tutorial: Border Attribute, CSS Borders & Styling Explained

HTML tables are used to display data in a structured format, typically in rows and columns. Also main important feature of HTML tables is the ability to add borders, this helps to table cells, rows, and columns, making data easier to read and visually organized.

This tutorial covers six distinct border styles with rendered examples, the CSS properties behind them, how to move from inline styling to a reusable stylesheet class, individual cell borders versus whole-table borders, responsive behavior on small screens, and accessibility basics, plus a live playground further down the page where you can test any border style instantly.

HTML table with border styling example - Trulli
HTML table border example
HTML bordered table rendered in a browser - Trulli
Bordered table in a browser

Table Border Styling Examples

Six practical border styles, from a basic solid line to a rounded, colored border, each with the exact code and its rendered result.

📋 1. Basic Table Border

ExampleCopy Code
<table style="border: 1px solid black;">
  <tr>
    <th>Name</th>
    <th>Age</th>
  </tr>
  <tr>
    <td>John</td>
    <td>30</td>
  </tr>
</table>

Rendered Result

NameAge
John30

🧹 2. Dotted Table Borders

ExampleCopy Code
<table style="border: 2px dotted blue;">
  <tr>
    <th>Item</th>
    <th>Price</th>
  </tr>
  <tr>
    <td>Book</td>
    <td>$10</td>
  </tr>
</table>

Rendered Result

ItemPrice
Book$10

🧹 3. Round Table Borders Example

ExampleCopy Code
<table style="border: 2px solid gray; border-radius: 10px;">
  <tr>
    <th>Language</th>
    <th>Difficulty</th>
  </tr>
  <tr>
    <td>Python</td>
    <td>Easy</td>
  </tr>
</table>

Rendered Result

LanguageDifficulty
PythonEasy

🧹 4. Style Table Borders Example

ExampleCopy Code
<table style="border: 3px double black;">
  <tr>
    <th>Country</th>
    <th>Capital</th>
  </tr>
  <tr>
    <td>India</td>
    <td>New Delhi</td>
  </tr>
</table>

Rendered Result

CountryCapital
IndiaNew Delhi

🧹 5. Collapsed Table Borders Example

ExampleCopy Code
<table style="border: 1px solid black; border-collapse: collapse;">
  <tr>
    <th>Product</th>
    <th>Qty</th>
  </tr>
  <tr>
    <td>Pen</td>
    <td>50</td>
  </tr>
</table>

Rendered Result

ProductQty
Pen50

🧹 6. Border Color Example

ExampleCopy Code
<table style="border: 2px solid; border-color: orange;">
  <tr>
    <th>Day</th>
    <th>Activity</th>
  </tr>
  <tr>
    <td>Monday</td>
    <td>Gym</td>
  </tr>
</table>

Rendered Result

DayActivity
MondayGym

🔍 Table Tag Breakdown

Tag Purpose
<table>Defines the table
<tr>Table row
<th>Table header (bold + centered by default)
<td>Table data (cell content)
<caption>Defines a table caption
<colgroup>Specifies a group of one or more columns in a table for formatting
<col>Specifies column properties for each column within a colgroup element
<thead>Groups the header content in a table
<tbody>Groups the body content in a table

Try It Yourself (Copy This Code and Paste. See how it works). Paste any of the six border examples above into the live playground below and adjust the width, style, or color values to see the change instantly.

</> Try It Yourself (Copy this code and paste. See how it works)

Live Code Preview

CSS Border Properties Quick Reference

Every border example on this page combines three separate CSS values, width, style, and color, into a single shorthand property. Understanding each piece individually makes it much easier to build a custom border from scratch rather than copying an example.

Part Controls Example Values
Width How thick the border line is 1px, 2px, 3px
Style The visual pattern of the line solid, dotted, dashed, double
Color The color of the border line black, #ff6347, orange

Writing border: 2px dotted blue;, as the Dotted Table Borders example earlier on this page does, is really just these three parts written together in width-style-color order, the standard order CSS expects for the border shorthand property.

Responsive Table Borders on Small Screens

A table that looks perfectly organized on a desktop monitor can become cramped or force horizontal scrolling on a narrow phone screen, since table columns do not naturally reflow the way paragraphs do. A couple of simple techniques keep bordered tables usable on smaller devices.

ExampleCopy Code
<div style="overflow-x: auto;">
    <table style="border-collapse: collapse; width: 100%;">
        <tr>
            <th style="border:1px solid #333; padding:6px;">Name</th>
            <th style="border:1px solid #333; padding:6px;">Age</th>
        </tr>
        <tr>
            <td style="border:1px solid #333; padding:6px;">John</td>
            <td style="border:1px solid #333; padding:6px;">30</td>
        </tr>
    </table>
</div>

Wrapping a table in a <div> with overflow-x: auto; lets a wide, bordered table scroll horizontally within its own container on a small screen, rather than breaking the rest of the page's layout or shrinking every column down to an unreadable width.

Real-World Example: A Bordered Pricing Table

To see several border techniques from this page working together, here is a small pricing table combining a custom border color, collapsed borders, and a styled header row:

pricing-table.htmlCopy Code
<table style="border: 2px solid #2563eb; border-collapse: collapse; width: 100%;">
    <tr>
        <th style="border:1px solid #2563eb; padding:8px; background:#eff6ff;">Plan</th>
        <th style="border:1px solid #2563eb; padding:8px; background:#eff6ff;">Price</th>
    </tr>
    <tr>
        <td style="border:1px solid #2563eb; padding:8px;">Basic</td>
        <td style="border:1px solid #2563eb; padding:8px;">$9/mo</td>
    </tr>
    <tr>
        <td style="border:1px solid #2563eb; padding:8px;">Pro</td>
        <td style="border:1px solid #2563eb; padding:8px;">$29/mo</td>
    </tr>
</table>

Reading through this example: the outer table border, individual cell borders, and header background color all use the same blue tone for a cohesive look, border-collapse: collapse; keeps the grid lines clean and single, and padding gives each cell enough breathing room to stay readable. This is close to how a real pricing table on a marketing page would be built, just with inline styles instead of a shared class for simplicity, though moving it into a .pricing-table class in a stylesheet, using the same technique covered earlier on this page, would be the natural next step for a production site.

Try pasting this pricing table into the live editor above and swapping the plan names, prices, and border color to build a version of your own.

Understanding the border-collapse Property

The Collapsed Table Borders example earlier on this page uses a property worth understanding on its own: border-collapse. By default, every table cell draws its own separate border, which means where two cells touch, you actually see two border lines sitting right next to each other, making the overall grid look thicker and slightly uneven.

Value Effect
border-collapse: separate; Default behavior; each cell keeps its own distinct border
border-collapse: collapse; Adjacent cell borders merge into a single shared line

Most real, production tables use border-collapse: collapse;, since a clean, single-line grid generally looks more polished than the doubled default, which is exactly why the reference examples on this page mostly include it.

Styling Table Borders with Internal or External CSS

Every example so far uses an inline style attribute directly on the <table> tag, which is perfect for quick practice, but repeating the same styling on every table across a real website is inefficient. Moving the same rules into an internal or external stylesheet, using a class selector, solves this the same way it did for text styling earlier in this tutorial series.

styled-table.htmlCopy Code
<style>
    .data-table {
        border-collapse: collapse;
        width: 100%;
    }
    .data-table th, .data-table td {
        border: 1px solid #333;
        padding: 8px 12px;
        text-align: left;
    }
    .data-table th {
        background-color: #f0f0f0;
    }
</style>

<table class="data-table">
    <tr>
        <th>Name</th>
        <th>Age</th>
    </tr>
    <tr>
        <td>John</td>
        <td>30</td>
    </tr>
</table>

Every table that uses class="data-table" automatically picks up identical borders, spacing, and header shading, and updating the single .data-table rule in the stylesheet instantly restyles every table on the site that uses it, without touching a single HTML file.

Individual Cell Borders vs Whole-Table Borders

Every example on this page so far has applied a border to the <table> tag itself, but borders can just as easily be applied to individual <th> or <td> cells instead, which is actually necessary once border-collapse: collapse; is in use, since the outer table border alone will not draw lines between individual cells.

ExampleCopy Code
<table style="border-collapse: collapse;">
  <tr>
    <th style="border: 1px solid black; padding: 6px;">Name</th>
    <th style="border: 1px solid black; padding: 6px;">Score</th>
  </tr>
  <tr>
    <td style="border: 1px solid black; padding: 6px;">Amit</td>
    <td style="border: 1px solid black; padding: 6px;">92</td>
  </tr>
</table>

Here, the border is applied to each <th> and <td> individually rather than to the table as a whole, which is exactly the pattern this tutorial page's own rendered previews use behind the scenes, and it is the more common real-world approach once a table has more than a couple of rows.

Common Mistakes With HTML Table Borders

Table Accessibility Basics

The Table Tag Breakdown above lists several tags, <caption>, <thead>, and <tbody>, that go beyond pure border styling and genuinely help both screen readers and search engines understand a table's structure.

accessible-table.htmlCopy Code
<table style="border-collapse: collapse;">
  <caption>Weekly Activity Schedule</caption>
  <thead>
    <tr>
      <th style="border:1px solid #333; padding:6px;">Day</th>
      <th style="border:1px solid #333; padding:6px;">Activity</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td style="border:1px solid #333; padding:6px;">Monday</td>
      <td style="border:1px solid #333; padding:6px;">Gym</td>
    </tr>
  </tbody>
</table>

Practice Exercises: Style Your Own Bordered Table

The fastest way to make table border styling feel natural is to build a few variations yourself. Try each of these directly in the live editor above:

  1. Recreate the Basic Table Border example, then change its color from black to a hex code of your choice.
  2. Add border-collapse: collapse; to a table with individual cell borders and compare it side by side with one that omits it.
  3. Build a small internal stylesheet with a .data-table class and apply it to two separate tables.
  4. Add a <caption> above one of your tables describing what it shows.
  5. Combine a rounded border (border-radius) with a custom border-color on the same table.

Working through these five exercises touches every border technique covered in this tutorial at least once, from the simplest solid line to a fully styled, accessible, class-based table.

Gallery of CSS Border Style Values

The Style part of the border shorthand covered above accepts several different values beyond just solid and dotted, each producing a visually distinct line.

Style Value Appearance
solidA single, continuous line
dottedA line made of small round dots
dashedA line made of short dashes
doubleTwo parallel solid lines
grooveA carved, 3D-looking indent effect
ridgeThe opposite of groove, a raised 3D effect
noneNo border line at all, even if a width and color are set

The Style Table Borders example earlier on this page uses double, which is exactly why that table's border renders as two thin parallel lines rather than one, a distinctive look that works well for a more formal or decorative table without needing any custom colors.

The Deprecated HTML border Attribute

Long before CSS became the standard way to style tables, HTML itself had a built-in border attribute that could be added directly to the <table> tag without any style attribute at all.

Legacy Syntax (not recommended)Copy Code
<table border="1">
  <tr>
    <th>Name</th>
    <th>Age</th>
  </tr>
</table>

This older border="1" attribute still works in every modern browser today, but it only accepts a plain number for thickness, with no support for color, style, or rounded corners, none of the flexibility shown throughout this tutorial. Modern HTML and CSS best practices recommend using the style attribute or a stylesheet instead, exactly as every example on this page already demonstrates, reserving the legacy attribute for quick, throwaway tests rather than real production code.

Border Radius and Rounded Table Corners

The Round Table Borders example earlier on this page uses border-radius: 10px; to soften the table's corners, a property borrowed from general CSS box styling rather than being table-specific at all.

This is why the Round Table Borders example on this page intentionally does not combine border-radius with border-collapse: collapse;, keeping the two separate-border and rounded-corner effects working together cleanly rather than fighting each other.

Frequently Asked Questions About HTML Table Borders

How do I add a border to an HTML table?

The simplest way is to add a style attribute to the table tag, such as style="border: 1px solid black;", which applies a solid black border around the table.

Why do my table cells still look like they have double borders?

By default, each table cell draws its own separate border, causing a doubled-up appearance where cells meet. Adding border-collapse: collapse; to the table merges these into single, shared border lines.

Can I give a table a different border color than its default?

Yes. Adding a border-color property, such as style="border: 2px solid; border-color: orange;", sets a custom color for the table's border independently of its width or style.

Should I style table borders with inline CSS or a stylesheet?

Inline styles work well for quick practice examples, but for a real website, styling tables through an internal or external stylesheet using a class selector is recommended, since it keeps styling consistent and easy to update across every table on a site.

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

The border property controls the thickness, style, and color of border lines. The border-collapse property controls whether adjacent cell borders merge into one shared line or remain separate, doubled lines.

Do table borders affect accessibility or SEO?

Border styling itself has no direct effect on accessibility or SEO, but using proper structural tags like caption, thead, and tbody alongside your borders helps both screen readers and search engines understand the table's structure.

Is the old HTML border attribute still usable?

Yes, the border attribute (such as border="1") still works in every modern browser, but it only accepts a plain number for thickness with no color or style options, so CSS is the recommended modern approach.

How do I make a bordered table scroll on mobile instead of breaking the layout?

Wrap the table in a container div with overflow-x: auto; so a wide, bordered table can scroll horizontally within that container on small screens rather than forcing the whole page to widen.