On this page10 sections
  1. Learning Objectives
  2. 1. Borders That Don't Double Up
  3. 2. Table Sizes
  4. 3. Padding and Spacing
  5. 4. Header Alignment, Stripes and Hover
  6. 5. Styling Whole Columns With <colgroup>
  7. Common Beginner Mistakes
  8. Practice Questions
  9. Practice Answers
  10. Key Takeaways

Lecture 014: Table Borders, Sizes, Spacing & Colgroup

Category3: Links, Images, Tables & Lists
Lecture014 of 048
Estimated video length12 min
TargetHTML5 · any modern browser
LevelBeginner

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 border and border-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

html
<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:

html
<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

html
<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

  • padding on cells = space inside each cell
  • border-spacing on the table = space between cells (only when borders aren't collapsed)

Example

html
<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

html
<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

html
<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

  1. Which property removes double borders between cells?
  2. What's the difference between padding on a cell and border-spacing on a table?
  3. How do you make a table fill the width of its container?
  4. Write a rule that stripes every even row light grey.
  5. Where does <colgroup> go inside a table?
  6. Give the first two columns a yellow background with one <col>.

Practice Answers

  1. border-collapse: collapse;
  2. Padding is space inside each cell; border-spacing is space between cells.
  3. table { width: 100%; }
  4. tr:nth-child(even) { background: #f1f3f5; }
  5. Right after the <caption>, or right after <table> if there's no caption.
  6. <colgroup><col span="2" style="background: yellow;"></colgroup>

Key Takeaways

  • border + border-collapse: collapse gives clean single-line borders.
  • width, height, padding and border-spacing control size and spacing.
  • :nth-child(even) stripes rows; :hover highlights them.
  • <colgroup> / <col> style whole columns.