/lovable-issues

Ensuring TailwindCSS Classes Apply Properly in Lovable

Discover why Tailwind utility classes may fail in lovable projects and learn best practices for using TailwindCSS effectively.

Matt Graham, CEO of Rapid Developers

Book a call with an Expert

Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.

Book a free No-Code consultation

Why Tailwind Utility Classes May Fail in Lovable Projects

 
Understanding the Problem
 

  • Tailwind CSS uses utility classes that are very small and very specific. These classes are combined to style your webpages quickly.
  • Sometimes when developers work on projects full of personality and love, known as "lovable projects," these utility classes might not perform as expected.
  • This can lead to areas of the website not looking right or behaving unexpectedly.

 
Common Reasons for Failure
 

  • One reason can be that some tools and settings that manage the website’s styling might remove classes it does not see in use. For example, the build process might delete what looks like unused code to keep things neat, but sometimes it mistakenly removes utility classes that are only added dynamically.
    
        // Example configuration where classes might be removed unintentionally
        module.exports = {
          purge: ['./src/\*_/_.html', './src/\*_/_.js'],
          theme: {
            extend: {},
          },
          variants: {},
          plugins: [],
        }
        
  • Another reason is that the project might be using multiple tools to process the code. If these tools are not set up to understand Tailwind CSS, some of the utility classes might not work as expected because they get lost in the mix.
  • Sometimes, the special magic of Tailwind classes can fail if the configuration file is not exactly right. A small mistake in the settings file may prevent a whole range of styles from being applied.
    
        // A snippet representing a misconfigured Tailwind setup
        module.exports = {
          content: ['./index.html', './components/\*_/_.{js,jsx}'], // This might miss certain files
          theme: {
            colors: {
              blue: '#1fb6ff',
              pink: '#ff49db',
            }
          }
        }
        
  • Sometimes, integrating Tailwind with other CSS libraries or styles makes it hard for the system to decide which style comes first, causing the utility classes to "disappear" or be overridden by other rules.

 
Why It Even Matters
 

  • When these utility classes fail, it can change the feel and presentation of a project many people love. It means the intended design may not show up, resulting in parts of the site looking odd or not aligned with the vision.
  • The issue is not just about code; it’s about delivering the right experience. In a project that users care about, every little detail counts, and when something unexpected happens, it can feel like the heart of the project is off.

 
The Role of Configuration and Tools
 

  • Tailwind CSS relies on a configuration file and build tools to work efficiently. If even one tiny part of the configuration is incorrect or if the build tools are not in sync, the utility classes might fail to apply the intended styles.
  • This is similar to following a recipe. If you use the wrong ingredient or miss a step, the final dish might not taste right. In the same way, if the setup for Tailwind CSS is not done properly, the results in your project won’t match your vision.
  • The code snippets above provide examples of configurations that might be the source of the problem, showing where the settings might lead to unexpected removal of classes or misinterpretation by the build process.
    
        // A simplified example where the source files might be mis-specified
        module.exports = {
          content: ['./public/\*_/_.html'], // This may ignore dynamic components
          theme: {},
          plugins: [],
        }
        

How to Use TailwindCSS Properly in Lovable

 
Setting Up Tailwind CSS via CDN in Your Main HTML File
 

  • Open your main HTML file (for example, index.html) in the Lovable code editor.
  • In the <head> section of your HTML file, add the TailwindCSS CDN link. This way you don’t need to install any dependencies through a terminal. Paste the following code inside your <head>:
    
    <!DOCTYPE html>
    <html lang="en">
      <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>My Lovable App</title>
        <script src="https://cdn.tailwindcss.com"></script>
      </head>
      <body>
        <h1 class="text-3xl font-bold underline">Hello Tailwind!</h1>
      </body>
    </html>
        
  • Save the file. Your Lovable app is now using TailwindCSS for styling.

 
Customizing Tailwind Configuration Directly in Your HTML
 

  • Since Lovable does not provide a terminal, you can customize Tailwind’s settings in your HTML file itself.
  • Right after including the CDN script and still in the <head> section, add a configuration script. Paste this code below the CDN script:
    
    <script>
      tailwind.config = {
        theme: {
          extend: {
            colors: {
              customBlue: '#1e40af'
            }
          }
        }
      }
    </script>
        
  • This snippet tells Tailwind to add a new color called customBlue that you can use in your classes.

 
Creating a Custom CSS File with Tailwind Directives (Optional Advanced Step)
 

  • If you want to add more structured custom styles, create a new file named style.css in the root folder of your Lovable project.
  • Add the Tailwind directives into this file. These directives pull in Tailwind’s base styles, components, and utilities. Paste the following into style.css:
    
    @tailwind base;
    @tailwind components;
    @tailwind utilities;
        
  • Then, in your index.html file, link to this CSS file by adding the following line inside the <head> (below your Tailwind configuration script):
    
    <link rel="stylesheet" href="style.css">
        
  • Note: Since Lovable does not have a terminal to run build commands, these @tailwind directives might not be processed automatically. The CDN integration (from the first step) is fully functional, so using it is recommended for simple projects.

 
Using Tailwind Classes in Your Markup
 

  • Now that TailwindCSS is set up, use its classes directly in your HTML elements. For example, modify a button as follows to apply Tailwind’s utility classes:
    
    <button class="bg-customBlue text-white font-semibold py-2 px-4 rounded">
      Click Me
    </button>
        
  • Experiment with different Tailwind classes to style your elements. You can refer to the TailwindCSS documentation online for more ideas.

Want to explore opportunities to work with us?

Connect with our team to unlock the full potential of no-code solutions with a no-commitment consultation!

Book a Free Consultation

Best Practices for Using Tailwind in Lovable Projects

 
Integrating Tailwind via CDN
 

  • Open your main HTML file (for example, index.html) in the Lovable code editor.
  • Inside the <head> section of your HTML file, add the Tailwind CDN script. This lets you use Tailwind’s styles without needing a terminal installation. Place the following code right before the closing </head> tag:
    
    <script src="https://cdn.tailwindcss.com"></script>
        
  • This single script tag handles loading Tailwind’s styles into your project. It is a quick setup for using Tailwind directly in Lovable.

 
Creating Your Custom CSS File for Tailwind Directives
 

  • If you plan to add custom CSS or extend Tailwind’s defaults, create a new file named tailwind.css in your project’s styles folder.
  • In this file, include the Tailwind directives to import base styles, components, and utilities. Paste the following lines into tailwind.css:
    
    @tailwind base;
    @tailwind components;
    @tailwind utilities;
        
  • Then, open your HTML file (index.html in our example) and link to this stylesheet within the <head> section after the Tailwind CDN script:
    
    <link rel="stylesheet" href="styles/tailwind.css">
        
  • This approach helps you separate custom styles from the default Tailwind setup.

 
Implementing Inline Tailwind Configuration
 

  • Sometimes, you might want to customize Tailwind’s default behavior. With Lovable not having a terminal, you can add an inline configuration directly in your HTML file.
  • Right after loading the Tailwind CDN script in the <head> of your index.html file, include a script block with your configuration settings. For example, to extend the color palette:
    
    <script>
      tailwind.config = {
        theme: {
          extend: {
            colors: {
              customBlue: '#1fb6ff',
              customGreen: '#13ce66'
            }
          }
        }
      }
    </script>
        
  • This inline configuration is processed by the CDN, allowing you to use bg-customBlue or text-customGreen classes in your HTML.

 
Organizing Your HTML Markup with Tailwind Utility Classes
 

  • Tailwind encourages using utility classes directly in your HTML. When creating layouts or components, apply classes to style elements without writing separate CSS.
  • For example, to build a centered button with Tailwind utilities, add this code snippet in your HTML where you want the button to appear:
    
    <button class="bg-customBlue hover:bg-blue-700 text-white font-bold py-2 px-4 rounded">
      Click Me
    </button>
        
  • This practice improves consistency and speed when styling elements, keeping your code clear and maintainable.

 
Using Component Extraction for Reusable UI Blocks
 

  • For larger projects, it is best to extract repeated UI elements into separate files (or components) if your environment supports it. For instance, if you have a recurring navigation bar, create a new file named navbar.html in a components folder.
  • Place your navigation bar HTML code inside navbar.html. An example snippet could be:
    
    <nav class="flex items-center justify-between p-6 bg-gray-800">
      <div class="text-white font-bold">My Project</div>
      <div>
        <a href="#" class="text-gray-300 hover:text-white mx-2">Home</a>
        <a href="#" class="text-gray-300 hover:text-white mx-2">About</a>
        <a href="#" class="text-gray-300 hover:text-white mx-2">Contact</a>
      </div>
    </nav>
        
  • Then, include this component in your main HTML file using your project’s templating method or simple copy-and-paste. Organizing repeated code into components makes updates easier and keeps your codebase clean.

Client trust and success are our top priorities

When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.

Rapid Dev was an exceptional project management organization and the best development collaborators I've had the pleasure of working with. They do complex work on extremely fast timelines and effectively manage the testing and pre-launch process to deliver the best possible product. I'm extremely impressed with their execution ability.

CPO, Praction - Arkady Sokolov

May 2, 2023

Working with Matt was comparable to having another co-founder on the team, but without the commitment or cost. He has a strategic mindset and willing to change the scope of the project in real time based on the needs of the client. A true strategic thought partner!

Co-Founder, Arc - Donald Muir

Dec 27, 2022

Rapid Dev are 10/10, excellent communicators - the best I've ever encountered in the tech dev space. They always go the extra mile, they genuinely care, they respond quickly, they're flexible, adaptable and their enthusiasm is amazing.

Co-CEO, Grantify - Mat Westergreen-Thorne

Oct 15, 2022

Rapid Dev is an excellent developer for no-code and low-code solutions.
We’ve had great success since launching the platform in November 2023. In a few months, we’ve gained over 1,000 new active users. We’ve also secured several dozen bookings on the platform and seen about 70% new user month-over-month growth since the launch.

Co-Founder, Church Real Estate Marketplace - Emmanuel Brown

May 1, 2024 

Matt’s dedication to executing our vision and his commitment to the project deadline were impressive. 
This was such a specific project, and Matt really delivered. We worked with a really fast turnaround, and he always delivered. The site was a perfect prop for us!

Production Manager, Media Production Company - Samantha Fekete

Sep 23, 2022