On this page10 sections
Lecture 014: Styling Tables
| Category | 3: Text, Fonts & Content |
| Lecture | 014 of 057 |
| Estimated video length | 12 min |
| Target | CSS3 · any modern browser |
| Level | Beginner |
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
<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
<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-numsmakes every digit the same width.vertical-align(top,middle,bottom) aligns content inside tall cells.
3. A Polished Table
Example
<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:
<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
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
- Which property removes double borders?
- Why right-align numbers?
- Stripe even rows light grey.
- How do you make a wide table scroll on small screens?
- Which property moves the caption below the table?
Practice Answers
border-collapse: collapse;- So digits with the same place value line up and are easy to compare.
tr:nth-child(even) td { background: #f1f3f5; }- Wrap it in a
<div>withoverflow-x: auto. caption-side: bottom;
Key Takeaways
border-collapse: collapse+ borders onth, td= clean lines.- Pad cells, left-align text, right-align numbers.
:nth-child(even)stripes and:hoverhighlights rows.- Wrap wide tables in
overflow-x: autofor phones.