Tutorials /HTML /Table Borders

Table Borders

💡 Key Points on HTML Table Borders
  • Table borders define the outline of a table and its cells using CSS or the deprecated border attribute.
  • The CSS border property styles table borders with control over thickness, style, and color.
  • border-collapse determines how adjacent cell borders are handled (collapsed or separated).
  • Borders enhance table readability by visually separating data.
  • Use CSS for modern border styling instead of HTML attributes.


HTML Table Borders: A Beginner's Guide

Table borders in HTML are used to outline tables and their cells, making data easier to read and visually distinct. While the <table> tag supports a deprecated border attribute, modern web development uses CSS to style borders for greater flexibility. This tutorial explains how to add and customize table borders with examples and previews, helping beginners create clear and attractive tables.

What Are Table Borders?

Borders are lines around a table or its cells that separate content, improving readability. They can be applied to the entire table, individual cells, or specific rows/columns using CSS properties like border, border-style, border-width, and border-color. The border-collapse property controls how cell borders interact.

Why Use Table Borders? Borders make tables more readable by clearly defining cell boundaries, especially for complex data sets.



Adding Borders to Tables

Let’s explore how to add borders to tables using both the HTML border attribute (for understanding legacy code) and CSS (the modern approach), with previews.

1. Using the HTML Border Attribute (Deprecated)

The border attribute adds a simple border to the table and cells, but it’s outdated and offers limited control.

<table border="1">
  <tr>
    <th>Name</th>
    <th>Age</th>
  </tr>
  <tr>
    <td>Alice</td>
    <td>25</td>
  </tr>
</table>

Preview

Name Age
Alice 25


2. Using CSS for Borders

CSS provides more control over border style, width, and color. Apply borders to the <table>, <th>, and <td> tags.

<table style="border: 2px solid black; border-collapse: collapse;">
  <tr>
    <th style="border: 2px solid black; padding: 8px;">Name</th>
    <th style="border: 2px solid black; padding: 8px;">Age</th>
  </tr>
  <tr>
    <td style="border: 2px solid black; padding: 8px;">Alice</td>
    <td style="border: 2px solid black; padding: 8px;">25</td>
  </tr>
</table>

Preview

Name Age
Alice 25


3. Controlling Border Collapse

The border-collapse property determines whether adjacent cell borders merge or remain separate:

  • collapse: Merges borders into a single line.
  • separate: Keeps borders distinct with spacing (default).

Example with separate borders:

<table style="border: 1px solid blue; border-collapse: separate; border-spacing: 5px;">
  <tr>
    <th style="border: 1px solid blue; padding: 8px;">Item</th>
    <th style="border: 1px solid blue; padding: 8px;">Price</th>
  </tr>
  <tr>
    <td style="border: 1px solid blue; padding: 8px;">Pen</td>
    <td style="border: 1px solid blue; padding: 8px;">$2</td>
  </tr>
</table>

Preview

Item Price
Pen $2


4. Custom Border Styles

Use border-style to change the border appearance (e.g., solid, dashed, dotted).

<table style="border: 3px dashed green; border-collapse: collapse;">
  <tr>
    <th style="border: 3px dashed green; padding: 8px;">Fruit</th>
    <th style="border: 3px dashed green; padding: 8px;">Color</th>
  </tr>
  <tr>
    <td style="border: 3px dashed green; padding: 8px;">Apple</td>
    <td style="border: 3px dashed green; padding: 8px;">Red</td>
  </tr>
</table>

Preview

Fruit Color
Apple Red


Key CSS Properties for Table Borders

Here are the main CSS properties for styling table borders:

  • border: Sets width, style, and color (e.g., border: 1px solid black).
  • border-collapse: Controls whether borders merge (collapse) or remain separate (separate).
  • border-spacing: Sets the space between cells when border-collapse: separate (e.g., border-spacing: 5px).
  • border-style: Defines the border type (e.g., solid, dashed, dotted).
Warning: Avoid using the HTML border attribute in modern web development. Use CSS for better control and responsiveness.



Best Practices for Table Borders

To make your table borders effective and user-friendly:

  • Use CSS instead of the border attribute for modern, flexible styling.
  • Choose border-collapse: collapse for cleaner, compact tables.
  • Use subtle border colors and styles to avoid overwhelming the design.
  • Ensure borders are consistent across all cells for a uniform look.
  • Test tables on different devices to ensure borders display correctly.

Common Mistakes to Avoid

Beginners often make these errors:

  • Using the deprecated border attribute instead of CSS.
  • Forgetting border-collapse, leading to double borders in separate mode.
  • Applying inconsistent border styles, making tables look uneven.
Pro Tip: Combine border-collapse: collapse with padding in cells to create clean, professional-looking tables with clear separation.



Try It Yourself

Create a simple HTML file and experiment with table borders using CSS. Try different styles and border-collapse settings.

<!DOCTYPE html>
<html lang="en">
<body>
  <h3>Student Grades</h3>
  <table style="border: 1px solid navy; border-collapse: collapse;">
    <tr>
      <th style="border: 1px solid navy; padding: 8px;">Student</th>
      <th style="border: 1px solid navy; padding: 8px;">Grade</th>
    </tr>
    <tr>
      <td style="border: 1px solid navy; padding: 8px;">Bob</td>
      <td style="border: 1px solid navy; padding: 8px;">A</td>
    </tr>
  </table>
</body>
</html>

Preview

Student Grades

Student Grade
Bob A

By mastering CSS border properties for the <table> tag, you can create visually appealing and readable tables. Practice regularly to get comfortable!

The Coding Journey provides high-quality, beginner-friendly, and advanced web development tutorials. Learn React, Next.js, JavaScript, Tailwind CSS, and more with hands-on projects. Build faster, code smarter, and level up your skills! 🚀

© 2025 All Rights Reserved | The coding journey