DEV Community

Cover image for Setting up TailwindCSS with SASS in Laravel
Catherine Chen
Catherine Chen

Posted on β€’ Edited on

11 4

Setting up TailwindCSS with SASS in Laravel

Start

Before beginning, make sure you have Composer and npm installed.

First, create a Laravel project.

Install dependencies

Next, install Laravel's npm dependencies by running the following commands:

npm install
npm install tailwindcss
Enter fullscreen mode Exit fullscreen mode

Sass comes with Laravel, so you don't need to install it manually.

tailwind.config.js

You may want to create a tailwind.config.js file by running:

npx tailwindcss init
Enter fullscreen mode Exit fullscreen mode

in your root folder.

webpack.mix.js

Inside webpack.mix.js, require tailwindcss:

const tailwindcss = require('tailwindcss');
Enter fullscreen mode Exit fullscreen mode

Update your code to read as such:

mix.sass('resources/sass/app.scss', 'public/css')
    .options({
        processCssUrls: false,
        postCss: [ tailwindcss('./tailwind.config.js') ],
    })
Enter fullscreen mode Exit fullscreen mode

where app.scss is the name of your main SCSS file.

For each extra SCSS file you have, add another snippet of code. Example:

mix.sass('resources/sass/app.scss', 'public/css')
    .options({
        processCssUrls: false,
        postCss: [ tailwindcss('./tailwind.config.js') ],
    })
    .sass('resources/sass/app2.scss', 'public/css')
    .options({
        processCssUrls: false,
        postCss: [ tailwindcss('./tailwind.config.js') ],
    })
Enter fullscreen mode Exit fullscreen mode

Previewing Changes

To preview changes to your SCSS files as soon as you make them, run npm run watch as you edit. Every time a SCSS file listed in webpack.config.js is updated, your files will be built again. Make sure to disable caching in your browser first, or the newly built files will not be loaded, and the browser will continue to use your old files.


That's it! Now you're all set. πŸ˜„

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

Top comments (6)

Collapse
 
petsto profile image
Petar Stoyanov β€’

This is awesome! Thanks for adding this guide. Helped tremendously with a setup I wanted. Legend πŸš€πŸš€πŸš€

Collapse
 
ahmedhelalahmed profile image
Ahmed Helal β€’

This part is missing in tailwind.config.js

module.exports = {
  content: [
    "./resources/**/*.blade.php",
    "./resources/**/*.js",
    "./resources/**/*.vue",
  ],
  theme: {
    extend: {},
  },
  plugins: [],
}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
galagargh profile image
Galagargh β€’

Was tearing my hair out wondering why my cols were not working, thanks

Collapse
 
cristiano profile image
cristiano β€’

This guide was super helpful and easy to follow, thanks so much! πŸ™

Collapse
 
shanecarmody profile image
… β€’

Thanks for the article.

Why do you add an options block for each .sass declaration? Doing like below does the same thing. There doesn’t seem to be any performance impact.

mix.sass('resources/sass/app.scss', 'public/css')
.sass('resources/sass/app2.scss', 'public/css')
.options({
processCssUrls: false,
postCss: [ tailwindcss('./tailwind.config.js') ],
})

Collapse
 
peterangelov profile image
Peter Angelov β€’

Thank you πŸ’š

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

πŸ‘‹ Kindness is contagious

Please leave a ❀️ or a friendly comment on this post if you found it helpful!

Okay