CSS Tables

CSS Tables

Table Width and Height

table {
  width: 100%;
}

th {
  height: 50px;
}

Width and height of a table are defined by the width and height properties.

Horizontal Alignment

//the following example left-aligns the text in <th> elements

th {
  text-align: left;
}

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

By default, the content of &lt;th&gt; elements are center-aligned and the content of &lt;td&gt; elements are left-aligned.

Vertical Alignment

<pre><code>//the following example sets the vertical text alignment to bottom for &lt;td&gt; elements

td {
  height: 50px;
  vertical-align: bottom;
}</code></pre>

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

By default, the vertical alignment of the content in a table is middle (for both <th> and <td> elements).

Table Padding

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

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

Horizontal Dividers

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

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

Hoverable Table

tr:hover {background-color: #f5f5f5;}

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

Striped Tables

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

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

Table Color

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

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

Responsive Table

<pre><code>&lt;div style=&quot;overflow-x:auto;&quot;&gt;

&lt;table&gt;
... table content ...
&lt;/table&gt;

&lt;/div&gt;</code></pre>

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.

CSS Tables — Structure map

Clickable & Draggable!

CSS Tables — Related pages: