agyanadda@gmail.com

CSS Tables

Styling Content 1 min read Updated Dec 1, 2026 Topic 28 of 39

Useful table styles

  • border-collapse: collapse; — merges double borders into one
  • padding on th and td — space inside cells
  • text-align — align cell content
  • tr:nth-child(even) — striped rows
  • tr:hover — highlight the row under the mouse

Responsive tables

Wrap the table in a div with overflow-x: auto; so it scrolls sideways on small screens.

Example

Example — Striped table with hover HTML
<style>
  .wrap { overflow-x: auto; }
  table { width: 100%; border-collapse: collapse; font-family: Arial, sans-serif; }
  th, td { border: 1px solid #ddd; padding: 10px; text-align: left; }
  th { background: #1f386b; color: white; }
  tr:nth-child(even) { background: #f4f7fc; }
  tr:hover { background: #fff3cd; }
</style>

<div class="wrap">
<table>
  <tr><th>Course</th><th>Topics</th><th>Level</th></tr>
  <tr><td>HTML</td><td>33</td><td>Beginner</td></tr>
  <tr><td>CSS</td><td>38</td><td>Beginner</td></tr>
  <tr><td>JavaScript</td><td>40</td><td>Intermediate</td></tr>
</table>
</div>

Test Yourself With Exercises

Exercise 1 Multiple Choice

Which property merges table cell borders into one line?

Exercise 2 Fill in the Blank

tr:nth-child(___) selects every second row.

Exercise 3 True / False

overflow-x: auto on a wrapper helps tables on mobile.

Frequently Asked Questions

No. Use tables only for tabular data. Use Flexbox or Grid for layout.