Hassan Agmir Hassan Agmir

Electron + Tailwind CSS Setup Guide

Hassan Agmir
Electron + Tailwind CSS Setup Guide

This guide helps you configure Tailwind CSS with Electron.js using PostCSS and live reloading.

๐Ÿ”ง Step 1: Project Structure

electron-project/
โ”œโ”€โ”€ dist/
โ”‚   โ””โ”€โ”€ output.css            # Compiled Tailwind CSS
โ”œโ”€โ”€ src/
โ”‚   โ”œโ”€โ”€ index.html            # Your HTML file
โ”‚   โ””โ”€โ”€ style/
โ”‚       โ””โ”€โ”€ style.css         # Tailwind source CSS
โ”œโ”€โ”€ main.js                   # Electron entry point
โ”œโ”€โ”€ tailwind.config.js        # Tailwind configuration
โ”œโ”€โ”€ postcss.config.js         # PostCSS plugins
โ”œโ”€โ”€ package.json              # Project metadata & scripts

๐Ÿ“ฆ Step 2: Install Required Packages

Run the following command to install dependencies:

npm install electron tailwindcss postcss autoprefixer concurrently css-loader postcss-loader style-loader wait-on --save-dev

๐ŸŽจ Step 3: Tailwind CSS Configuration

tailwind.config.js

module.exports = {
  content: [
    "./src/**/*.{html,js}",
    "./src/index.html"
  ],
  theme: {
    extend: {},
  },
  plugins: [],
};

โš™๏ธ Step 4: PostCSS Configuration

โœ… Fix the typo in your config file. Use module.exports, not module.exportes.

postcss.config.js

module.exports = {
  plugins: [
    require('tailwindcss'),
    require('autoprefixer')
  ]
};

๐Ÿงต Step 5: Create Style Entry File

src/style/style.css

@tailwind base;
@tailwind components;
@tailwind utilities;

๐Ÿ–ฅ Step 6: Electron Entry Point

main.js

const { app, BrowserWindow } = require('electron');
const path = require('path');

function createWindow() {
  const win = new BrowserWindow({
    width: 800,
    height: 600,
  });

  win.loadFile('src/index.html');
}

app.whenReady().then(createWindow);

app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') app.quit();
});

๐ŸŒ Step 7: HTML Setup

src/index.html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Electron + Tailwind</title>
  <link href="../dist/output.css" rel="stylesheet">
</head>
<body class="flex items-center justify-center h-screen bg-gray-100">
  <h1 class="text-4xl font-bold text-blue-600">Hello Electron + Tailwind!</h1>
</body>
</html>

๐Ÿงช Step 8: Scripts for Development

package.json Scripts

"scripts": {
  "build:css": "tailwindcss -i ./src/style/style.css -o ./dist/output.css",
  "watch:css": "tailwindcss -i ./src/style/style.css -o ./dist/output.css --watch",
  "start": "electron .",
  "dev": "concurrently \"npm run watch:css\" \"electron .\""
}

๐Ÿš€ How to Use

๐Ÿ”ง Build Once

npm run build:css
npm start

๐Ÿงช Start Dev Mode with Live Tailwind Watch

npm run dev

This will:

  • Watch Tailwind CSS changes
  • Start the Electron app
  • Automatically recompile output.css on file save

โœ… Final Notes

  • โš  The tailwindcss version is important, so always use the LTS one.
  • You must import the output.css into your HTML.
  • Make sure dist/output.css is in the correct relative path from index.html.
  • Use wait-on for more advanced coordination if needed (like waiting for server start).
Subscribe to my Newsletters

Stay updated with the latest programming tips, tricks, and IT insights! Join my community to receive exclusive content on coding best practices.

ยฉ Copyright 2025 by Hassan Agmir . Built with โค by Me