Skip to content
Justice Tech School

Styling Tables

Tables have separate borders by default, which leaves gaps. border-collapse: collapse merges them into single lines. Use padding on th and td for breathing room and text-align for alignment.

Zebra stripes with tr:nth-child(even) and a hover highlight make wide tables easier to scan. For narrow screens, wrap a table in a container with overflow-x: auto.

Example
<style>
  table { border-collapse: collapse; width: 100%; }
  th, td { padding: 8px 12px; border: 1px solid #cbd5e1; text-align: left; }
  th { background: #0f766e; color: white; }
  tr:nth-child(even) td { background: #f1f5f9; }
  tr:hover td { background: #ccfbf1; }
</style>
<table>
  <tr><th>Fruit</th><th>Colour</th></tr>
  <tr><td>Apple</td><td>Red</td></tr>
  <tr><td>Banana</td><td>Yellow</td></tr>
  <tr><td>Plum</td><td>Purple</td></tr>
</table>

Exercise

Merge the double borders into single lines.

table {
  border-: collapse;
}

Quiz Test

What does border-collapse: collapse do?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.