TailWindCss v4.1在Vue3里的使用

tailwindcss v4比v3方便了不少,因此基本的安装配置和使用也不同,我写下我的学习笔记于此

安装及其配置

安装

powershell 复制代码
npm install tailwindcss @tailwindcss/vite

配置

js 复制代码
// vite.config.js
import { defineConfig } from 'vite'
import tailwindcss from '@tailwindcss/vite'
export default defineConfig({
    plugins: [ 
        tailwindcss(),
    ],
})
css 复制代码
/* ./src/style.css */
@import "tailwindcss";

将全局css文件引入index.html中

html 复制代码
<link rel="stylesheet" href="./src/style.css" />

测试现在是否可以使用tailwindcss:

html 复制代码
<!--App.vue-->
<template>
  <div class="flex flex-col min-h-screen ">
    <p class="text-red-600 text-3xl font-bold underline">Hello World</p>
  </div>
</template>

结果:

自定义工具类(utility classes)

Theme variables

我们可以自定义颜色,引入外部字体,可以自定义圆角,不过在定义前需要分类,颜色分为颜色类--color,字体则是--font其他同理可查看官方文档,这里以--color和--font为例。

Color

在全局样式文件里设置

css 复制代码
/*style.css*/
@import "tailwindcss";

@theme {
  --color-caomolihong: #ef475d;
}

在组件项目中使用: 让背景变为草茉莉红

html 复制代码
<!--App.vue-->
<template>
  <div class="bg-caomolihong min-h-screen ">
  </div>
</template>

让字体变为草茉莉红

html 复制代码
<!--App.vue-->
<templete>
  <div>
    <p class="text-red-600 text-3xl font-bold underline">Hello World</p>
  </div>
</templete>

Font

引入goole font

html 复制代码
<!--index.html-->
<link rel="preconnect" href="https://fonts.googleapis.com" />
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
<link
  href="https://fonts.googleapis.com/css2?family=Libertinus+Keyboard&display=swap"
  rel="stylesheet"
/>
css 复制代码
/*style.css*/
@import "tailwindcss";

@theme {
  --color-caomolihong: #ef475d;
  --font-test: "Libertinus Keyboard";
}

使用

html 复制代码
<template>
  <div>
    <p class="font-test">Hello World</p>
  </div>
</template>
相关推荐
ZC跨境爬虫9 小时前
跟着 MDN 学 HTML day_9:(信件语义标记)
前端·css·笔记·ui·html
摇滚侠11 小时前
外边距问题 塌陷问题 HTML CSS
css
前端摸鱼匠11 小时前
Vue 3 的v-bind合并行为:讲解v-bind与普通属性合并的规则
前端·javascript·vue.js·前端框架·ecmascript
W.A委员会12 小时前
CSS中的单位
css·css3·html5
nbwenren13 小时前
2026实测:Gemini 3 镜像站视觉能力实践——拍照原型图,一键生成 HTML+CSS 代码
前端·css·html
之歆16 小时前
Day07_CSS盒子模型 · 样式继承 · 用户代理样式
前端·css
爱上好庆祝19 小时前
学习js的第五天
前端·css·学习·html·css3·js
EnCi Zheng20 小时前
M5-markconv自定义CSS样式指南 [特殊字符]
前端·css·python
前端老石人20 小时前
前端开发中的 URL 完全指南
开发语言·前端·javascript·css·html
用户0595401744621 小时前
把多级缓存一致性验证从手工测试换成 Pytest 参数化,Bug 排查时间缩短 90%
前端·css