Installation
Setting up Tailwind CSS in an Eleventy project.
Start by creating a new Eleventy project if you don’t have one set up already. The most common approach is outlined in their getting started guide.
mkdir my-projectcd my-projectnpm init -ynpm pkg set type="module"npm install @11ty/eleventymkdir srctouch src/index.html
Install @tailwindcss/vite
and its peer dependencies via npm.
npm install tailwindcss @tailwindcss/vite
Install @11ty/eleventy-plugin-vite
and its peer dependencies via npm.
npm install @11ty/eleventy-plugin-vite
Create src/styles.css
file and add an @import
for Tailwind CSS.
@import "tailwindcss";
Create a eleventy.config.js
file in your project root, and add the @11ty/eleventy-plugin-vite
and @11ty/eleventy-plugin-vite
plugin. Include an addPassthroughCopy
to copy your CSS to the output folder.
import EleventyVitePlugin from "@11ty/eleventy-plugin-vite";import tailwindcss from '@tailwindcss/vite'export default (eleventyConfig) => { eleventyConfig.addPassthroughCopy("src/styles.css"); eleventyConfig.addPlugin(EleventyVitePlugin, { viteOptions: { plugins: [ tailwindcss() ] } });};export const config = { dir: { input: "src", },};
Run your build process with npx eleventy --serve
.
npx eleventy --serve
Add your CSS file to the <head>
and start using Tailwind’s utility classes to style your content.
<!doctype html><html> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <link href="./styles.css" type="text/css" rel="stylesheet" /> </head> <body> <h1 class="text-3xl font-bold underline text-amber-500"> Hello world! </h1> </body></html>