On this page10 sections
Lecture 014: Table Borders, Sizes, Spacing & Colgroup
| Category | 3: Links, Images, Tables & Lists |
| Lecture | 014 of 048 |
| Estimated video length | 12 min |
| Target | HTML5 · any modern browser |
| Level | Beginner |
A bare table is hard to scan. A few CSS properties turn it into something clear: borders that don't double up, comfortable cell padding, sensible widths, striped rows, and whole columns styled at once with
<colgroup>.
Learning Objectives
- Add clean borders with
borderandborder-collapse - Set table, column and row sizes
- Control cell padding and spacing
- Stripe rows, highlight on hover, and style columns with
<colgroup>
1. Borders That Don't Double Up
Give table, th and td a border, then collapse the doubled lines into one.
Example
<style>
table, th, td { border: 1px solid #868e96; }
table { border-collapse: collapse; }
</style>
<table>
<tr><th>Fruit</th><th>Price</th></tr>
<tr><td>Mango</td><td>Rs 300/kg</td></tr>
<tr><td>Banana</td><td>Rs 150/dozen</td></tr>
</table>Without border-collapse: collapse, every cell has its own border, so you see double lines.
Borders can also be styled per side, or rounded:
<style>
table { border-collapse: collapse; }
th, td { border-bottom: 1px solid #dee2e6; padding: 8px; }
</style>
<table>
<tr><th>Name</th><th>City</th></tr>
<tr><td>Ayesha</td><td>Lahore</td></tr>
<tr><td>Omar</td><td>Quetta</td></tr>
</table>2. Table Sizes
Example
<style>
table { width: 100%; border-collapse: collapse; }
th, td { border: 1px solid #ced4da; }
th:first-child { width: 70%; }
tr.tall { height: 60px; }
</style>
<table>
<tr><th>Task</th><th>Due</th></tr>
<tr class="tall"><td>Write report</td><td>Friday</td></tr>
<tr><td>Email team</td><td>Today</td></tr>
</table>width: 100% makes the table fill its container; a width on a header cell sizes its column.
3. Padding and Spacing
paddingon cells = space inside each cellborder-spacingon the table = space between cells (only when borders aren't collapsed)
Example
<style>
.spaced { border-spacing: 8px; }
.spaced td { border: 1px solid #74c0fc; padding: 10px 16px; background: #e7f5ff; }
</style>
<table class="spaced">
<tr><td>A</td><td>B</td><td>C</td></tr>
<tr><td>D</td><td>E</td><td>F</td></tr>
</table>4. Header Alignment, Stripes and Hover
Example
<style>
table { border-collapse: collapse; width: 100%; font-family: Arial, sans-serif; }
th { background: #343a40; color: white; text-align: left; }
th, td { padding: 10px; }
tr:nth-child(even) { background: #f1f3f5; }
tr:hover td { background: #fff3bf; }
</style>
<table>
<tr><th>Student</th><th>Maths</th><th>English</th></tr>
<tr><td>Ali</td><td>88</td><td>74</td></tr>
<tr><td>Hira</td><td>92</td><td>81</td></tr>
<tr><td>Bilal</td><td>67</td><td>90</td></tr>
<tr><td>Zara</td><td>79</td><td>85</td></tr>
</table>th text is centred by default; text-align: left lines it up with the data.
5. Styling Whole Columns With <colgroup>
<colgroup> goes straight after <caption> (or the <table> tag). Each <col> describes
one column, or several with span.
Example
<style>
table { border-collapse: collapse; }
th, td { border: 1px solid #ced4da; padding: 8px; }
</style>
<table>
<colgroup>
<col span="2" style="background: #e6fcf5;">
<col style="background: #fff4e6;">
</colgroup>
<tr><th>Mon</th><th>Tue</th><th>Wed</th></tr>
<tr><td>Maths</td><td>Urdu</td><td>Sports</td></tr>
<tr><td>English</td><td>Science</td><td>Art</td></tr>
</table>Only a few properties work on <col>: background, width, border and visibility.
Common Beginner Mistakes
1. Double borders. Add border-collapse: collapse to the table.
2. border-spacing with collapsed borders. It does nothing once borders are collapsed.
3. Expecting color on <col> to work. Only background, width, border and visibility apply.
4. Cramped cells. Always add some padding to th and td.
Practice Questions
- Which property removes double borders between cells?
- What's the difference between
paddingon a cell andborder-spacingon a table? - How do you make a table fill the width of its container?
- Write a rule that stripes every even row light grey.
- Where does
<colgroup>go inside a table? - Give the first two columns a yellow background with one
<col>.
Practice Answers
border-collapse: collapse;- Padding is space inside each cell; border-spacing is space between cells.
table { width: 100%; }tr:nth-child(even) { background: #f1f3f5; }- Right after the
<caption>, or right after<table>if there's no caption. <colgroup><col span="2" style="background: yellow;"></colgroup>
Key Takeaways
border+border-collapse: collapsegives clean single-line borders.width,height,paddingandborder-spacingcontrol size and spacing.:nth-child(even)stripes rows;:hoverhighlights them.<colgroup>/<col>style whole columns.