Style FusionAuth Themes with Tailwind CSS

On this page

When your application uses Tailwind CSS, you can use the same Tailwind CSS classes in an advanced theme. The Tailwind CSS build scans the theme templates for class names and writes the stylesheet that the hosted login pagesThe login, registration, password recovery, and multi-factor authentication pages that FusionAuth serves. A Theme controls the look of the pages. use. The FusionAuth CLI uploads the templates and the stylesheet to FusionAuth.

This guide covers Tailwind CSS 4 and Tailwind CSS 3. Tailwind CSS 4 supports Safari 16.4 and later, Chrome 111 and later, and Firefox 128 and later. When the users of your login pages need older browsers, use Tailwind CSS 3.

Prerequisites#

  • Node.js 20 or later

Set Up FusionAuth#

  1. Navigate to Customization -> Themes and click Add . Enter a Name for the theme and click Save .

    The form to create a theme in the Admin UI.
    The form to create a theme in the Admin UI.
  2. Navigate to Settings -> API Keys and click Add . Select the GET and PATCH permissions for /api/theme/, and click Save .

    The form to create an API key, with the theme permissions highlighted.
    The form to create an API key, with the theme permissions highlighted.

Create the Project#

  1. Create a Node.js project, and install the FusionAuth CLI and Tailwind CSS:

    $ mkdir fusionauth-tailwind
    $ cd fusionauth-tailwind
    $ npm init -y
    $ npm install @fusionauth/cli tailwindcss @tailwindcss/cli
    
  2. Create input.css in the project root. The file imports Tailwind CSS, and @source adds the FreeMarker templates in the tpl directory to the build. Add your own classes to this file:

    @import "tailwindcss";
    @source "./tpl";
    
  3. Add the following scripts to package.json. Replace <themeId> and <apiKey> with the Id of the theme and the API key:

    "scripts": {
      "watch:tailwind": "tailwindcss -i ./input.css -o ./tpl/stylesheet.css --watch",
      "watch:theme": "fusionauth theme:watch <themeId> -k <apiKey> -h http://localhost:9011"
    },
    
  1. Create a Node.js project, install the FusionAuth CLI and Tailwind CSS, and create a Tailwind CSS configuration file:

    $ mkdir fusionauth-tailwind
    $ cd fusionauth-tailwind
    $ npm init -y
    $ npm install @fusionauth/cli tailwindcss@3
    $ npx tailwindcss init
    
  2. Set content in tailwind.config.js to the FreeMarker templates in the tpl directory:

    /** @type {import('tailwindcss').Config} */
    module.exports = {
      content: ['./tpl/*.ftl'],
      theme: {
        extend: {}
      },
      plugins: []
    }
    
  3. Create input.css in the project root. The file imports the Tailwind CSS base styles, components, and utilities. Add your own classes to this file:

    @tailwind base;
    @tailwind components;
    @tailwind utilities;
    
  4. Add the following scripts to package.json. Replace <themeId> and <apiKey> with the Id of the theme and the API key:

    "scripts": {
      "watch:tailwind": "tailwindcss -i ./input.css -o ./tpl/stylesheet.css --watch",
      "watch:theme": "fusionauth theme:watch <themeId> -k <apiKey> -h http://localhost:9011"
    },
    

Prepare the Theme#

  1. Download the theme. Replace <themeId> and <apiKey> again:

    $ npx fusionauth theme:download <themeId> -k <apiKey> -h http://localhost:9011
    

    The CLI writes the templates to .ftl files in the tpl directory, and the stylesheet to tpl/stylesheet.css.

  2. Remove the link tag for the default FusionAuth stylesheet from the head macro in tpl/helpers.ftl. The default styles conflict with Tailwind CSS. Recent themes link fusionauth-hosted.css:

    <link rel="stylesheet" href="${request.contextPath}/css/entrypoints/fusionauth-hosted.css?version=${version}"/>
    

    Older themes link fusionauth-style.css instead.

  3. Upload the theme to apply the change to tpl/helpers.ftl:

    $ npx fusionauth theme:upload <themeId> -k <apiKey> -h http://localhost:9011
    

Edit the Theme#

Run each of the following scripts in a separate terminal:

  • npm run watch:tailwind rebuilds tpl/stylesheet.css when a template changes.
  • npm run watch:theme uploads the templates and the stylesheet to FusionAuth when a file in the tpl directory changes.

When a template uses a Tailwind CSS class, the build adds the class to the stylesheet:

<div class="bg-gray-200">
  <h1 class="text-2xl">Hello World</h1>
</div>

To preview the theme, navigate to Customization -> Themes , choose the theme, and click .

The themes list, with the preview button highlighted.
The themes list, with the preview button highlighted.
The preview of the themed login page.
The preview of the themed login page.

When the application also uses React, you can copy the markup of the application's components into the templates. For more information, see Match FusionAuth Themes to a React Application.

Add DaisyUI#

DaisyUI is a Tailwind CSS plugin with prebuilt components, color themes, light and dark modes, and right-to-left support. DaisyUI 5 requires Tailwind CSS 4. With Tailwind CSS 3, use DaisyUI 2, which the FusionAuth example DaisyUI theme uses.

The example theme sets up light and dark modes, and includes templates for the following pages:

  • Log in
  • Log out
  • Registration
  • Forgot password
  1. Install the plugin:

    $ npm install daisyui
    
  2. Add the plugin to input.css:

    @import "tailwindcss";
    @source "./tpl";
    @plugin "daisyui";
    

To use the color scheme of the example theme, set the themes in input.css. --default sets the light theme, and --prefersdark sets the theme for browsers in dark mode. The daisyui/theme block changes the primary color of the business theme, and the @theme block adds the progress bar animation that the example templates use:

@import "tailwindcss";
@source "./tpl";

@plugin "daisyui" {
  themes: corporate --default, business --prefersdark;
}

@plugin "daisyui/theme" {
  name: "business";
  prefersdark: true;
  --color-primary: #c891f2;
}

@theme {
  --animate-progressBar: progressBar 10s ease-in 1;
  @keyframes progressBar {
    0% { width: 0%; }
    100% { width: 100%; }
  }
}
  1. Install the plugin:

    $ npm install daisyui@2
    
  2. Add the plugin to tailwind.config.js:

    module.exports = {
      content: ['./tpl/*.ftl'],
      theme: {
        extend: {}
      },
      plugins: [
        require('daisyui')
      ]
    }
    

To change the color scheme of the example theme, edit daisyui.themes and daisyui.darkTheme in tailwind.config.js:

21 hidden lines/** @type {import('tailwindcss').Config} */module.exports = {  content: ['./tpl/*.ftl'],  theme: {    extend: {        keyframes: {            progressBar: {                "0%": {                    width: "0%"                },                "100%": {                    width: "100%"                }            }        },        animation: {            progressBar: "progressBar 10s ease-in 1"        }    }  },  plugins: [require("daisyui")],  daisyui: {      themes: [          'corporate',          {            business: {                ...require("daisyui/src/colors/themes")["[data-theme=business]"],                'primary': '#c891f2'            }          }      ],      darkTheme: 'business'  }}

Use DaisyUI components in the templates:

<div class="bg-gray-200">
  <h1 class="text-2xl">Hello World</h1>
  <div class="p-4">
    <button class="btn btn-primary">Primary</button>
  </div>
</div>

To create a color theme, use the DaisyUI Theme Generator.