Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • PHP Tutorial
  • PHP Exercises
  • PHP Array
  • PHP String
  • PHP Calendar
  • PHP Filesystem
  • PHP Math
  • PHP Programs
  • PHP Array Programs
  • PHP String Programs
  • PHP Interview Questions
  • PHP GMP
  • PHP IntlChar
  • PHP Image Processing
  • PHP DsSet
  • PHP DsMap
  • PHP Formatter
  • Web Technology
Open In App
Next Article:
Installation & Setup Guide of Tailwind CSS with PHP
Next article icon

Installation & Setup Guide of Tailwind CSS with PHP

Last Updated : 25 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

TailwindCSS is an open-source "Utility-first framework" of CSS(Cascading style sheet). It contains tons of features. You can use its Utility classes to design Web pages without writing any single-line CSS. It's way much better than any other CSS framework like Bootstraps, Bulma, etc. You just have to use it directly in your markup.

PHP is a server-side scripting language used to manage the Back-end of the Web Application. It is open-source which means it is free to download and use.

It's quite challenging to use TailwindCSS with PHP but it's possible. Some People want to get rid of Bootstrap and want to shift to Tailwind but face the challenge of how to integrate it. If we talk about HTML, it's quite simple to use because tailwind postCSS and CLI directly support HTML but not php.

In this article, we will learn how to set up Tailwind CSS with PHP. Before installing Tailwind CSS, make sure you have node and npm installed.

Make sure your xampp is installed on your desktop and Apache and MySQL are running in xampp server.

So there are 2 ways to Install & Setup Guide to install TailwindCSS with PHP:

Method 1: Put the PlayCDN script directly into the PHP file and run it directly:

Create a new File named index.php in xampp/htdocs folder.

Now at the Following CDN command in the last of the HTML body section.

<script src="https://cdn.tailwindcss.com"></script>
PHP
<!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>Document</title> </head>  <body>    <h1 class="text-3xl font-bold                   underline bg-slate-500 p-5                text-blue-300">           Tailwindcss using Play CDN    </h1>     <script src="https://cdn.tailwindcss.com"></script> </body> </html> 

Go to the browser and type localhost/foldername.

Output:

 

But for Production, CDN is not recommended. 

Method 2: By Using CLI with a small modification: You can use TailwindCSS with PHP easily by following these steps.

Create a Brand New folder and Open that folder with any Code Editor. For Example: VS code, Atom, Sublime, etc.

Step 1. Create a new File named index.php in xampp/htdocs folder.

 

Step 2: Install TailwindCSS: Use Terminal to install Tailwindcss by following these commands.

Copy the following command and paste it into the VC code terminal in the folder directory you created earlier.

npm install -D tailwindcss

Step 3:  Create TailwindCSS config file: Copy and Paste this command into the Terminal to create TailwindCSS config File

npx tailwindcss init

This command will create one file named as tailwind.config.js.

 

Step 4: Configure the tailwind.config.js file: Open the tailwind.config.js file and replace the existing code with this code.

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

Step 5: Add the Tailwind directives to your CSS: create an input.css file in the folder(directory).

Paste the following code into it.

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

Now, link that input.css file in index.php that we created earlier.

Step 6: Start the Tailwind CLI build process: run the following command in the Terminal.

npx tailwindcss -i input.css -o output.css --watch

Project Structure:

 

Step 7: Check if the Setup is running: Copy and paste the following code in the index.php file to check if the Code is running and save it.

PHP
<!doctype html> <html> <head>     <meta charset="UTF-8">     <meta name="viewport"              content="width=device-width, initial-scale=1.0">     <link href="output.css" rel="stylesheet"> </head> <body>     <h1 class="text-3xl font-bold underline">         Hello world!     </h1> </body> </html> 

and Go to the browser and type localhost/foldername.

Output:

Method 3: Use Tailwindcss Standalone

Step 1: Download Tailwind CSS Standalone CLI

Download the Tailwind CSS standalone CLI from the official Tailwind CSS website.

Step 2: Set Up a CodeIgniter Project

Download and Install CodeIgniter:

  • Go to the CodeIgniter website and download the latest version.
  • Move the extracted CodeIgniter files to your web server directory (htdocs for XAMPP or www for WAMP).
  • Rename the folder to your project name (e.g., my_project) .This step is optional.

Set Up Your Project:

  • Configure your web server to serve the CodeIgniter project (e.g., using Apache or Nginx).
  • Make sure the base_url in application/config/config.php is set correctly.
  • base_url is http://localhost/your_project_dir_name

Step 3: Create the Tailwind CSS Stylesheet

1. Create a Tailwind CSS Configuration File:

  • Open a terminal in your project directory.
  • Run the following command to create a Tailwind configuration file:
npx tailwindcss init 

This will create a tailwind.config.js file in your project.

2. Create the Tailwind Input File:

Create the public Folder:

  • At the root of your CodeIgniter project (where you have the application, system, and user_guide folders), create a new folder named public.

Create the css Directory:

  • Inside the public folder, create a new folder named css.
  • Inside the css folder, create a file named tailwind-input.css.
//public/css/tailwind-input.css  @tailwind base; @tailwind components; @tailwind utilities; 

3. Build the Tailwind CSS File:

  • Use the Tailwind CLI to build the CSS file:
npx tailwindcss -i ./public/css/tailwind-input.css -o ./public/css/tailwind-output.css --watch 

This will generate a tailwind-output.css file in the public/css directory.

Step 4: Set Up the View in CodeIgniter

1. Create a View File:

  • In the application/views directory, create a file named layout.php.
PHP
//application/views/layout.php <!DOCTYPE html> <html lang="en">    <head>     <meta charset="UTF-8">     <meta name="viewport" content="width=device-width, initial-scale=1.0">     <title>Document</title>    <link href="<?= base_url('public/css/tailwind-output.css') ?>" rel="stylesheet"> </head>    <body>     <div>         <h1 class="text-4xl font-bold text-center">   			Hello, Tailwind CSS with CodeIgniter!   		</h1>     </div> </body>    </html> 

2. Create a Controller:

  • In the application/controllers directory, create a file named Welcome.php (If not exist).
PHP
//application/controllers/Welcome.php <?php defined('BASEPATH') OR exit('No direct script access allowed');  class Welcome extends CI_Controller {      public function __construct() {         parent::__construct();         $this->load->helper('url');     }      public function index() {         $this->load->view('layout');     } } 

Step 5: Test the Setup

Start Your Web Server:

  • Ensure your web server is running and serving the CodeIgniter project.

Access the Application:

  • Open your browser and navigate to your CodeIgniter project's base URL (e.g., http://localhost/your-project-name).

You should see a page with a styled heading, confirming that Tailwind CSS is working correctly with your CodeIgniter view.

Output:

Screenshot-2024-07-18-224602
Final output

Next Article
Installation & Setup Guide of Tailwind CSS with PHP

S

sahunitin970
Improve
Article Tags :
  • PHP

Similar Reads

    What is Tailwind CSS ?
    Tailwind CSS is a CSS framework that helps developers in the rapid designing of the web without shifting back and forth between different pages for HTML and CSS. Tailwind CSS doesn't come with pre-designed components, rather it provides set of utility classes that can be used to style HTML elements
    3 min read
    How to Add Tailwind CSS to HTML ?
    Tailwind CSS is a utility-first framework offering pre-defined classes for rapid styling of HTML elements. It simplifies customization and accelerates web development workflows.Integration Options:CDN Integration: Quickly add Tailwind CSS via a CDN link in the <head> of your HTML.Using npm: In
    3 min read
    Top Open Source Libraries For Tailwind CSS Components
    Tailwind CSS is a free and open-source, utility-first CSS framework used for building custom user interfaces rapidly. Tailwind is highly customizable and using it on your website makes development and responsiveness easy. There are almost 500+ components that can be used in your Tailwind projects an
    6 min read
    How to setup Tailwind CSS with Vite ?
    Tailwind CSS is a popular CSS framework that simplifies the process of designing user interfaces with its utility-first approach. Vite is a build tool that provides a fast development experience for modern web projects. Together, they make front-end development efficient and enjoyable. In this artic
    4 min read
    What is Tailwind CSS?
    Tailwind CSS is the utility-first CSS framework that provides the low-level utility classes that can be used to build custom designs, without the need to write the traditional CSS. Unlike traditional frameworks like Bootstrap, Tailwind CSS does not provide predefined components like buttons or cards
    5 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences