You are currently viewing Merging TailwindCSS into Flask apps

Merging TailwindCSS into Flask apps

TailwindCSS is a utility-first CSS framework for rapidly creating custom user interfaces. The main advantage of using TailwindCSS is that it is a highly customizable, low-level CSS framework and lets the user take full control of it.

It is a utility-first CSS framework which means that it provides utility classes to build custom designs without writing CSS as we do it traditionally. It puts an end to the use of weird names for CSS classes and IDs.

In this article, we’ll learn how to merge the TailwindCSS into our Flask app.

Building a flask app

Installing Flask

Before creating a flask app, we’ll begin the installation first. Open up your terminal and run the following command.

Creating the Flask server

Next, we’ll lay the pipeline for creating the flask server. Start by creating a folder in which we’ll put our files then inside that folder create an file.

Then, we’ll create a subfolder called templates in which we’ll store our HTML files and then create another subfolder called static which will be holding styles, scripts, and static images for our frontend.

In our file we’ll add the following code to create a flask server

The above code will create a simple flask server with a route that will be responsible for displaying the webpage from the index.html file which we haven’t created yet.

Add a new file called index.html inside the templates subfolder.

In our index.html file add the following HTML

Let’s start our server by running the following command in our terminal

This will start our development server and we can see our webpage serving on

Flask App Serving on localhost

Adding TailwindCSS into the flask

We’ll now perform the following steps to merge the CSS framework(TailwindCSS) into our flask app.

Installing TailwindCSS

We can use either npm or yarn to install the dependency in our project folder. Run the following command in the terminal for installing TailwindCSS using npm and yarn respectively.


I am using npm for this tutorial.

After running the command, the installation will begin and the following files will be created in the root directory.

Creating a configuration file

Run the following command in the terminal to create a configuration file.

The command will create a minimal tailwind.config.js file at the root of the project.

We need to do some modifications to the content section of the tailwind.config.js to configure the paths to all of your HTML templates.

We just added the templates folder where all the HTML files will go and used the glob pattern which makes it easy to match all of the content files in the project.

  • Use * to match anything except slashes and hidden files
  • Use ** to match zero or more directories
  • Use comma separate values between {} to match against a list of options

Additionally, we enabled the jit (just-in-time) mode since the JIT mode generates the CSS on-demand by scanning the template files.

Creating CSS files and adding tailwind directives

Inside the static folder, create two CSS folders named src and css and inside these folders add input.css and main.css files respectively.

Now, add the following code inside the src/input.css file to insert Tailwind’s base, components, and utilities styles into the CSS.

src/input.css file

Starting the build process

To generate the CSS from the preprocessor directives, we need to run the following command in the terminal.

Note: We will have to run the above code every time we add CSS to our HTML files to see the changes. So the best practice would be to make the above command a script, that will rebuild the CSS without the need to run the CSS build process code every time.

In the package.json file add the scripts

Now, run the script create-css in the terminal

The above command will eliminate the need to run the CSS build process every time after changing the code inside the HTML file.

Creating frontend with TailwindCSS

Now, to test if it is working, use the utility classes of the TailwindCSS to style the templates. Head over to the templates/index.html and edit the file.

Link the css/main.css file in the Head tag inside the index.html.


Flask app using TailwindCSS

Flask App using TailwindCSS


Usually, developers prefer to use Bootstrap to build UI for the frontend, but in this tutorial, we integrated TailwindCSS into our flask app.

We’ve covered the following steps in this tutorial

  • Created a demo flask app.
  • Installed the TailwindCSS into the project root directory and Configured it.
  • Added tailwind directives in the CSS files.
  • Ran the command to generate the CSS from the directives.

That’s all for now

Keep Coding✌✌