Skip to content

Styled Data Table

Beginner ~15 min

What you'll build

An HTML page with a data table showing 5 rows of student data, with styled headers, zebra-striped rows, and hover effects.

Goals

  • Use table, thead, tbody, tr, th, and td elements correctly
  • Apply zebra striping with the nth-child pseudo-class
  • Add hover highlighting for rows
  • Style the table for readability with borders and padding

Prerequisites

  • HTML document structure
  • Basic CSS selectors and properties

Steps

  1. 1

    Create the table HTML

    Create table.html with a table element using thead and tbody. Add 5 rows of student data with Name, Age, and Grade columns.

    Code
    <!DOCTYPE html>
    <html lang="en">
      <head>
        <meta charset="UTF-8">
        <title>Student Table</title>
        <link rel="stylesheet" href="table.css">
      </head>
      <body>
        <table>
          <thead>
            <tr>
              <th>Name</th>
              <th>Age</th>
              <th>Grade</th>
            </tr>
          </thead>
          <tbody>
            <tr><td>Ada Lovelace</td><td>20</td><td>A</td></tr>
            <tr><td>Grace Hopper</td><td>21</td><td>A+</td></tr>
            <tr><td>Alan Turing</td><td>22</td><td>B+</td></tr>
            <tr><td>Linus Torvalds</td><td>19</td><td>A</td></tr>
            <tr><td>Margaret Hamilton</td><td>23</td><td>A+</td></tr>
          </tbody>
        </table>
      </body>
    </html>

    Expected result

    A basic table with headers and 5 rows of student data.

  2. 2

    Style the table structure

    Create table.css. Add border-collapse, padding, and borders to make the table clean.

    Code
    table {
      border-collapse: collapse;
      width: 100%;
      max-width: 40rem;
      margin: 2rem auto;
      font-family: system-ui, sans-serif;
    }
    th, td {
      padding: 0.75rem 1rem;
      text-align: left;
      border-bottom: 1px solid #e2e8f0;
    }

    Expected result

    A clean table with consistent padding and bottom borders.

  3. 3

    Style the header and add zebra striping

    Style the header row with a background color. Use nth-child(even) for zebra striping on body rows.

    Code
    th {
      background: #1e293b;
      color: #fff;
      font-weight: 600;
    }
    tbody tr:nth-child(even) {
      background: #f1f5f9;
    }

    Expected result

    Dark header row and alternating row backgrounds for readability.

  4. 4

    Add hover highlighting

    Add a hover effect on tbody rows to highlight the row the cursor is over.

    Code
    tbody tr:hover {
      background: #e0e7ff;
      cursor: pointer;
    }

    Expected result

    Rows highlight in light indigo on hover.

  5. 5

    Add responsive styles

    Use media queries to adjust the layout for mobile: switch multi-column layouts to single column on small screens, reduce font sizes, and adjust spacing. Mobile-first means small-screen experience matters equally.

    Code
    @media (max-width: 640px) {
      .container {
        flex-direction: column;
        padding: 1rem;
      }
      h1 { font-size: 1.5rem; }
      .card { width: 100%; margin-bottom: 1rem; }
    }

    Expected result

    Below 640px viewport, the layout becomes single-column with smaller font and spacing; content does not overflow.

Try it yourself

What you learned

You built a readable data table with semantic HTML, zebra striping with nth-child, and hover effects — the standard pattern for displaying tabular data on the web.

Related Resources

Related Lessons

Related Practice

Related Cheatsheets

Related Glossary

  • HTML

    HyperText Markup Language.

Related Glossary

A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.

  • HTML

    HyperText Markup Language.

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors

← Back to topic