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>
相关推荐
A黄俊辉A4 天前
uniapp webview中实现 app和内嵌的H5双向通信
vue.js·json
honkun64 天前
vue 表格组件 vxe-table 配置 ajax 请求自动加载数据与表单查询
vue.js·vxe-table
kybs19914 天前
全球灾害数据分析可视化 毕业设计-附源码66794
vue.js·spring boot·mysql·安全·django·c#·asp.net
雪芽蓝域zzs4 天前
第26章:ECharts 自定义主题 + 页面一键切换主题(深色 / 浅色两套)
vue.js·echars
雪芽蓝域zzs4 天前
第22章:ECharts 图表联动(多图表交互联动)
vue.js·echars
daols884 天前
vue 表格组件 vxe-table 实现虚拟滚动与无需滚动加载
vue.js·vxe-table
雪芽蓝域zzs4 天前
第18章:ECharts 折线图 + 标记点、标记线,自定义 tooltip
vue.js·echars
雪芽蓝域zzs4 天前
第24章:ECharts 地图组件(基础行政区地图,若依 Vue3)
vue.js·echars
计算机毕设定制辅导-无忧学长4 天前
《基于Vue的流浪动物救助中心管理系统的设计与实现》
java·vue.js·spring boot·流浪动物救助中心管理系统
雪芽蓝域zzs4 天前
第25章:地图下钻交互(点击省份,切换到对应省份城市地图)
vue.js·echars