news 2026/4/16 15:44:05

在 Vue 3 项目中使用 Tailwind CSS

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
在 Vue 3 项目中使用 Tailwind CSS

本文详细介绍了在Vue3项目中集成TailwindCSS的完整流程:

  1. 通过Vite创建Vue3项目;
  2. 安装TailwindCSS及相关依赖;
  3. 配置tailwind.config.js和样式文件;
  4. 在组件中使用实用类实现响应式布局、暗色模式等功能;
  5. 推荐安装常用插件优化表单、排版等组件;
  6. 提供性能优化建议和TypeScript支持方案。

文章包含具体配置示例和常见问题解决方案,突出TailwindCSS在Vue3项目中的优势:快速开发、响应式设计简便、减少自定义CSS等,为开发者提供完整的集成指南。


在 Vue 3 项目中使用 Tailwind CSS


1. 创建 Vue 3 项目(如果尚未创建)

# 使用 Vite 创建 Vue 3 项目 npm create vue@latest # 或 npm init vue@latest # 选择需要的特性,TypeScript、Router、Pinia 等按需选择

2. 安装 Tailwind CSS

# 进入项目目录 cd your-project # 安装 Tailwind 及相关依赖 npm install -D tailwindcss postcss autoprefixer # 初始化 Tailwind 配置 npx tailwindcss init -p

3. 配置 Tailwind CSS

tailwind.config.js

/** @type {import('tailwindcss').Config} */ export default { content: [ "./index.html", "./src/**/*.{vue,js,ts,jsx,tsx}", ], theme: { extend: { // 自定义主题扩展 colors: { primary: '#3490dc', secondary: '#ffed4a', danger: '#e3342f', } }, }, plugins: [], }

创建 src/style.css

@tailwind base; @tailwind components; @tailwind utilities; /* 自定义样式可以写在这里 */

4. 在 main.js/ts 中引入样式

import { createApp } from 'vue' import App from './App.vue' import './style.css' // 引入 Tailwind createApp(App).mount('#app')

5. 基本使用示例

App.vue

<template> <div class="min-h-screen bg-gray-100"> <!-- 响应式设计 --> <div class="container mx-auto px-4 py-8"> <!-- 文字样式 --> <h1 class="text-4xl font-bold text-center text-primary mb-8"> Vue 3 + Tailwind CSS </h1> <!-- 网格系统 --> <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6"> <div v-for="item in items" :key="item.id" class="bg-white rounded-lg shadow-md p-6 hover:shadow-lg transition-shadow" > <h2 class="text-xl font-semibold text-gray-800 mb-2"> {{ item.title }} </h2> <p class="text-gray-600"> {{ item.description }} </p> <button @click="handleClick(item.id)" class="mt-4 px-4 py-2 bg-primary text-white rounded hover:bg-blue-600 transition-colors" > 点击按钮 </button> </div> </div> <!-- Flex 布局 --> <div class="flex flex-col sm:flex-row gap-4 mt-8 items-center justify-center"> <button class="px-6 py-2 bg-green-500 text-white rounded"> 主要操作 </button> <button class="px-6 py-2 bg-gray-300 text-gray-700 rounded"> 次要操作 </button> </div> </div> </div> </template> <script setup> import { ref } from 'vue' const items = ref([ { id: 1, title: '卡片1', description: 'Tailwind CSS 实用工具类' }, { id: 2, title: '卡片2', description: '响应式设计示例' }, { id: 3, title: '卡片3', description: 'Vue 3 组合式 API' }, ]) const handleClick = (id) => { alert(`点击了卡片 ${id}`) } </script>

6. 常用实用技巧

提取组件类(使用 @apply)

/* style.css 中 */ .btn-primary { @apply px-4 py-2 bg-blue-500 text-white rounded hover:bg-blue-600 transition-colors; } .card { @apply bg-white rounded-lg shadow-md p-6; }

响应式设计

vue

<div class="text-sm md:text-base lg:text-lg"> 响应式文字大小 </div> <div class="w-full md:w-1/2 lg:w-1/3"> 响应式宽度 </div>

暗色模式

tailwind.config.js

module.exports = { darkMode: 'class', // 或 'media' // ... 其他配置 }

使用:

vue

<div class="bg-white dark:bg-gray-800 text-gray-900 dark:text-white"> 暗色模式支持 </div> <!-- 切换暗色模式 --> <button @click="toggleDark"> 切换模式 </button>

7. 推荐插件

bash

npm install -D @tailwindcss/forms @tailwindcss/typography @tailwindcss/aspect-ratio

tailwind.config.js

module.exports = { plugins: [ require('@tailwindcss/forms'), require('@tailwindcss/typography'), require('@tailwindcss/aspect-ratio'), ], }

8. 性能优化

// tailwind.config.js module.exports = { // 生产环境移除未使用的样式 purge: [ './index.html', './src/**/*.{vue,js,ts,jsx,tsx}', ], // 或使用 content(Tailwind v3+) content: [ './index.html', './src/**/*.{vue,js,ts,jsx,tsx}', ], }

9. TypeScript 支持(可选)

安装类型声明:

npm install -D @types/tailwindcss

常见问题

  1. 样式不生效:检查样式文件是否正确引入

  2. HMR 不工作:确保tailwind.config.js中的content配置正确

  3. 自定义颜色不生效:确保在extend中正确配置


优势总结

  • ✅ 快速原型开发

  • ✅ 响应式设计简便

  • ✅ 一致性设计系统

  • ✅ 减少自定义 CSS

  • ✅ 优秀的 Vue 3 集成


这样你就成功在 Vue 3 项目中集成了 Tailwind CSS!

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/16 10:22:13

隐私保护中的深度学习同态加密与代理重加密机制研究【附代码】

✅ 博主简介&#xff1a;擅长数据搜集与处理、建模仿真、程序设计、仿真代码、论文写作与指导&#xff0c;毕业论文、期刊论文经验交流。✅成品或者定制&#xff0c;扫描文章底部微信二维码。&#xff08;1&#xff09;隐私保护图像分类深度学习方案设计深度学习技术在图像识别…

作者头像 李华
网站建设 2026/4/16 8:40:50

HunyuanVideo-Foley应用场景:短视频创作者必备音效神器

HunyuanVideo-Foley应用场景&#xff1a;短视频创作者必备音效神器 1. 引言&#xff1a;短视频时代的音效挑战 在当前内容为王的短视频生态中&#xff0c;优质的视听体验已成为决定用户留存的关键因素。然而&#xff0c;大多数创作者在视频制作过程中仍面临一个长期痛点&…

作者头像 李华
网站建设 2026/4/16 10:21:48

5分钟部署通义千问2.5-7B-Instruct,vLLM框架让AI对话快速落地

5分钟部署通义千问2.5-7B-Instruct&#xff0c;vLLM框架让AI对话快速落地 1. 引言 在当前大模型应用快速落地的背景下&#xff0c;如何高效部署一个性能强大、响应迅速且支持商用的语言模型成为开发者关注的核心问题。通义千问2.5-7B-Instruct作为阿里于2024年9月发布的中等体…

作者头像 李华
网站建设 2026/4/16 10:21:59

VibeVoice-TTS语音标注:数据预处理最佳实践

VibeVoice-TTS语音标注&#xff1a;数据预处理最佳实践 1. 引言&#xff1a;VibeVoice-TTS与Web UI的工程价值 随着多说话人长文本语音合成需求的增长&#xff0c;传统TTS系统在对话连贯性、角色区分度和长序列稳定性方面的局限日益凸显。微软推出的VibeVoice-TTS框架&#x…

作者头像 李华
网站建设 2026/4/16 10:20:20

Keil5芯片包下载错误代码分析与处理实例

Keil5芯片包下载失败&#xff1f;这些错误代码你必须懂&#xff01;在嵌入式开发的日常中&#xff0c;搭建一个稳定可靠的开发环境往往是项目启动的第一步。而当你满怀期待地打开Keil Vision&#xff0c;准备新建一个基于新MCU的工程时&#xff0c;却被告知“Device not found”…

作者头像 李华
网站建设 2026/4/16 10:19:13

小白也能懂:AI智能文档扫描仪从安装到使用的完整指南

小白也能懂&#xff1a;AI智能文档扫描仪从安装到使用的完整指南 1. 引言 在日常办公、学习或合同处理中&#xff0c;我们经常需要将纸质文件快速转化为电子版。传统方式依赖专业扫描仪或手动拍照修图&#xff0c;效率低且效果差。而市面上主流的“全能扫描王”类应用虽然功能…

作者头像 李华