CSS Tables

In CSS, you can style HTML tables and their elements (such as <table>, <tr>, <th>, <td>, etc.) using various properties to control their appearance, spacing, borders, backgrounds, and more. Styling tables effectively is crucial for data readability, as unstyled tables often appear cramped and difficult to scan.

Watch Out: Use tables only for tabular data (like price lists, schedules, or spreadsheets). Never use tables for your website's layout; use CSS Flexbox or Grid for that instead.
  • Table Borders (border-collapse and border-spacing): By default, HTML tables have a gap between the borders of adjacent cells. You can control this behavior using border-collapse. Most modern designs prefer "collapsing" these borders into a single line.
table {
    border-collapse: collapse; /* Merges adjacent cell borders into one */
    width: 100%;
    border-spacing: 0; /* Ensures no extra space if collapse is not used */
}
Common Mistake: Forgetting to use border-collapse: collapse;. Without it, you will see "double borders" around every cell, which usually looks outdated and cluttered.
  • Table Cell Borders (border): You apply borders to the cells (<td> and <th>) rather than just the table itself to create a grid look.
td, th {
    border: 1px solid #ddd; /* A light gray border for a clean, modern look */
}
  • Table Width and Height (width and height): Setting the width ensures your table fits its container. Using 100% is common for responsive designs.
table {
    width: 100%; /* The table will span the full width of its parent element */
    height: auto; /* It's usually best to let the height adjust based on content */
}
Developer Tip: Use table-layout: fixed; on your table if you want to force columns to respect specific widths, even if the content inside them is long.
  • Table Background (background-color): You can apply backgrounds to the whole table, specific rows, or header cells to make them stand out.
table {
    background-color: #f9f9f9; /* Subtle background for the whole table */
}

th {
    background-color: #333; /* Dark background for headers */
    color: white; /* White text for contrast */
}
  • Table Header (<thead>) and Footer (<tfoot>): Styling these sections separately helps users distinguish between metadata (titles and totals) and the actual data rows.
thead {
    border-bottom: 2px solid #2c3e50; /* A thicker line to separate the header from data */
}

tfoot {
    font-weight: bold;
    background-color: #eee;
}
  • Striped Rows (:nth-child): This technique, often called "Zebra Striping," significantly improves readability in large datasets by helping the eye track horizontally across a row.
/* Apply a light gray background to every even-numbered row */
tr:nth-child(even) {
    background-color: #f2f2f2;
}

/* Optional: Add a hover effect to highlight the row the user is looking at */
tr:hover {
    background-color: #e9ecef;
}
Best Practice: Always include a hover state (tr:hover) on data tables. It provides excellent visual feedback for users navigating through rows of information.
  • Table Cell Padding (padding): Padding is the most effective way to make a table look professional. It gives the text "room to breathe."
td, th {
    padding: 12px 15px; /* Ample vertical and horizontal space */
    text-align: left; /* Ensures all data aligns neatly to the left */
}
  • Table Caption (caption-side): The <caption> element provides a title for your table. You can use caption-side to choose where it appears.
caption {
    padding: 10px;
    font-style: italic;
    caption-side: bottom; /* Moves the title below the table */
}

These CSS properties and techniques allow you to transform raw HTML data into a visually appealing and highly functional interface. By focusing on borders, spacing, and zebra-striping, you ensure your data remains accessible and easy to digest for your users.

Responsive Tables: On small screens, tables can break layouts. Wrap your <table> in a <div> with overflow-x: auto; to allow users to swipe horizontally to see all data without breaking the page width.