Installation
在 SvelteKit 中安装 Tailwind CSS
在 SvelteKit 项目中设置 Tailwind CSS。

创建你的项目
如果你还没有创建 SvelteKit 项目,可以先新建一个。最常用的方法在 SvelteKit 入门 介绍中有说明。
终端npx sv create my-projectcd my-project安装 Tailwind CSS
安装
tailwindcss及其相关依赖,然后生成你的tailwind.config.js和postcss.config.js文件。终端npm install -D tailwindcss@3 postcss autoprefixernpx tailwindcss init -p在 <style> 块中启用 PostCSS 使用
在你的
svelte.config.js文件中,导入vitePreprocess以启用将<style>块作为 PostCSS 进行处理。svelte.config.jsimport adapter from '@sveltejs/adapter-auto'; import { vitePreprocess } from '@sveltejs/vite-plugin-svelte'; /** @type {import('@sveltejs/kit').Config} */ const config = { kit: { adapter: adapter() }, preprocess: vitePreprocess() }; export default config;配置你的模板路径
在你的
tailwind.config.js文件中添加所有模板文件的路径。tailwind.config.js/** @type {import('tailwindcss').Config} */ export default { content: ['./src/**/*.{html,js,svelte,ts}'], theme: { extend: {} }, plugins: [] };将 Tailwind 指令添加到你的 CSS 中
创建一个
./src/app.css文件,并为 Tailwind 的每一层添加@tailwind指令。app.css@tailwind base; @tailwind components; @tailwind utilities;导入 CSS 文件
创建一个
./src/routes/+layout.svelte文件并导入新创建的app.css文件。+layout.svelte<script> import "../app.css"; </script> <slot />开始构建流程
使用
npm run dev运行你的构建流程。终端npm run dev开始在你的项目中使用 Tailwind
开始使用 Tailwind 的工具类来为你的内容添加样式,并确保为任何需要由 Tailwind 处理的
<style>块设置lang="postcss"。+page.svelte<h1 class="text-3xl font-bold underline"> Hello world! </h1> <style lang="postcss"> :global(html) { background-color: theme(colors.gray.100); } </style>

