On this page10 sections
  1. Learning Objectives
  2. 1. Borders
  3. 2. Size and Alignment
  4. 3. A Polished Table
  5. 4. Responsive Tables
  6. 5. Caption Styling
  7. Common Beginner Mistakes
  8. Practice Questions
  9. Practice Answers
  10. Key Takeaways

Lecture 014: Styling Tables

Category3: Text, Fonts & Content
Lecture014 of 057
Estimated video length12 min
TargetCSS3 · any modern browser
LevelBeginner

A well-styled table is easy to scan: clear borders, room in every cell, aligned numbers, striped rows and a header that stands out. On phones, a table needs one more trick so it doesn't break the layout.


Learning Objectives

  • Add borders and collapse them
  • Set table width, cell padding and alignment
  • Stripe rows, highlight on hover, and style the header
  • Make wide tables scroll on small screens

1. Borders

Example

html
<style>
  table { border-collapse: collapse; }
  th, td { border: 1px solid #adb5bd; padding: 6px 10px; }
</style>

<table>
  <tr><th>City</th><th>Population</th></tr>
  <tr><td>Karachi</td><td>20.4 m</td></tr>
  <tr><td>Lahore</td><td>13.0 m</td></tr>
</table>

Without border-collapse: collapse, each cell has its own border and you see double lines.


2. Size and Alignment

Example

html
<style>
  table { width: 100%; border-collapse: collapse; }
  th, td { border-bottom: 1px solid #dee2e6; padding: 10px; }
  th { text-align: left; }
  td.num { text-align: right; font-variant-numeric: tabular-nums; }
  td { vertical-align: top; height: 40px; }
</style>

<table>
  <tr><th>Item</th><th style="text-align: right;">Price</th></tr>
  <tr><td>Notebook</td><td class="num">Rs 150</td></tr>
  <tr><td>Fountain pen</td><td class="num">Rs 1,250</td></tr>
  <tr><td>Geometry box</td><td class="num">Rs 480</td></tr>
</table>
  • Right-align numbers so the digits line up.
  • font-variant-numeric: tabular-nums makes every digit the same width.
  • vertical-align (top, middle, bottom) aligns content inside tall cells.

3. A Polished Table

Example

html
<style>
  .data { width: 100%; border-collapse: collapse; font-family: system-ui, sans-serif; font-size: 14px; }
  .data th { background: #1c7ed6; color: white; text-align: left; padding: 10px; }
  .data td { padding: 10px; border-bottom: 1px solid #e9ecef; }
  .data tr:nth-child(even) td { background: #f8f9fa; }
  .data tr:hover td { background: #fff9db; }
  .data th:first-child { border-top-left-radius: 8px; }
  .data th:last-child  { border-top-right-radius: 8px; }
</style>

<table class="data">
  <tr><th>Student</th><th>Class</th><th>Score</th></tr>
  <tr><td>Hamza</td><td>9-A</td><td>91</td></tr>
  <tr><td>Iqra</td><td>9-B</td><td>88</td></tr>
  <tr><td>Daniyal</td><td>9-A</td><td>76</td></tr>
  <tr><td>Mahnoor</td><td>9-C</td><td>95</td></tr>
</table>

4. Responsive Tables

A wide table overflows a phone screen. Wrap it in a box that scrolls sideways.

Example: drag the preview corner to make it narrow:

html
<style>
  .scroll { overflow-x: auto; border: 1px solid #dee2e6; border-radius: 8px; }
  .scroll table { border-collapse: collapse; min-width: 600px; }
  .scroll th, .scroll td { padding: 8px 12px; border-bottom: 1px solid #e9ecef; white-space: nowrap; }
</style>

<div class="scroll">
  <table>
    <tr><th>Day</th><th>Fajr</th><th>Dhuhr</th><th>Asr</th><th>Maghrib</th><th>Isha</th></tr>
    <tr><td>Monday</td><td>5:12</td><td>12:20</td><td>3:45</td><td>6:02</td><td>7:25</td></tr>
    <tr><td>Tuesday</td><td>5:13</td><td>12:20</td><td>3:44</td><td>6:01</td><td>7:24</td></tr>
  </table>
</div>

5. Caption Styling

css
caption { caption-side: bottom; font-size: 13px; color: #868e96; padding-top: 6px; }

Common Beginner Mistakes

1. Double borders. Add border-collapse: collapse.

2. No padding. Text touching the lines is hard to read.

3. Centred numbers. Right-align them so the place values line up.

4. Tables wider than the phone. Wrap them in an overflow-x: auto box.


Practice Questions

  1. Which property removes double borders?
  2. Why right-align numbers?
  3. Stripe even rows light grey.
  4. How do you make a wide table scroll on small screens?
  5. Which property moves the caption below the table?

Practice Answers

  1. border-collapse: collapse;
  2. So digits with the same place value line up and are easy to compare.
  3. tr:nth-child(even) td { background: #f1f3f5; }
  4. Wrap it in a <div> with overflow-x: auto.
  5. caption-side: bottom;

Key Takeaways

  • border-collapse: collapse + borders on th, td = clean lines.
  • Pad cells, left-align text, right-align numbers.
  • :nth-child(even) stripes and :hover highlights rows.
  • Wrap wide tables in overflow-x: auto for phones.