Open In App

Create Radio Groups UI using React and Tailwind CSS

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

React is a popular JavaScript library for building user interfaces combined with Tailwind CSS a utility-first CSS framework that offers a powerful approach to developing stylish and responsive components. This article shows how to build various radio group UIs using React and Tailwind CSS. It includes several designs such as simple lists inline lists lists with descriptions and color pickers each styled to meet specific design needs.

Prerequisites

Steps to Create Radio Groups UI

Step 1: Set up a React Application

npx create-react-app react-app

Step 2: Install node modules using the command.

npm install

Step 3: Navigate to the Project

cd react-app

Step 4: Install Tailwind CSS using the command.

npm install -D tailwindcss
npx tailwindcss init

Step 5: Configure the tailwind paths in your tailwind.config.js file.

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

Step 6: Add tailwind directives to your index.css file.

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

body {
overflow: hidden;
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;
}

Project Structure:

Screenshot-2024-09-12-114329
project structure

Updated Dependencies:

"dependencies": {
"react": "^18.3.1",
"react-dom": "^18.3.1"
}

Step 7: Install React Icons

npm install react-icons

Example: In this example we will create the footers using react and tailwind CSS

JavaScript
// src/App.js

import React, { useState } from 'react';
import { FaCheck, FaPaintBrush, FaSquare, FaList, 
FaTable, FaInfoCircle } from 'react-icons/fa';
import { HiOutlineMenu } from 'react-icons/hi';
import { BsFillCircleFill } from 'react-icons/bs';
import './index.css'; // Ensure Tailwind CSS is imported here

const App = () => {
    const [selected, setSelected] = useState('');

    const handleChange = (e) => {
        setSelected(e.target.value);
    };

    return (
        <div className="bg-white text-green-700 min-h-screen p-6">
            <h1 className="text-4xl font-bold mb-8 text-center"> 
                Radio Groups UI with React & Tailwind CSS</h1>

            <section className="space-y-6">
                <h2 className="text-3xl font-semibold mb-4 flex items-center">
                    <FaList className="mr-3" /> Simple List</h2>
                <div className="space-y-4">
                    <label className="flex items-center space-x-3">
                        <input
                            type="radio"
                            name="simple-list"
                            value="Option 1"
                            checked={selected === 'Option 1'}
                            onChange={handleChange}
                            className="form-radio text-green-500"
                        />
                        <span className="p-3 border border-green-500
                        rounded bg-white">Option 1</span>
                    </label>
                    <label className="flex items-center space-x-3">
                        <input
                            type="radio"
                            name="simple-list"
                            value="Option 2"
                            checked={selected === 'Option 2'}
                            onChange={handleChange}
                            className="form-radio text-green-500"
                        />
                        <span className="p-3 border border-green-500
                        
                        rounded bg-white">Option 2</span>
                    </label>
                </div>
            </section>

            <section className="space-y-6">
                <h2 className="text-3xl font-semibold mb-4 flex items-center">
                    <HiOutlineMenu className="mr-3" /> Simple Inline List</h2>
                <div className="flex space-x-6">
                    <label className="flex items-center space-x-3">
                        <input
                            type="radio"
                            name="inline-list"
                            value="Option A"
                            checked={selected === 'Option A'}
                            onChange={handleChange}
                            className="form-radio text-green-500"
                        />
                        <span className="p-3 border border-green-500 rounded bg-white">
                            Option A</span>
                    </label>
                    <label className="flex items-center space-x-3">
                        <input
                            type="radio"
                            name="inline-list"
                            value="Option B"
                            checked={selected === 'Option B'}
                            onChange={handleChange}
                            className="form-radio text-green-500"
                        />
                        <span className="p-3 border border-green-500 
                        rounded bg-white">Option B</span>
                    </label>
                </div>
            </section>

            <section className="space-y-6">
                <h2 className="text-3xl font-semibold mb-4 flex items-center">
                    <FaInfoCircle className="mr-3" /> List with Description</h2>
                <div className="space-y-4">
                    <label className="flex flex-col space-y-2">
                        <span className="text-gray-600">Description for Option 1</span>
                        <div className="flex items-center space-x-3">
                            <input
                                type="radio"
                                name="desc-list"
                                value="Option 1"
                                checked={selected === 'Option 1'}
                                onChange={handleChange}
                                className="form-radio text-green-500"
                            />
                            <span className="p-3 border border-green-500
                            rounded bg-white">Option 1</span>
                        </div>
                    </label>
                    <label className="flex flex-col space-y-2">
                        <span className="text-gray-600">Description for Option 2</span>
                        <div className="flex items-center space-x-3">
                            <input
                                type="radio"
                                name="desc-list"
                                value="Option 2"
                                checked={selected === 'Option 2'}
                                onChange={handleChange}
                                className="form-radio text-green-500"
                            />
                            <span className="p-3 border border-green-500
                            rounded bg-white">Option 2</span>
                        </div>
                    </label>
                </div>
            </section>

            <section className="space-y-6">
                <h2 className="text-3xl font-semibold mb-4 flex items-center">
                    <FaSquare className="mr-3" /> List with Inline Description</h2>
                <div className="space-y-4">
                    <label className="flex items-center space-x-3">
                        <span className="text-gray-600">Description for Option A</span>
                        <input
                            type="radio"
                            name="inline-desc-list"
                            value="Option A"
                            checked={selected === 'Option A'}
                            onChange={handleChange}
                            className="form-radio text-green-500"
                        />
                        <span className="p-3 border border-green-500 rounded
                        bg-white">Option A</span>
                    </label>
                    <label className="flex items-center space-x-3">
                        <span className="text-gray-600">Description for Option B</span>
                        <input
                            type="radio"
                            name="inline-desc-list"
                            value="Option B"
                            checked={selected === 'Option B'}
                            onChange={handleChange}
                            className="form-radio text-green-500"
                        />
                        <span className="p-3 border border-green-500 
                        rounded bg-white">Option B</span>
                    </label>
                </div>
            </section>

            <section className="space-y-6">
                <h2 className="text-3xl font-semibold mb-4 flex items-center">
                    <FaCheck className="mr-3" /> List with Radio on Right</h2>
                <div className="space-y-4">
                    <label className="flex items-center justify-end space-x-3">
                        <span className="p-3 border border-green-500
                        rounded bg-white">Option X</span>
                        <input
                            type="radio"
                            name="radio-right-list"
                            value="Option X"
                            checked={selected === 'Option X'}
                            onChange={handleChange}
                            className="form-radio text-green-500"
                        />
                    </label>
                    <label className="flex items-center justify-end space-x-3">
                        <span className="p-3 border border-green-500 
                        rounded bg-white">Option Y</span>
                        <input
                            type="radio"
                            name="radio-right-list"
                            value="Option Y"
                            checked={selected === 'Option Y'}
                            onChange={handleChange}
                            className="form-radio text-green-500"
                        />
                    </label>
                </div>
            </section>

            <section className="space-y-6">
                <h2 className="text-3xl font-semibold mb-4 flex items-center">
                    <BsFillCircleFill className="mr-3" /> Color Picker</h2>
                <div className="flex space-x-6">
                    <label className="flex items-center space-x-3">
                        <input
                            type="radio"
                            name="color-picker"
                            value="Red"
                            checked={selected === 'Red'}
                            onChange={handleChange}
                            className="form-radio text-red-500"
                        />
                        <span className="p-3 bg-red-500 text-white rounded">Red</span>
                    </label>
                    <label className="flex items-center space-x-3">
                        <input
                            type="radio"
                            name="color-picker"
                            value="Green"
                            checked={selected === 'Green'}
                            onChange={handleChange}
                            className="form-radio text-green-500"
                        />
                        <span className="p-3 bg-green-500 text-white
                        rounded">Green</span>
                    </label>
                </div>
            </section>

            <section className="space-y-6">
                <h2 className="text-3xl font-semibold mb-4 flex items-center">
                    <FaTable className="mr-3" /> Simple Table</h2>
                <div className="overflow-x-auto bg-white border
                border-gray-200 rounded-lg shadow-md">
                    <table className="min-w-full bg-white">
                        <thead>
                            <tr className="bg-green-500 text-white">
                                <th className="py-3 px-4 border-b">Item</th>
                                <th className="py-3 px-4 border-b">Description</th>
                            </tr>
                        </thead>
                        <tbody>
                            <tr>
                                <td className="py-2 px-4 border-b">Item 1</td>
                                <td className="py-2 px-4 border-b">
                                Description for item 1</td>
                            </tr>
                            <tr>
                                <td className="py-2 px-4 border-b">Item 2</td>
                                <td className="py-2 px-4 border-b">
                                Description for item 2</td>
                            </tr>
                        </tbody>
                    </table>
                </div>
            </section>
        </div>
    );
};

export default App;

To start the Application run the following command:

npm start

Output:

Screenshot-2024-09-15-200228
output

Conclusion

This setup demonstrates how to design and align various UI elements using React and Tailwind CSS. The design focuses on a clean organized layout with a white background and green text making the UI visually appealing and user friendly.


Next Article

Similar Reads