0

I am using React with Vite and Tailwind CSS 10.9.1 (latest version). However, my project does not have a tailwind.config.js file—only a vite.config.js.

I want to set a background image using Tailwind CSS.

mountain.jsx

<div class="bg-[url(/img/mountains.jpg)] ..."></div>

vite.config.js

theme: {
  backgroundImage: {
    'sky': "url('./src/assets/sapfix-bg.png')",
  },
}

sky.jsx

<div classname="bg-sky ..."></div>

I'm expecting answers that suggest alternative ways to set a background image in Tailwind CSS latest version (2025).

2
  • 1
    The latest version of Tailwind CSS as of time of writing is 4.0.8. Could you confirm what Tailwind version you are using please? Commented Feb 21 at 21:33
  • I figured it out: the 10.9.1 version number refers to NPM (what information not relevant for a question). At the end of the question, the OP talks about the latest TailwindCSS version in 2025, possibly v4... but clarification is needed. Commented Feb 22 at 9:40

1 Answer 1

0

Use tailwind.config.js legacy JavaScript-based configuration

The question specifically refers to the removal of tailwind.config.js. From v4 onwards, the CSS-first configuration is the default, but you still have the option to use the legacy JS-based configuration through the @config directive.

The installation process, however, differs between v3 and v4.

TailwindCSS v3

In v3, it's no longer enough to run npm install tailwindcss; you need to install a version-specific package. The init process must also be run, and the 3 @tailwind directive lines must be added to the CSS files.

npm install tailwindcss@3 postcss autoprefixer
npx tailwindcss init -p

tailwind.config.js

/** @type {import('tailwindcss').Config} */
export default {
  content: [
    "./index.html",
    "./src/**/*.{js,ts,jsx,tsx}",
  ],
  theme: {
    extend: {},
  },
  plugins: [],
}

index.css

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

TailwindCSS v4

In v4, there's no need for PostCSS or Autoprefixer; instead, you should use the @tailwindcss/vite package. The init process has been removed since there's no need to create a tailwind.config.js file. Instead of the @tailwind directives, you only need to write @import "tailwindcss"; in the CSS file.

npm install tailwindcss @tailwindcss/vite

vite.config.js

import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import tailwindcss from '@tailwindcss/vite'

// https://vite.dev/config/
export default defineConfig({
  plugins: [
    tailwindcss(),
    react(),
  ],
})

index.css

@import "tailwindcss";

How to set background image?

The real focus of the question is setting the background image, and for this, there's actually no need for tailwind.config.js in either v3 or v4, as it can be done on its own.

<script src="https://unpkg.com/@tailwindcss/browser@4"></script>
<style type="text/tailwindcss">
@layer base {
  .bg-sky {
    background-image: url(https://picsum.photos/300/300);
  }
}
</style>

<div class="bg-[url(https://picsum.photos/200/200)] w-32 h-32"></div>
<div class="bg-sky w-42 h-42"></div>

Or with tailwind.config.js in v3 (or in v4 by @config directive).

// tailwind.config.js
tailwind.config = {
  theme: {
    extend: {
      backgroundImage: {
        sky: 'url(https://picsum.photos/300/300)',
      }
    }
  }
}
<script src="https://cdn.tailwindcss.com"></script>

<div class="bg-[url(https://picsum.photos/200/200)] w-32 h-32"></div>
<div class="bg-sky w-48 h-48"></div>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.