CSS Tables

CSS Tables allow you to style HTML tables by adding borders, spacing, colors, alignment, padding, hover effects, and responsive layouts. Well-designed tables make data easy to read and visually appealing.

1. Introduction to CSS Tables

HTML tables display data in rows and columns. CSS is used to improve the appearance of tables by adding borders, colors, spacing, alignment, and other formatting.

Common CSS Table Properties
  • border
  • border-collapse
  • width
  • height
  • padding
  • text-align
  • background-color

table{

    border:1px solid black;

}
CSS tables help organize and present data in a clean and professional format.

2. Table Borders

The border property adds borders around the table, rows, and cells.


table, th, td{

    border:2px solid black;

}
Border Style Description
solid Single solid border.
dashed Dashed border.
dotted Dotted border.
double Double-line border.
none No border.

th{

    border:2px dashed blue;

}
Borders make tables easier to read by separating rows and columns.

3. Border Collapse

By default, table borders are separated. The border-collapse property merges adjacent borders into a single border.


table{

    border-collapse:collapse;

}
Value Description
collapse Merges adjacent borders into one.
separate Keeps borders separate (default).

table{

    border-collapse:separate;

}
border-collapse: collapse; is commonly used in professional tables.

4. Table Width & Height

The width and height properties control the size of tables and cells.


table{

    width:100%;

}

th{

    height:60px;

}
Property Purpose
width Sets table width.
height Sets row or cell height.
max-width Limits maximum width.
Responsive tables often use width:100%;.

5. Table Alignment

CSS allows you to align text inside table cells using the text-align and vertical-align properties.


th{

    text-align:center;

}

td{

    vertical-align:middle;

}
Property Values
text-align left, center, right
vertical-align top, middle, bottom
Proper alignment improves the readability and presentation of tabular data.

6. Table Padding

The padding property adds space between the content of a table cell and its border, making tables easier to read.


th, td{

    padding:15px;

}
Padding Value Result
5px Small spacing
10px Medium spacing
15px Comfortable spacing
20px Large spacing

th{

    padding:20px;

}
Adequate padding improves readability and gives tables a professional appearance.

7. Table Hover Effect

The :hover pseudo-class highlights rows when the mouse pointer moves over them.


tr:hover{

    background-color:#f2f2f2;

}

tbody tr:hover{

    background:#d1ecf1;

    cursor:pointer;

}
Property Purpose
background-color Highlights the row.
cursor:pointer Changes the mouse cursor.
Hover effects make tables interactive and easier to navigate.

8. Zebra Striped Tables

Zebra-striped tables use alternating row colors to improve readability, especially for large datasets.


tr:nth-child(even){

    background-color:#f2f2f2;

}

tr:nth-child(odd){

    background-color:white;

}
Selector Description
nth-child(even) Styles even rows.
nth-child(odd) Styles odd rows.
Zebra stripes make long tables much easier to read.

9. Responsive Tables

Responsive tables allow users to scroll horizontally on smaller screens instead of breaking the layout.


.table-container{

    overflow-x:auto;

}

...
Technique Benefit
overflow-x:auto Horizontal scrolling on small devices.
width:100% Uses available screen width.
Responsive tables improve usability on mobile phones and tablets.

10. Practical Table Examples

Example 1: Simple Table Border

table, th, td{

    border:1px solid black;

}

Example 2: Border Collapse

table{

    border-collapse:collapse;

}

Example 3: Hover Effect

tr:hover{

    background-color:lightblue;

}

Example 4: Zebra Striped Table

tr:nth-child(even){

    background:#f2f2f2;

}
Combining borders, padding, hover effects, and zebra stripes creates clean, modern, and professional tables.

11. CSS Tables Best Practices

Well-designed tables improve readability and make data easier to understand. Follow these best practices when styling tables.

  • Use border-collapse: collapse; for clean borders.
  • Add sufficient padding inside table cells.
  • Align text consistently.
  • Use zebra stripes for large tables.
  • Add hover effects for better user interaction.
  • Keep column widths consistent.
  • Use responsive tables for mobile devices.
  • Avoid excessive colors and borders.
Clean, responsive, and well-spaced tables provide a better user experience.

12. Common Table Mistakes

Mistake Correct Practice
No cell padding. Add padding for readability.
Using separate borders. Use border-collapse: collapse;.
Very bright colors. Use soft, readable colors.
No hover effect. Add row highlighting.
Fixed-width tables on mobile. Use responsive layouts.

❌ Incorrect

table{

    width:600px;

}

✔ Better

table{

    width:100%;

    border-collapse:collapse;

}
Poorly designed tables are difficult to read and create a bad user experience.

13. Browser Support

CSS table properties are fully supported by all modern web browsers.

Browser Support
Google Chrome ✔ Supported
Mozilla Firefox ✔ Supported
Microsoft Edge ✔ Supported
Safari ✔ Supported
Opera ✔ Supported
Modern browsers fully support CSS table styling properties.

14. CSS Tables Interview Questions

  1. What is the purpose of CSS tables?
  2. Which property is used to merge table borders?
  3. What is the difference between collapse and separate?
  4. How do you add spacing inside table cells?
  5. Which property changes the alignment of table text?
  6. How do you create zebra-striped tables?
  7. How do you add a hover effect to table rows?
  8. How can you make a table responsive?
  9. Why is border-collapse commonly used?
  10. What are the best practices for designing CSS tables?
These questions are commonly asked in HTML & CSS interviews and practical examinations.

15. Lesson Summary

In this lesson, you learned:
  • ✔ Introduction to CSS Tables.
  • ✔ Table Borders.
  • ✔ Border Collapse.
  • ✔ Table Width & Height.
  • ✔ Table Alignment.
  • ✔ Table Padding.
  • ✔ Table Hover Effect.
  • ✔ Zebra Striped Tables.
  • ✔ Responsive Tables.
  • ✔ Practical Examples.
  • ✔ Best Practices and Common Mistakes.
Congratulations! You have successfully completed the CSS Tables lesson. You can now create clean, responsive, and professional tables using CSS.

Quick Quiz

Which CSS property is used to merge adjacent table borders into a single border?