Open In App

Create Tables UI using React and Tailwind CSS

Last Updated : 18 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

The Basic Table is the simplest form of a table ideal for displaying simple data without any additional styling. It consists of a header row and data rows. This type of table is useful for presenting lists or tabular data where complex styling or interaction is not required.

This is perfect for displaying lists of items such as names, ages, and cities. It serves as a foundational structure for more complex table designs.

Prerequisites

Below are the types of Tables we will be creating using React and Tailwind CSS:

Basic Table

The Basic Table is the simplest form of a table ideal for displaying simple data without any additional styling. It consists of a header row and data rows. This type of table is useful for presenting lists or tabular data where complex styling or interaction is not required.

Structure:

  • Header: Contains column titles.
  • Rows: Displays data under each column.
  • Styling: Basic borders and padding are applied for readability.

Use Case: Perfect for displaying lists of items such as names, ages and cities. It serves as a foundational structure for more complex table designs.

Striped Table

The Striped Table alternates row colors to improve readability and help users differentiate between rows more easily. This visual effect is achieved by applying different background colors to alternate rows.

Structure:

  • Header: Column titles.
  • Rows: Data rows with alternating background colors.
  • Styling: Light gray background color for every other row.

Use Case : Ideal for tables with numerous rows where distinguishing between rows is important for better readability such as in reports or long lists.

Bordered Table

The Bordered Table features borders around each cell giving the table a grid-like appearance. This style enhances the separation of data points and makes the table's structure clearer.

Structure:

  • Header: Column titles with borders.
  • Rows: Data cells with borders on all sides.
  • Styling: Borders applied to the table, header and cells for a defined layout.

Use Case: Suitable for detailed data presentations where clarity and separation between data points are crucial such as in financial statements or detailed reports.

Responsive Table

The Responsive Table is designed to adapt to various screen sizes by allowing horizontal scrolling on smaller screens. This ensures that the table remains readable and functional across devices.

Structure:

  • Header: Column titles.
  • Rows: Data cells.
  • Styling: Wrapped in a div with overflow-x-auto class to enable horizontal scrolling.

Use Case: Ideal for tables with many columns where the content may not fit on smaller screens. It is especially useful for displaying extensive data sets in a mobile-friendly format.

Steps To Create Tables UI

Here we created a sample react js project then we installed tailwind css once it is completed we start development for Tables UI using React and Tailwind CSS. Below we provide step by step process to achieve this application.

Step 1: Set up a React Application

First created a sample React JS application by using mentioned command then navigate to project folder

npx create-react-app react-app
cd react-app

Project Structure

Screenshot-2024-09-12-114329
Project folder

Step 2: Install and Configure Tailwind CSS

Once Project is created successfully Now install and configure the Tailwind css by using below commands in your project.

cd react-app
npm install -D tailwindcss postcss autoprefixer
npx tailwindcss init -p

Example: Now we will need to develop user interface for Tables UI using tailwind css and html. This example shows the Demonstration of Four kinds of Table using Tailwind CSS and HTML.

CSS
/*index.css*/

@tailwind base;
@tailwind components;
@tailwind utilities;

body {
    margin: 0;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen',
        'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue',
        sans-serif;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

code {
    font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New',
        monospace;
}
JavaScript
// src/App.js

import React from 'react';

function App() {
    return (
        <div className="p-6">
            <h1 className="text-2xl font-bold mb-4">Tables UI with Tailwind CSS</h1>

            {/* Basic Table */}
            <section className="mb-8">
                <h2 className="text-xl
                 font-semibold mb-2">Basic Table</h2>
                <table className="min-w-full
                 bg-white border 
                 border-gray-200">
                    <thead>
                        <tr className="border-b border-gray-200">
                            <th className="px-4 py-2
                             text-left">
                                Name
                            </th>
                            <th className="px-4 py-2
                             text-left">
                                Age
                            </th>
                            <th className="px-4 py-2 
                            text-left">
                                City
                            </th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr>
                            <td className="px-4 
                            py-2">
                                John Doe
                            </td>
                            <td className="px-4 
                            py-2">
                                30
                            </td>
                            <td className="px-4
                             py-2">
                                New York
                            </td>
                        </tr>
                        <tr className="bg-gray-100">
                            <td className="px-4 py-2">
                                Jane Smith
                            </td>
                            <td className="px-4 
                            py-2">
                                25
                            </td>
                            <td className="px-4 
                            py-2">
                                San Francisco
                            </td>
                        </tr>
                    </tbody>
                </table>
            </section>

            {/* Striped Table */}
            <section className="mb-8">
                <h2 className="text-xl font-semibold mb-2">
                    Striped Table</h2>
                <table className="min-w-full bg-white 
                border border-gray-200">
                    <thead>
                        <tr className="border-b 
                        border-gray-200">
                            <th className="px-4 py-2 
                            text-left">
                                Product
                            </th>
                            <th className="px-4 py-2 
                            text-left">
                                Price
                            </th>
                            <th className="px-4 py-2 
                            text-left">
                                Quantity
                            </th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr className="bg-gray-100">
                            <td className="px-4 py-2">
                                Laptop
                            </td>
                            <td className="px-4 py-2">
                                $1000
                            </td>
                            <td className="px-4 py-2">
                                1
                            </td>
                        </tr>
                        <tr>
                            <td className="px-4 py-2">
                                Headphones
                            </td>
                            <td className="px-4 py-2">
                                $200
                            </td>
                            <td className="px-4 py-2">
                                2
                            </td>
                        </tr>
                    </tbody>
                </table>
            </section>

            {/* Bordered Table */}
            <section className="mb-8">
                <h2 className="text-xl 
                font-semibold mb-2">Bordered Table</h2>
                <table className="min-w-full bg-white border border-gray-300">
                    <thead>
                        <tr className="border-b border-gray-300">
                            <th className="px-4 py-2
                             text-left">
                                Course
                            </th>
                            <th className="px-4 py-2 
                            text-left">
                                Instructor
                            </th>
                            <th className="px-4 py-2 
                            text-left">
                                Duration
                            </th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr>
                            <td className="px-4 py-2 border-t
                             border-gray-300">
                                React Basics
                            </td>
                            <td className="px-4 py-2 border-t
                             border-gray-300">
                                Alice Johnson
                            </td>
                            <td className="px-4 py-2 border-t
                             border-gray-300">
                                4 weeks
                            </td>
                        </tr>
                        <tr className="bg-gray-50">
                            <td className="px-4 py-2 border-t
                             border-gray-300">
                                Tailwind CSS
                            </td>
                            <td className="px-4 py-2 border-t
                             border-gray-300">
                                Bob Smith
                            </td>
                            <td className="px-4 py-2 border-t
                             border-gray-300">
                                3 weeks
                            </td>
                        </tr>
                    </tbody>
                </table>
            </section>

            {/* Responsive Table */}
            <section className="mb-8">
                <h2 className="text-xl font-semibold mb-2">Responsive Table</h2>
                <div className="overflow-x-auto">
                    <table className="min-w-full bg-white border border-gray-200">
                        <thead>
                            <tr className="border-b border-gray-200">
                                <th className="px-4 py-2 text-left">
                                    Employee
                                </th>
                                <th className="px-4 py-2 text-left">
                                    Department
                                </th>
                                <th className="px-4 py-2 text-left">
                                    Salary
                                </th>
                            </tr>
                        </thead>
                        <tbody>
                            <tr>
                                <td className="px-4 py-2">
                                    Mark Lee
                                </td>
                                <td className="px-4 py-2">
                                    Engineering
                                </td>
                                <td className="px-4 py-2">
                                    $90000
                                </td>
                            </tr>
                            <tr className="bg-gray-100">
                                <td className="px-4 py-2">
                                    Sophie Turner
                                </td>
                                <td className="px-4 py-2">
                                    Marketing
                                </td>
                                <td className="px-4 py-2">
                                    $75000
                                </td>
                            </tr>
                        </tbody>
                    </table>
                </div>
            </section>
        </div>
    );
}

export default App;
JavaScript
/*tailwind.config.js*/

/** @type {import('tailwindcss').Config} */
module.exports = {
    content: [
        "./src/**/*.{js,jsx,ts,tsx}",
    ],
    theme: {
        extend: {},
    },
    plugins: [],
}

Run the Application:

Once Development is completed Now we need run the react js application by using below command. By default the react js application run on port number 3000.

npm start

Output:

http://localhost:3000/

Next Article

Similar Reads