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.
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.
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.
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>
Name | Age |
---|---|
Alice | 25 |
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>
Name | Age |
---|---|
Alice | 25 |
The border-collapse property determines whether adjacent cell borders merge or remain separate:
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>
Item | Price |
---|---|
Pen | $2 |
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>
Fruit | Color |
---|---|
Apple | Red |
Here are the main CSS properties for styling table borders:
border: 1px solid black
).collapse
) or remain separate (separate
).border-collapse: separate
(e.g., border-spacing: 5px
).solid
, dashed
, dotted
).To make your table borders effective and user-friendly:
border-collapse: collapse
for cleaner, compact tables.Beginners often make these errors:
separate
mode.border-collapse: collapse
with padding
in cells to create clean, professional-looking tables with clear separation.
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>
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!