Open In App

How to style HTML Tables with Tailwind CSS ?

Last Updated : 26 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

TailwindCSS offers utilities to make tables visually appealing. The colored header table emphasizes visual hierarchy through background color, while the stripped row table improves readability with alternating row colors. Similarly, the hover effect table enhances user interaction by applying visual feedback on row hover, and the border to all rows and columns table provides clear visual separation and structure through border styling on all elements.

Coloured Header Table

In the colored Header table, we will implement a design to the table where the color of the header of the table specifying column names can be changed. The ‘<thead>’ element is styled with ‘bg-pink-500’ for a pink background and ‘text-white’ for white text, enhancing readability. Table headers (‘<th>’) have a gray border (‘border-gray-300’) to separate cells visually and padding (‘px-4 py-2’) for spacing, ensuring a clean layout. This combination of styles creates a visually appealing and well-organized table header.

Example: Illustration of styling HTML tables with Tailwind CSS using Coloured Header Table.

HTML




<!DOCTYPE html>
<html lang="en">
 
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0">
    <title>Colored Header Table</title>
    <link href=
          rel="stylesheet">
</head>
 
<body class="bg-gray-200 p-4">
  <h1 class="text-center text-2xl text-green-700">GeeksforGeeks</h1>
    <h3 class="text-center text-2xl text-green-700">Colored Header Table</h3>
    <table class="table-auto w-full bg-white border border-gray-300">
        <thead class="bg-pink-500 text-white">
            <tr>
                <th class="border border-gray-300 px-4 py-2">
                  Name
                  </th>
                <th class="border border-gray-300 px-4 py-2">
                  ID
                  </th>
                <th class="border border-gray-300 px-4 py-2">
                  Address
                  </th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td class="border border-gray-300 px-4 py-2">
                      Shiv
                  </td>
                <td class="border border-gray-300 px-4 py-2">
                      1
                  </td>
                <td class="border border-gray-300 px-4 py-2">
                      Noida
                  </td>
            </tr>
            <tr>
                <td class="border border-gray-300 px-4 py-2">
                      Krishn
                  </td>
                <td class="border border-gray-300 px-4 py-2">
                      2
                  </td>
                <td class="border border-gray-300 px-4 py-2">
                      Noida
                  </td>
            </tr>
            <tr>
                <td class="border border-gray-300 px-4 py-2">
                      Ram
                  </td>
                <td class="border border-gray-300 px-4 py-2">
                      3
                  </td>
                <td class="border border-gray-300 px-4 py-2">
                      Noida
                  </td>
            </tr>
        </tbody>
    </table>
</body>
 
</html>


Output:

l1

Stripped Row Table

In the stripped row table, we will be implementing a table design where every other row will have a different background. The ‘table’ element is styled with ‘table-auto’, ‘w-full’, and ‘bg-white’ classes for automatic column widths, full width, and a white background, respectively. Table header cells (‘<th>’) and data cells (‘<td>’) have a border (‘border-gray-300’) for visual separation, while alternating rows are given a light blue background (‘bg-blue-100’) to create a striped effect, enhancing readability and visual appeal.

Example: Illustration of styling HTML tables with Tailwind CSS using Stripped Row Table.

HTML




<!DOCTYPE html>
<html lang="en">
 
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0">
    <title>Striped Rows Table</title>
    <link href=
          rel="stylesheet">
</head>
 
<body class="p-4">
    <h1 class="text-center text-2xl text-green-700">
          GeeksforGeeks
      </h1>
    <h3 class="text-center text-2xl text-green-700 my-4">
          Stripped Row Table
      </h3>
    <table class="table-auto w-full bg-white border border-gray-300">
        <thead>
            <tr>
                <th class="border border-gray-300 px-4 py-2">Name</th>
                <th class="border border-gray-300 px-4 py-2">Class</th>
                <th class="border border-gray-300 px-4 py-2">Address</th>
            </tr>
        </thead>
        <tbody>
            <tr class="bg-blue-100">
                <td class="border border-gray-300 px-4 py-2">Shiv</td>
                <td class="border border-gray-300 px-4 py-2">1</td>
                <td class="border border-gray-300 px-4 py-2">Noida</td>
            </tr>
            <tr>
                <td class="border border-gray-300 px-4 py-2">Ganesh</td>
                <td class="border border-gray-300 px-4 py-2">1</td>
                <td class="border border-gray-300 px-4 py-2">Noida</td>
            </tr>
            <tr class="bg-blue-100">
                <td class="border border-gray-300 px-4 py-2">Krishn</td>
                <td class="border border-gray-300 px-4 py-2">1</td>
                <td class="border border-gray-300 px-4 py-2">Noida</td>
            </tr>
        </tbody>
    </table>
</body>
 
</html>


Output:

str1

Hover Effect Table

In hover effect table, the background color of the rows changes on hovering over it, giving us an enhanced view of the pointed row. The table is structured with automatic column widths (‘table-auto’), full width (‘w-full’), and a white background (‘bg-white’), while borders (‘border-gray-300’) provide visual separation. The table header is highlighted with a green background (‘bg-green-500’) and white text (‘text-white’). When hovering over table rows, a light green background (‘hover:bg-green-100’) is applied, enhancing interactivity and visual feedback. This combination of styles creates a visually appealing and interactive table with hover effects.

Example: Illustration of styling HTML tables with Tailwind CSS using Hover Effect Table.

HTML




<!DOCTYPE html>
<html lang="en">
 
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0">
    <title>Hover Effect Table</title>
    <link href=
          rel="stylesheet">
</head>
 
<body class="bg-gray-200 p-4">
    <h1 class="text-center text-2xl text-green-700">
        GeeksforGeeks
    </h1>
    <h3 class="text-center text-2xl text-green-700 my-4">
        Hover Effect Table
    </h3>
    <table class="table-auto w-full bg-white border border-gray-300">
        <thead class="bg-green-500 text-white">
            <tr>
                <th class="border border-gray-300 px-4 py-2">Name</th>
                <th class="border border-gray-300 px-4 py-2">Class</th>
                <th class="border border-gray-300 px-4 py-2">Address</th>
            </tr>
        </thead>
        <tbody>
            <tr class="hover:bg-green-100">
                <td class="border border-gray-300 px-4 py-2">Shiv</td>
                <td class="border border-gray-300 px-4 py-2">1</td>
                <td class="border border-gray-300 px-4 py-2">Noida</td>
            </tr>
            <tr class="hover:bg-green-100">
                <td class="border border-gray-300 px-4 py-2">Ganesh</td>
                <td class="border border-gray-300 px-4 py-2">1</td>
                <td class="border border-gray-300 px-4 py-2">Noida</td>
            </tr>
            <tr class="hover:bg-green-100">
                <td class="border border-gray-300 px-4 py-2">Krishn</td>
                <td class="border border-gray-300 px-4 py-2">1</td>
                <td class="border border-gray-300 px-4 py-2">Noida</td>
            </tr>
        </tbody>
    </table>
</body>
 
</html>


Output:

th1

Border to All rows and column

In this styled table, all the rows and columns have borders. It gives the overall table a bold look. The table is defined with automatic column widths (‘table-auto’), full width (‘w-full’), and a white background (‘bg-white’). Borders (‘border border-gray-300’) are added to all table elements, providing visual separation. The table header has a red background (‘bg-red-500’) and white text (‘text-white’). Each cell has a red border (‘border border-red-800’) and padding (‘px-4 py-2’) for consistent spacing and visual appeal.

Example: Illustration of styling HTML tables with Tailwind CSS using Border to All rows and column.

HTML




<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0">
    <title>Borders to All Rows and Columns Table</title>
    <link href=
          rel="stylesheet">
</head>
<body class="bg-gray-200 p-4">
    <table class="table-auto w-full bg-white border border-gray-300">
        <thead class="bg-red-500 text-white">
            <tr>
                <th class="border border-red-800 px-4 py-2">Header 1</th>
                <th class="border border-red-800 px-4 py-2">Header 2</th>
                <th class="border border-red-800 px-4 py-2">Header 3</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td class="border border-red-800 px-4 py-2">Data 1</td>
                <td class="border border-red-800 px-4 py-2">Data 2</td>
                <td class="border border-red-800 px-4 py-2">Data 3</td>
            </tr>
            <tr>
                <td class="border border-red-800 px-4 py-2">Data 4</td>
                <td class="border border-red-800 px-4 py-2">Data 5</td>
                <td class="border border-red-800 px-4 py-2">Data 6</td>
            </tr>
            <tr>
                <td class="border border-red-800 px-4 py-2">Data 7</td>
                <td class="border border-red-800 px-4 py-2">Data 8</td>
                <td class="border border-red-800 px-4 py-2">Data 9</td>
            </tr>
        </tbody>
    </table>
</body>
</html>


Output:

bta



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads