CSS

CSS Tables

1. Table Borders

To specify table borders in CSS, use the border property.
Example :
table, th, td {
   border: 1px solid black;
}

2. Collapse Table Borders

The border-collapse property sets whether the table borders should be collapsed into a single border:
table {
    border-collapse: collapse;
}

table, th, td {
    border: 1px solid black;
}
If you only want a border around the table, only specify the border property for <table>:
table {
    border: 1px solid black;
}

3. Table Width and Height

Width and height of a table are defined by the width and height properties.
table {
    width: 100%;
}

th {
    height: 50px;
}

4. Horizontal Alignment

The text-align property sets the horizontal alignment (like left, right, or center) of the content in <th> or <td>.

th {
    text-align: left;
}

5. Vertical Alignment

The vertical-align property sets the vertical alignment (like top, bottom, or middle) of the content in <th> or <td>.

td {
    height: 50px;
    vertical-align: bottom;
}

6. Table Padding

To control the space between the border and the content in a table, use the padding property on <td> and <th> elements:

th, td {
    padding: 15px;
    text-align: left;
}

7. Horizontal Dividers

Add the border-bottom property to <th> and <td> for horizontal dividers:

th, td {
    border-bottom: 1px solid #ddd;
}

8. Hoverable Table

Use the :hover selector on <tr> to highlight table rows on mouse over:

tr:hover {background-color: #f5f5f5}

8. Striped Tables

For zebra-striped tables, use the nth-child() selector and add a background-color to all even (or odd) table rows:

tr:nth-child(even) {background-color: #f2f2f2}

9. Table Color

The example below specifies the background color and text color of <th> elements:

th {
    background-color: #4CAF50;
    color: white;
}

10. Responsive Table

A responsive table will display a horizontal scroll bar if the screen is too small to display the full content:
Add a container element (like <div>) with overflow-x:auto around the <table> element to make it responsive:
<div style="overflow-x:auto;">

<table>
... table content ...
</table>

</div>
Fancy Table
<!DOCTYPE html>
<html>
<head>
<style>
#customers {
    font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
    border-collapse: collapse;
    width: 100%;
}

#customers td, #customers th {
    border: 1px solid #ddd;
    text-align: left;
    padding: 8px;
}

#customers tr:nth-child(even){background-color: #f2f2f2}

#customers tr:hover {background-color: #ddd;}

#customers th {
    padding-top: 12px;
    padding-bottom: 12px;
    background-color: #4CAF50;
    color: white;
}
caption {
    caption-side: bottom;
}
</style>
</head>
<body>

<table id="customers">
<caption>Table 1.1 Customers</caption>
  <tr>
    <th>Company</th>
    <th>Contact</th>
    <th>Country</th>
  </tr>
  <tr>
    <td>Alfreds Futterkiste</td>
    <td>Maria Anders</td>
    <td>Germany</td>
  </tr>
  <tr>
    <td>Berglunds snabbköp</td>
    <td>Christina Berglund</td>
    <td>Sweden</td>
  </tr>
  <tr>
    <td>Centro comercial Moctezuma</td>
    <td>Francisco Chang</td>
    <td>Mexico</td>
  </tr>
  <tr>
    <td>Ernst Handel</td>
    <td>Roland Mendel</td>
    <td>Austria</td>
  </tr>
  <tr>
    <td>Island Trading</td>
    <td>Helen Bennett</td>
    <td>UK</td>
  </tr>
  <tr>
    <td>Königlich Essen</td>
    <td>Philip Cramer</td>
    <td>Germany</td>
  </tr>
  <tr>
    <td>Laughing Bacchus Winecellars</td>
    <td>Yoshi Tannamuri</td>
    <td>Canada</td>
  </tr>
  <tr>
    <td>Magazzini Alimentari Riuniti</td>
    <td>Giovanni Rovelli</td>
    <td>Italy</td>
  </tr>
  <tr>
    <td>North/South</td>
    <td>Simon Crowther</td>
    <td>UK</td>
  </tr>
  <tr>
    <td>Paris spécialités</td>
    <td>Marie Bertrand</td>
    <td>France</td>
  </tr>
</table>

</body>
</html>