GrahamMorbyDev's avatar

Laravel with tailwind not loading styles

Hey guys

So I'm trying to get tailwind in a new Laravel app and I'm either lost or it's not working.

resources/app/app.css

@import "tailwindcss/base";

@import "tailwindcss/components";

@import "tailwindcss/utilities";

My tailwind config is as follows:

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

and postcss config

module.exports = {
  plugins: {
    tailwindcss: {},
    autoprefixer: {},
  },
}

Then i load that in to my resources/js/app.js file

import './bootstrap';
import '../css/app.css';

So I then run npm run dev and I have tried npm run build, which then does nothing and my styles are not loading.

My vite file is:

import { defineConfig } from 'vite';
import laravel from 'laravel-vite-plugin';

export default defineConfig({
    plugins: [
        laravel({
            input: [
                'resources/js/app.js',
            ],
            refresh: true,
        }),
    ],
});

How im using in blade

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Artists</title>
    @vite('resources/js/app.js')
</head>
<body>
<h1 class="text-3xl font-bold underline">
    Hello world!
</h1>
</body>
</html>

Im using the latest version of Laravel, so any help would be wonderful

0 likes
28 replies
Talinon's avatar

@grahammorbydev

I've not used Tailwind with Vite, but looking at the framework specific instructions, they look to defer slightly from the Laravel instructions.

https://tailwindcss.com/docs/guides/vite#vue

Specifically this section:

content: [
    "./index.html",
    "./src/**/*.{vue,js,ts,jsx,tsx}",
  ],

These might be needed for Vite transpilation

thinkverse's avatar

@Talinon as is the guide, there are two guides for Laravel, one with Vite which is the default, and one for Laravel Mix.

1 like
Sinnbeck's avatar

Open devtools with f12 and see if there are errors

thinkverse's avatar

I see you wrote that app.css is under resources/app/app.css but in your app.js you are using ../css/app.css, is that a mistype in the post or in the app.js file?

How are you using app.js in your views? With or without the @vite directive? Just tried using Tailwind CSS like you are trying to and I got it to work and cannot replicate your issue.

Could you perhaps upload a reproduction?

thinkverse's avatar

@GrahamMorbyDev are you getting any errors in the console or anything when you run npm run dev or npm run build? Is your node version compatible with Vite? Vite requires node versions compatible with ^14.18.0 or >=16.0.0.

GrahamMorbyDev's avatar

Right hang on a second

So loading vite and then using sail doesnt work

when I load via localhost it works ??

thinkverse's avatar

@GrahamMorbyDev haven't used Sail myself but does your docker-compose match the stub? Also, did you run npm run or ./vendor/bin/sail npm run? Could be you're starting the Vite server outside of Sail and that's the issue?

t9dev's avatar

It should be

        @vite('resources/css/app.css', 'resources/js/app.js')

thinkverse's avatar

@t9dev you can do it both ways, you don't necessarily need the CSS file, it can be loaded via JavaScript as well. And if you need more inputs you should pass them in as an array not as separate string arguments.

@vite(['resources/css/app.css', 'resources/js/app.js'])

The second argument is the path to the build directory, so if you've changed your output directory from the default build that's where you want to add it.

As it is now your suggestion will generate an exception.

thinkverse's avatar

@t9dev Laravel's Vite directive has never allowed more than one input unless they're in an array, it has been like that since it was introduced. Either you have been using another Vite plugin, like Innocenzi's plugin or you added them as an array and forgot. Because adding two or more inputs as string arguments will throw an exception since the second parameter is an optional build directive.

thinkverse's avatar

@t9dev in your package.json, Laravel's package is laravel-vite-plugin, Innocenzi package is vite-plugin-laravel or the previous and now deprecated laravel-vite.

thinkverse's avatar

@t9dev strange, if I try your way, adding them as separate arguments, I get thrown an exception as predicted.

Vite manifest not found at: /Users/thinkverse/Sites/tailwind/public/resources/js/app.js/manifest.json

Weird it doesn't throw an exception for you.

t9dev's avatar

Also do not forget the VITE file

input: ['resources/css/app.css', 'resources/js/app.js'],
Matty10209's avatar

I was having an issue when using Herd, where tailwind was working, but some blade files had no tailwind styles applied. Using PHP artisan serve and localhost, instead of Herd vanity domain worked.

1 like

Please or to participate in this conversation.