Eleventy starters that bundle Tailwind CSS are plentiful in the official Starter Projects list, but many of them wire the two tools together in ways that feel contrived, and several assume nobody edits Tailwind's configuration during development. The setup below takes the opposite approach: Eleventy for structure, Nunjucks for templates, Tailwind CSS for styles, and Alpine.js for scripts, with as little machinery as possible. Some prior familiarity with Tailwind CSS, HTML, JavaScript, Nunjucks, the command line and npm is assumed.

Project setup and a first page

Create a folder, cd into it, and initialize a package.json:

npm init -y

Install Eleventy and Tailwind CSS:

npm install -D @11ty/eleventy tailwindcss@latest

To verify the install, create index.njk as the homepage. Because real pages will rely on templates, this one does too, with Nunjucks serving as the templating engine:

{% extends "_includes/default.njk" %}


{% block title %}It does work{% endblock %}


{% block content %}
  <div class="fixed inset-0 flex justify-center items-center">
    <div>
      <span class="text-change">Good design</span><br/>
      <span class="change">is<br/>as little design<br/>as possible</span>
    </div>
  </div>
{% endblock %}

Layouts with Nunjucks

Add a folder named exactly _includes and place default.njk inside it as the default layout template, containing a basic HTML boilerplate:

<!DOCTYPE html>
<html lang="en">
  <head>
    <title>
      {% block title %}Does it work?{% endblock %}
    </title>
    <meta charset="UTF-8"/>
    {% if description %}
      <meta name="description" content="{{description}}"/>
    {% endif %}
    <meta http-equiv="x-ua-compatible" content="ie=edge"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no, viewport-fit=cover"/>
    <link rel="stylesheet" href="https://css-tricks.com/style.css?v={% version %}"/>
    {% block head %}{% endblock %}
  </head>
  <body>
    {% block content %}
      {{ content | safe }}
    {% endblock %}
  </body>
</html>

Tailwind configuration

Create a styles subfolder containing tailwind.config.js:

module.exports = {
  content: ['_site/**/*.html'],
  safelist: [],
  theme: {
    extend: {
      colors: {
        change: 'transparent',
      },
    },
  },
  plugins: [],
}

Then add tailwind.css to that same folder:

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

@layer utilities {
  .change {
    color: transparent;
  }
}

Ignoring files

In the project root, add a .gitignore that lists which files to skip when committing to a repository such as GitHub:

_site/
.DS_Store
node_modules/

Configuring Eleventy

Create .eleventy.js with a leading dot to tell Eleventy which files to watch and where to write output:

const now = String(Date.now())

module.exports = function (eleventyConfig) {
  eleventyConfig.addWatchTarget('./styles/tailwind.config.js')
  eleventyConfig.addWatchTarget('./styles/tailwind.css')


  eleventyConfig.addPassthroughCopy({ './_tmp/style.css': './style.css' })


  eleventyConfig.addShortcode('version', function () {
    return now
  })
};

Then update package.json with the scripts needed to start and build the site. The dependencies are already present from the initial install:

{
  "scripts": {
    "start": "eleventy --serve & npx tailwindcss -i styles/tailwind.css -c styles/tailwind.config.js -o _site/style.css --watch",
    "build": "ELEVENTY_PRODUCTION=true eleventy && NODE_ENV=production npx tailwindcss -i styles/tailwind.css -c styles/tailwind.config.js -o _site/style.css --minify"
  },
  "devDependencies": {
    "@11ty/eleventy": "^1.0.0",
    "tailwindcss": "^3.0.0"
  }
}

Launch the site:

npm start

Opening http://localhost:8080 produces a visually unremarkable page, so check the browser tab's title:

It does work!

Verifying live reload

  • Edit /styles/tailwind.config.js and change the value of transparent to black. Tailwind's configuration should reload along with the page in the browser.
  • Keep watching the browser and change transparent to black in /styles/tailwind.css. The CSS file should reload and refresh in the browser.

Both sides of the pipeline now reload on save.

Minifying the build output

The generated HTML still carries redundant newlines. Install the tooling:

npm install -D html-minifier

Add this line at the top of .eleventy.js:

const htmlmin = require('html-minifier')

And configure htmlmin within that file:

eleventyConfig.addTransform('htmlmin', function (content, outputPath) {
    if (
      process.env.ELEVENTY_PRODUCTION &&
      outputPath &&
      outputPath.endsWith('.html')
    ) {
      let minified = htmlmin.minify(content, {
        useShortDoctype: true,
        removeComments: true,
        collapseWhitespace: true,
      });
      return minified
    }


    return content
})

This relies on an Eleventy transform, which can modify a template's output. The complete .eleventy.js at this point:

const htmlmin = require('html-minifier')

const now = String(Date.now())

module.exports = function (eleventyConfig) {
  eleventyConfig.addWatchTarget('./styles/tailwind.config.js')
  eleventyConfig.addWatchTarget('./styles/tailwind.css')

  eleventyConfig.addShortcode('version', function () {
    return now
  })

  eleventyConfig.addTransform('htmlmin', function (content, outputPath) {
    if (
      process.env.ELEVENTY_PRODUCTION &&
      outputPath &&
      outputPath.endsWith('.html')
    ) {
      let minified = htmlmin.minify(content, {
        useShortDoctype: true,
        removeComments: true,
        collapseWhitespace: true,
      })
      return minified
    }

    return content
  })
}

Running npm start again will show no change, because the optimization happens only during a build. Run npm run build and inspect the _site folder: index.html should contain no unnecessary characters, and style.css should be equally clean. The project is ready to deploy at this point.

Adding Alpine.js for scripts

Alpine.js suits a measured amount of vanilla JavaScript better than a heavyweight bundle. There is a primer by Hugo DiFrancesco worth reading as a starting point. Install from the command line:

npm install -D alpinejs

Update the function that passes assets through Alpine.js in .eleventy.js:

eleventyConfig.addPassthroughCopy({
  './node_modules/alpinejs/dist/cdn.js': './js/alpine.js',
})

Then add Alpine.js in _includes/default.njk, right before the closing </head> tag:

<script src="https://css-tricks.com/js/alpine.js?v={% version %}"></script>

To test it, add the following to index.njk:

{% extends "_includes/default.njk" %}


{% block title %}It does work{% endblock %}


{% block content %}
  <div class="fixed inset-0 flex justify-center items-center">
    <div>
      <span class="text-change">Good design</span><br/>
      <span class="change">is<br/>as little design<br/>as possible</span><br/>
      <span x-data="{message:'🤖 Hello World 🤓'}" x-text="message"></span>
    </div>
  </div>
{% endblock %}

Launch the project:

npm start

A working install displays "Hello World" in the browser.