HTML Table Borders
In this page:
<style>
table, th, td {
border: width style color;
}
</style>
Adding a Basic Border
Applying border: 1px solid black to the table, th, and td elements draws a border around the outer table edge as well as around every individual cell, producing the classic grid appearance most people picture when they think of an HTML table.
Example: Adding a Basic Border
<style>
table, th, td {
border: 1px solid black;
}
</style>
<table>
<tr><th>Name</th></tr>
<tr><td>Alex</td></tr>
</table>
Merging Lines with border-collapse
Without border-collapse, adjacent cells each draw their own separate border, which visually doubles up into a thicker line where two cells touch. border-collapse: collapse merges these shared edges into one single clean line, which is the look nearly every modern table design uses.
Example: Merging Lines with border-collapse
<style>
table {
border-collapse: collapse;
}
th, td {
border: 1px solid black;
}
</style>
<table>
<tr><td>Cell</td></tr>
</table>
Coloring and Styling Borders
Just like any other CSS border, table borders accept a color and a line style beyond the default solid black -- border: 2px dashed #3498db draws a thicker, dashed blue line, letting table borders match a site's overall color scheme instead of always looking gray or black.
Example: Coloring and Styling Borders
<style>
table, th, td {
border: 2px dashed #3498db;
}
</style>
<table>
<tr><td>Cell</td></tr>
</table>
Bordering Only Specific Sides
CSS lets you set only one side of a cell's border, like border-bottom, instead of all four -- this is how the common "row divider" table style is built, where only a horizontal line separates rows and there are no vertical column lines at all.
Example: Bordering Only Specific Sides
<style>
td {
border-bottom: 1px solid #ddd;
}
</style>
<table>
<tr><td>Row 1</td></tr>
<tr><td>Row 2</td></tr>
</table>
Rounded Table Borders
Because border-collapse: collapse merges all cell borders into a single flattened grid, border-radius does not work directly on individual cells in a collapsed table.
Achieving rounded outer corners instead requires border-collapse: separate along with border-spacing: 0, or applying border-radius to a wrapping container element around the table.
Example: Rounded Table Borders
<style>
.wrapper {
overflow: hidden;
border-radius: 8px;
}
</style>
<div class="wrapper">
<table>
<tr><td>Cell</td></tr>
</table>
</div>
- Setting border only on the <table> element and expecting individual cells to get borders too, when table, th, and td each need their own border declaration.
- Ending up with a doubled, thick-looking grid line between cells because border-collapse was never set to collapse.
- Using the deprecated HTML border="1" attribute instead of a CSS border property, which offers far less styling control.
- Borders must be applied to the table, th, and td elements individually, or all three, to fully outline a table's grid.
- border-collapse: collapse merges adjacent cell borders into a single clean line instead of a doubled one.
- Border color, width, and style (solid, dashed, dotted) are all fully customizable with standard CSS border properties.
Table border styling with CSS has been supported in every browser since CSS1.
Chapter Quiz — Complete all 26 topics to unlock
0/26 topics done
Complete these topics first:
- HTML Favicon
- HTML Page Title
- HTML Tables
- HTML Lists
- HTML Block & Inline
- HTML Comments
- HTML Colors
- HTML CSS
- HTML Links
- HTML Images
- HTML RGB Colors
- HTML HEX Colors
- HTML HSL Colors
- HTML Link Colors
- HTML Link Bookmarks
- HTML Background Images
- HTML Table Borders
- HTML Table Sizes
- HTML Table Headers
- HTML Table Padding & Spacing
- HTML Colspan & Rowspan
- HTML Table Styling
- HTML Table Colgroup
- HTML Unordered Lists
- HTML Ordered Lists
- HTML Other Lists / Description Lists