DEV Community

Cover image for How to Create Tailwind CSS tables
Sampson Ovuoba for Devwares

Posted on • Originally published at devwares.com on

How to Create Tailwind CSS tables

Introduction

Using Tailwind CSS to create UI components is easy. Tailwind CSS is the first utility CSS framework that allows you, to use low-level utility classes to build fast UI.

Setting up some of these components can be easy and will make it possible for you to use the CSS framework without worrying about fighting it. You can check out some advantages of this framework here.

However, we will use Tailwind CSS to build some tables. Tailwind CSS tables require a bit of code to set it up and may seem complicated. But its really not.

It's as easy as possible and, I will walk you through some Tailwind CSS tables you can start within your projects.

Table of Content

  • Where to start
  • Installing a Tailwind CSS to your project
  • Building the first table
  • Building the second table
  • Building the third table
  • Conclusion

Where to start

We will start by creating a project directory.

Code:

mkdir build-Tailwind-tables & cd build-Tailwind-tables
Enter fullscreen mode Exit fullscreen mode

The code above will create an empty directory and also direct you to the directory. These will become your current working directory. At this stage we can now initialize our project by writing this code.

Code:

npm init -y.
Enter fullscreen mode Exit fullscreen mode

this will create a package.json file on the current directory.

Installing Tailwind CSS

At this stage we can go ahead to install tailwind css to our project. We can do this by running the following code. Using npm .

Code:

npm install tailwindcss.
Enter fullscreen mode Exit fullscreen mode

when using yarn

Code:

yarn add tailwindcss.
Enter fullscreen mode Exit fullscreen mode

We can go ahead to create a style sheet file at this stage. This style sheet file is where we will compile our tailwind.css file into using the tailwind TLC tool. We can call this style sheet file style.css.

Inside the tailwind.css file we will use @tailwind directive to inject tailwind’s base, components and utilities. Like the code below

@tailwind base;
@tailwind components;
@tailwind utilities;
Enter fullscreen mode Exit fullscreen mode

For some of us that might wish to customize their tailwind css utilities. This can be done by writing the following code on the terminal.

Code:

npx tailwindcss init
Enter fullscreen mode Exit fullscreen mode

The command above will automatically generate a tailwind.config.js file. This file is where you can modify or customize any utility on tailwind. The generated file will look like the code below.

Code:

module.exports = {
  theme: {
    extend: {},
  },
  variants: {
    extend: {},
  },
  plugins: [],
};
Enter fullscreen mode Exit fullscreen mode

The next thing to do is to process our CSS. We can do this by running the following command on our terminal

Code:

npx tailwindcss build styles.css -o tailwind.css.
Enter fullscreen mode Exit fullscreen mode

The code above will generate our tailwind css codes and will get the project ready for you.

Building the table

After the installation of tailwind css. The next thing we are to do is to create an index.html file. Inside the html file we will write the following code

Code:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Tailwind tables</title>
    <link rel="stylesheet" href="style.css" />
  </head>
  <body></body>
</html>
Enter fullscreen mode Exit fullscreen mode

The code above is a HTML template which contains the link to the processed Tailwind CSS. We linked the style.css to our HTML template to enable us use Tailwind CSS utilities in our code.

Adding the styles.

We can now go ahead to code the body of the table and add some tailwind CSS utility styles.

Code:

<div class="container flex justify-center mx-auto">
  <div class="flex flex-col">
    <div class="w-full">
      <div class="border-b border-gray-200 shadow">
        <table class="divide-y divide-green-400 ">
          <thead class="bg-gray-50">
            <tr>
              <th class="px-6 py-2 text-xs text-gray-500">
                ID
              </th>
              <th class="px-6 py-2 text-xs text-gray-500">
                Name
              </th>
              <th class="px-6 py-2 text-xs text-gray-500">
                Email
              </th>
              <th class="px-6 py-2 text-xs text-gray-500">
                Created_at
              </th>
              <th class="px-6 py-2 text-xs text-gray-500">
                Edit
              </th>
              <th class="px-6 py-2 text-xs text-gray-500">
                Delete
              </th>
            </tr>
          </thead>
          <tbody class="bg-white divide-y divide-gray-300">
            <tr class="whitespace-nowrap">
              <td class="px-6 py-4 text-sm text-gray-500">
                1
              </td>
              <td class="px-6 py-4">
                <div class="text-sm text-gray-900">
                  Adam joe
                </div>
              </td>
              <td class="px-6 py-4">
                <div class="text-sm text-gray-500">adamjoe@example.com</div>
              </td>
              <td class="px-6 py-4 text-sm text-gray-500">
                2021-12-12
              </td>
              <td class="px-6 py-4">
                <a href="#">
                  <svg
                    xmlns="http://www.w3.org/2000/svg"
                    class="w-6 h-6 text-blue-400"
                    fill="none"
                    viewBox="0 0 24 24"
                    stroke="currentColor"
                  >
                    <path
                      stroke-linecap="round"
                      stroke-linejoin="round"
                      stroke-width="2"
                      d="M11 5H6a2 2 0 00-2 2v11a2 2 0 002 2h11a2 2 0 002-2v-5m-1.414-9.414a2 2 0 112.828 2.828L11.828 15H9v-2.828l8.586-8.586z"
                    />
                  </svg>
                </a>
              </td>
              <td class="px-6 py-4">
                <a href="#">
                  <svg
                    xmlns="http://www.w3.org/2000/svg"
                    class="w-6 h-6 text-red-400"
                    fill="none"
                    viewBox="0 0 24 24"
                    stroke="currentColor"
                  >
                    <path
                      stroke-linecap="round"
                      stroke-linejoin="round"
                      stroke-width="2"
                      d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16"
                    />
                  </svg>
                </a>
              </td>
            </tr>
            <tr class="whitespace-nowrap">
              <td class="px-6 py-4 text-sm text-gray-500">
                2
              </td>
              <td class="px-6 py-4">
                <div class="text-sm text-gray-900">
                  Jon doe
                </div>
              </td>
              <td class="px-6 py-4">
                <div class="text-sm text-gray-500">jhondoe@example.com</div>
              </td>
              <td class="px-6 py-4 text-sm text-gray-500">
                2021-1-12
              </td>
              <td class="px-6 py-4">
                <a href="#">
                  <svg
                    xmlns="http://www.w3.org/2000/svg"
                    class="w-6 h-6 text-blue-400"
                    fill="none"
                    viewBox="0 0 24 24"
                    stroke="currentColor"
                  >
                    <path
                      stroke-linecap="round"
                      stroke-linejoin="round"
                      stroke-width="2"
                      d="M11 5H6a2 2 0 00-2 2v11a2 2 0 002 2h11a2 2 0 002-2v-5m-1.414-9.414a2 2 0 112.828 2.828L11.828 15H9v-2.828l8.586-8.586z"
                    />
                  </svg>
                </a>
              </td>
              <td class="px-6 py-4">
                <a href="#">
                  <svg
                    xmlns="http://www.w3.org/2000/svg"
                    class="w-6 h-6 text-red-400"
                    fill="none"
                    viewBox="0 0 24 24"
                    stroke="currentColor"
                  >
                    <path
                      stroke-linecap="round"
                      stroke-linejoin="round"
                      stroke-width="2"
                      d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16"
                    />
                  </svg>
                </a>
              </td>
            </tr>
            <tr class="whitespace-nowrap">
              <td class="px-6 py-4 text-sm text-gray-500">
                3
              </td>
              <td class="px-6 py-4">
                <div class="text-sm text-gray-900">
                  Emily chan
                </div>
              </td>
              <td class="px-6 py-4">
                <div class="text-sm text-gray-500">emilychan@example.com</div>
              </td>
              <td class="px-6 py-4 text-sm text-gray-500">
                2021-1-12
              </td>
              <td class="px-6 py-4">
                <a href="#">
                  <svg
                    xmlns="http://www.w3.org/2000/svg"
                    class="w-6 h-6 text-blue-400"
                    fill="none"
                    viewBox="0 0 24 24"
                    stroke="currentColor"
                  >
                    <path
                      stroke-linecap="round"
                      stroke-linejoin="round"
                      stroke-width="2"
                      d="M11 5H6a2 2 0 00-2 2v11a2 2 0 002 2h11a2 2 0 002-2v-5m-1.414-9.414a2 2 0 112.828 2.828L11.828 15H9v-2.828l8.586-8.586z"
                    />
                  </svg>
                </a>
              </td>
              <td class="px-6 py-4">
                <a href="#">
                  <svg
                    xmlns="http://www.w3.org/2000/svg"
                    class="w-6 h-6 text-red-400"
                    fill="none"
                    viewBox="0 0 24 24"
                    stroke="currentColor"
                  >
                    <path
                      stroke-linecap="round"
                      stroke-linejoin="round"
                      stroke-width="2"
                      d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16"
                    />
                  </svg>
                </a>
              </td>
            </tr>
            <tr class="whitespace-nowrap">
              <td class="px-6 py-4 text-sm text-gray-500">
                4
              </td>
              <td class="px-6 py-4">
                <div class="text-sm text-gray-900">
                  susan coby
                </div>
              </td>
              <td class="px-6 py-4">
                <div class="text-sm text-gray-500">susancoby@example.com</div>
              </td>
              <td class="px-6 py-4 text-sm text-gray-500">
                2021-1-12
              </td>
              <td class="px-6 py-4">
                <a href="#">
                  <svg
                    xmlns="http://www.w3.org/2000/svg"
                    class="w-6 h-6 text-blue-400"
                    fill="none"
                    viewBox="0 0 24 24"
                    stroke="currentColor"
                  >
                    <path
                      stroke-linecap="round"
                      stroke-linejoin="round"
                      stroke-width="2"
                      d="M11 5H6a2 2 0 00-2 2v11a2 2 0 002 2h11a2 2 0 002-2v-5m-1.414-9.414a2 2 0 112.828 2.828L11.828 15H9v-2.828l8.586-8.586z"
                    />
                  </svg>
                </a>
              </td>
              <td class="px-6 py-4">
                <a href="#">
                  <svg
                    xmlns="http://www.w3.org/2000/svg"
                    class="w-6 h-6 text-red-400"
                    fill="none"
                    viewBox="0 0 24 24"
                    stroke="currentColor"
                  >
                    <path
                      stroke-linecap="round"
                      stroke-linejoin="round"
                      stroke-width="2"
                      d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16"
                    />
                  </svg>
                </a>
              </td>
            </tr>
          </tbody>
        </table>
      </div>
    </div>
  </div>
</div>
Enter fullscreen mode Exit fullscreen mode

Our webpage will look like the image below.

Tables

Conclusion

Tailwind CSS utilities make it easy to build beautiful tables for your webpage. We looked at how we can use Tailwind CSS to build beautiful tables and install Tailwind CSS to your project. By now, you should be able to use Tailwind CSS to build beautiful tables.

Design and code tailwind css websites 3x faster

We created a tool to visually build tailwind css components, prototypes, websites, and webapps. Ship projects faster using an intuitive tailwind builder and editor.Try Windframe out for free.

WINDFRAME

Resources

Top comments (0)