CSS Tables
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;
}
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;
}
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;
}
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. |
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 |
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;
}
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. |
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. |
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. |
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;
}
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.
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;
}
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 |
14. CSS Tables Interview Questions
- What is the purpose of CSS tables?
- Which property is used to merge table borders?
- What is the difference between
collapseandseparate? - How do you add spacing inside table cells?
- Which property changes the alignment of table text?
- How do you create zebra-striped tables?
- How do you add a hover effect to table rows?
- How can you make a table responsive?
- Why is
border-collapsecommonly used? - What are the best practices for designing CSS tables?
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.
Quick Quiz
Which CSS property is used to merge adjacent table borders into a single border?