# How to style tables in HTML

HTML table styling refers to the visual design of tables on websites. To style tables, you can use CSS instructions. These can be included directly in the HTML document or referenced through an external CSS file.

## How does HTML table styling work?

Modern web design utilizes the power of CSS to customize the appearance of [HTML tables](https://www.ionos.com/digitalguide/websites/web-development/html-table/). With CSS, you can easily modify **the appearance of tabular content elements**. For instance, you can assign background colors to table cells or define the thickness of borders. When styling an HTML table, you have three different methods for applying CSS code at your disposal:

- **Embedding in the `<head>` section**: You can include CSS styles using the [HTML tag](https://www.ionos.com/digitalguide/websites/web-development/html-tags/) `<style>` in the head section of the HTML document.
- **External CSS file**: Another option is to use a separate CSS file. You can link to it from the HTML document using the `<link>` tag in the `<head>` section.
- **Inline CSS**: You can also apply table styling in HTML by using the `style` attribute in the table element. However, this is only advisable in specific cases where code reusability and maintenance ease are not a concern.

Tip You can learn more about how to [embed CSS in HTML](https://www.ionos.com/digitalguide/websites/web-design/linking-css-to-html/) in our Digital Guide.

## What options are there for styling HTML tables?

There are numerous CSS properties you can use to improve the appearance and user experience of tables in web documents. For example, you can modify **basic table properties** using the following CSS instructions:

- `width`: Sets the width of the table or individual columns
- `border`: Defines the width, style and color of the border
- `border-collapse`: Controls whether cell borders are combined (`collapse`) or separate (`separate`)
- `border-spacing`: Determines the space between cells when `border-collapse: separate` is used

Below, we’ve summarized some of the most important options for styling tables in HTML and organized them by category.

### Cell spacing and text alignment

<table>
  <thead>
    <tr>
      <th>CSS property</th>
      <th>Description</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>`padding`</td>
      <td>Adds space inside the cells</td>
    </tr>
    <tr>
      <td>`margin`</td>
      <td>Adds space around the entire table</td>
    </tr>
    <tr>
      <td>`text-align`</td>
      <td>Defines the horizontal alignment of text within the cells (`left`, `center`, `right`)</td>
    </tr>
    <tr>
      <td>`vertical-align`</td>
      <td>Defines the vertical alignment of the cell content (`top`, `middle`, `bottom`)</td>
    </tr>
  </tbody>
</table>

### Colors and background image

<table>
  <thead>
    <tr>
      <th>CSS property</th>
      <th>Description</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>`background-color`</td>
      <td>Sets the background color of cells, rows or the entire table</td>
    </tr>
    <tr>
      <td>`color`</td>
      <td>Sets the text color</td>
    </tr>
    <tr>
      <td>`background-image`</td>
      <td>Specifies a background image for cells or the entire table</td>
    </tr>
  </tbody>
</table>

### Fonts and font size

<table>
  <thead>
    <tr>
      <th>CSS property</th>
      <th>Description</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>`font-family`</td>
      <td>Defines the font</td>
    </tr>
    <tr>
      <td>`font-size`</td>
      <td>Sets the font size</td>
    </tr>
    <tr>
      <td>`font-weight`</td>
      <td>Sets the font weight (e.g., `bold` for bold text)</td>
    </tr>
    <tr>
      <td>`text-transform`</td>
      <td>Controls the capitalization of text (`uppercase`, `lowercase`, `capitalize`)</td>
    </tr>
  </tbody>
</table>

### Table borders

<table>
  <thead>
    <tr>
      <th>CSS property</th>
      <th>Description</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>`border-style`</td>
      <td>Defines the style of the border (`solid`, `dashed`, `dotted`)</td>
    </tr>
    <tr>
      <td>`border-width`</td>
      <td>Sets the thickness of the border</td>
    </tr>
    <tr>
      <td>`border-color`</td>
      <td>Specifies the color of the table border</td>
    </tr>
  </tbody>
</table>

## How to style HTML tables (with examples)

Lastly, we’ll illustrate the **syntax and functionality** of the three HTML table styling methods mentioned above with practical examples. We’ll also briefly discuss the **advantages and disadvantages** of each method.

### Styling HTML tables in the `<head>` section

Styling tables with CSS in the head section is especially practical for **small projects** and **quick changes**. You don’t need an additional style sheet, and you can use IDs to specify which tables the styles should be applied to. However, these styles only apply to the corresponding web page, which makes reusability and maintenance tedious.

You can embed the CSS code in the head section using a `<style>` element. In the following example, we’re going to set the table header to have a green background, while the other rows will alternate between light gray and white:

```html
<html>
<head>
    <style>
        table {
        thead th {
            background-color: #4CAF50; / *Green for table header* /
        }
        tbody tr:nth-child(odd) {
            background-color: #f2f2f2; / *Light gray for odd rows* /
        }
        tbody tr:nth-child(even) {
            background-color: #ffffff; / *White for even rows* /
        }
    </style>
</head>
<body>
    <table>
        <thead>
            <tr>
                <th>Column 1</th>
                <th>Column 2</th>
                <th>Column 3</th>
                <th>Column 4</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>Entry 1</td>
                <td>Entry 2</td>
                <td>Entry 3</td>
                <td>Entry 4</td>
            </tr>
            <tr>
                <td>Entry 5</td>
                <td>Entry 6</td>
                <td>Entry 7</td>
                <td>Entry 8</td>
            </tr>
            <tr>
                <td>Entry 9</td>
                <td>Entry 10</td>
                <td>Entry 11</td>
                <td>Entry 12</td>
            </tr>
            <tr>
                <td>Entry 13</td>
                <td>Entry 14</td>
                <td>Entry 15</td>
                <td>Entry 16</td>
            </tr>
        </tbody>
    </table>
<div class="hash d-none" hidden>hash_4c69cf8e1d59146e45ad34887b1535e0991cc33f</div><script data-shy-copy-guard>(function(){var C=[0x00AD,0x200B,0x200C,0x200D,0x2060,0xFEFF];var s=function(t){for(var i=0;i<C.length;i++){t=t.split(String.fromCharCode(C[i])).join("");}return t;};document.addEventListener("copy",function(e){var sel=document.getSelection();if(!sel||sel.isCollapsed||!e.clipboardData){return;}e.clipboardData.setData("text/plain",s(sel.toString()));try{var d=document.createElement("div");d.appendChild(sel.getRangeAt(0).cloneContents());e.clipboardData.setData("text/html",s(d.innerHTML));}catch(x){}e.preventDefault();});})();</script></body>
</html>
```

[![Image: Formatting HTML tables: Example](https://www.ionos.com/digitalguide/fileadmin/_processed_/2/6/csm_html-table-format-example_48edb3fe3f.webp "Formatting HTML tables: Example")](https://www.ionos.com/digitalguide/fileadmin/DigitalGuide/Screenshots_2024/html-table-format-example.png) The table’s simple design is made up of a green header row and alternating row colors for the rows that follow. ### Styling HTML tables with CSS

Styling HTML tables through an external CSS sheet is the best way to separate content from design. Additionally, the styles are easy to reuse for other pages. Since browsers can cache the CSS file, it also helps **optimize loading times**. This method may, however, be too elaborate for smaller web projects.

The previous example can be implemented in two steps: First, create a CSS file named `styles.css` and save it in the same directory as the webpage. Add the following code:

```css
thead th {
    background-color: #4CAF50; / *Green for table header* /
}
tbody tr:nth-child(odd) {
    background-color: #f2f2f2; / *Light gray for odd rows* /
}
tbody tr:nth-child(even) {
    background-color: #ffffff; / *White for even rows* /
}
```

Next, link the file in the header using a `<link>` element. The table code remains unchanged:

```html
<html>
<head>
        <link rel="stylesheet" type="text/css" href="styles.css">
</head>
<body>
    <table>
        <thead>
            <tr>
                <th>Column 1</th>
                <th>Column 2</th>
                <th>Column 3</th>
                <th>Column 4</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>Entry 1</td>
                <td>Entry 2</td>
                <td>Entry 3</td>
                <td>Entry 4</td>
            </tr>
        </tbody>
    </table>
<div class="hash d-none" hidden>hash_4c69cf8e1d59146e45ad34887b1535e0991cc33f</div><script data-shy-copy-guard>(function(){var C=[0x00AD,0x200B,0x200C,0x200D,0x2060,0xFEFF];var s=function(t){for(var i=0;i<C.length;i++){t=t.split(String.fromCharCode(C[i])).join("");}return t;};document.addEventListener("copy",function(e){var sel=document.getSelection();if(!sel||sel.isCollapsed||!e.clipboardData){return;}e.clipboardData.setData("text/plain",s(sel.toString()));try{var d=document.createElement("div");d.appendChild(sel.getRangeAt(0).cloneContents());e.clipboardData.setData("text/html",s(d.innerHTML));}catch(x){}e.preventDefault();});})();</script></body>
</html>
```

### Styling HTML tables inline

Inline CSS is useful when you want to make **specific changes** to an individual table element. It’s ideal for small, one-time projects or tests where reusability isn’t important. However, for more sustainable coding scenarios, it’s best to use one of the other embedding methods.

To apply inline styling, simply include the styles directly in the relevant table elements. For our example from above, the code would look like this:

```html
<body>
    <table>
        <thead>
            <tr>
                <th style="background-color: #4CAF50;">Column 1</th>
                <th style="background-color: #4CAF50;">Column 2</th>
                <th style="background-color: #4CAF50;">Column 3</th>
                <th style="background-color: #4CAF50;">Column 4</th>
            </tr>
        </thead>
        <tbody>
            <tr style="background-color: #f2f2f2;">
                <td>Entry 1</td>
                <td>Entry 2</td>
                <td>Entry 3</td>
                <td>Entry 4</td>
            </tr>
            <tr>
                <td>Entry 5</td>
                <td>Entry 6</td>
                <td>Entry 7</td>
                <td>Entry 8</td>
            </tr>
            <tr style="background-color: #f2f2f2;">
                <td>Entry 9</td>
                <td>Entry 10</td>
                <td>Entry 11</td>
                <td>Entry 12</td>
            </tr>
            <tr>
                <td>Entry 13</td>
                <td>Entry 14</td>
                <td>Entry 15</td>
                <td>Entry 16</td>
            </tr>
        </tbody>
    </table>
<div class="hash d-none" hidden>hash_4c69cf8e1d59146e45ad34887b1535e0991cc33f</div><script data-shy-copy-guard>(function(){var C=[0x00AD,0x200B,0x200C,0x200D,0x2060,0xFEFF];var s=function(t){for(var i=0;i<C.length;i++){t=t.split(String.fromCharCode(C[i])).join("");}return t;};document.addEventListener("copy",function(e){var sel=document.getSelection();if(!sel||sel.isCollapsed||!e.clipboardData){return;}e.clipboardData.setData("text/plain",s(sel.toString()));try{var d=document.createElement("div");d.appendChild(sel.getRangeAt(0).cloneContents());e.clipboardData.setData("text/html",s(d.innerHTML));}catch(x){}e.preventDefault();});})();</script></body>
```


This is a markdown version of: [https://www.ionos.com/digitalguide/websites/web-development/html-table-styling/](https://www.ionos.com/digitalguide/websites/web-development/html-table-styling/) for AI/LLM consumption.