Styling VitePress with Tailwind

VitePress is a Markdown-based static site generator. Its default theme is geared towards docs sites, but it can be used for anything. It’s currently my go-to framework when building documentation for my software projects (git-random’s site is an example).
I like to write CSS via Tailwind (more exactly, I like to write CSS via programming language-configured DOM API, I’m a long-time Tailwind user, and though I’m currently more excited about UnoCSS than Tailwind it will be a while before I’ve ported my Tailwind plugins (for example tailwindcss-fluid-font-size - also a VitePress site)).
Here’s how to combine the two.
Install dependencies
Replace <package manager> with your package manager. Works with at least bun, npm, pnpm, and yarn.
<package manager> add -D autoprefixer postcss tailwindcssInitialize Tailwind
The command depends on your package manager. Here are the four most popular as of this writing. Run only one.
# bun
bunx tailwindcss init
# npm
npx tailwindcss init
# pnpm
pnpx tailwindcss init
# yarn
yarn dlx tailwindcss initConfigure PostCSS
Create the PostCSS config file, and configure it.
/** @type {import('postcss-load-config').Config} */
export default {
plugins: {
tailwindcss: {},
autoprefixer: {},
},
};Configure Tailwind content paths
This sample code assumes VitePress is in the project root. If it’s in a subdirectory, update the paths accordingly.
/** @type {import('tailwindcss').Config} */
export default {
content: ["**/*.md", ".vitepress/theme/*.{js,ts,vue}"],
};Add Tailwind to your custom styles
Create a custom theme, if necessary
Tip
Skip this if your VitePress site already has a custom theme (for example, if you initialized VitePress with vitepress init, and selected either “Default Theme + Customization” or “Custom Theme”).
-
Create a file
.vitepress/theme/index.jsor.vitepress/theme/index.ts. The latest official recommendation as of this writing is [1]TypeScript:
.vitepress/theme/index.tsts// https://vitepress.dev/guide/custom-theme import { h } from "vue"; import type { Theme } from "vitepress"; import DefaultTheme from "vitepress/theme"; import "./style.css"; export default { extends: DefaultTheme, Layout: () => { return h(DefaultTheme.Layout, null, { // https://vitepress.dev/guide/extending-default-theme#layout-slots }); }, enhanceApp({ app, router, siteData }) { // ... }, } satisfies Theme;or JavaScript:
.vitepress/theme/index.jsjs// https://vitepress.dev/guide/custom-theme import { h } from "vue"; import DefaultTheme from "vitepress/theme"; import "./style.css"; /** @type {import('vitepress').Theme} */ export default { extends: DefaultTheme, Layout: () => { return h(DefaultTheme.Layout, null, { // https://vitepress.dev/guide/extending-default-theme#layout-slots }); }, enhanceApp({ app, router, siteData }) { // ... }, }; -
Create the file
.vitepress/theme/style.css.
Add Tailwind directives to the VitePress theme stylesheet
@tailwindcss base;
@tailwindcss components;
@tailwindcss utilities;Write some Tailwind
Now you can use Tailwind in Markdown page source
This is black.
<span class="text-red-400">This is red</span>.and/or in VitePress theme files
<template>Hello <span class="text-red-400">world</span>.</template>Footnotes
Also In This Series
Articles You Might Enjoy
-
-
-
Writing zsh tab completions can be straightforward

How I add tab completion for the zsh command line





