WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

CSS Tables


The look of a HTML table can be incredibly enhanced with CSS:

Name Company Country
K's Merchandise Jennifer G. Valdez UK
Kelly P. Luse Broadcast captioner Rassian
Judy B. Jones Francisco Chang Hungry
Amanda H. Edwards Roland Mendel France
Island Trading Helen Bennett UK
Cindy L. Kerns Philip Cramer UK
Sara C. Grider Yoshi Tannamuri Canada
Magazzini Alimentari Riuniti Giovanni Rovelli Italy

Table Borders

Specify a CSS table border, using the border property.

The following example specifies the black borders of the Th and TD elements of a table:

Example

table, th, td { border: 1px solid black; }
Try it yourself »

Please note that the table in the example above has double borders. This is because the table and th / td elements have separate boundaries.

To display a single border of a table, use the border-collapse property.


Collapse Table Borders

The border-collapse property sets whether the border of the table is collapsed into a single border or separated:

Example

table { border-collapse:collapse; } table,th, td { border: 1px solid black; }
Try it yourself »

If you just need a fringe around the table, just indicate the border property for <table>:

Example

table {
    border: 1px solid black;
}
Try it yourself »

Table Width and Height

Width and height attributes define the width and height of the table.

The following example is a table with a width of 100% and a height of 50 pixels:

Example

table { width:100%; } th { height:50px; }
Try it yourself »

Horizontal Alignment

Text alignment and vertical alignment properties in the table.

The text-align property sets the horizontal alignment, left, right, or center:

Example

td { text-align:right; }
Try it yourself »

Vertical Alignment

The vertical alignment property sets vertical alignment, such as top, bottom or middle:

Example

td { height:50px; vertical-align:bottom; }
Try it yourself »

Table Padding

If you control the border between spaces in the contents of the table, you should use the padding attributes of the td and th elements:

Example

td { padding:15px; }
Try it yourself »

Horizontal Dividers

First Name Last Name Savings
Kathleen McNally $100
Hope McNally $150
Joanne Tate $300

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

Example

th, td {
    border-bottom: 1px solid #ddd;
}
Try it Yourself »

Hoverable Table

Use the :hover selector on <tr> to feature table columns on mouse over:

First Name Last Name Savings
Kathleen McNally $100
Hope McNally $150
Joanne Tate $300

Example

tr:hover {background-color: #f5f5f5}

Striped Tables

First Name Last Name Savings
Kathleen McNally $100
Hope McNally $150
Joanne Tate $300

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

Example

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

Table Color

The precedent beneath indicates the background-color what's more, content shade of <th> elements:

First Name Last Name Savings
Kathleen McNally $100
Hope McNally $150
Joanne Tate $300

Example

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

Responsive Table

A responsive table will show a level parchment bar if the screen is as well little to show the full content:

First Name Last Name Points Points Points Points Points Points Points Points Points Points Points Points
Joyce Allen 50 50 50 50 50 50 50 50 50 50 50 50
Eve Addam 94 94 94 94 94 94 94 94 94 94 94 94
Lisa Johnson 67 67 67 67 67 67 67 67 67 67 67 67

Add a compartment component (like <div>) with overflow-x:auto around the <table> component to make it responsive:

Example

<div style="overflow-x:auto;">

<table>
... table substance ...
</table>

</div>