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>
相关推荐
Mr Xu_5 分钟前
Vue 3 中 watch 的使用详解:监听响应式数据变化的利器
前端·javascript·vue.js
一 乐1 小时前
校园二手交易|基于springboot + vue校园二手交易系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·后端
科技D人生1 小时前
Vue.js 学习总结(20)—— Vue-Office 实战:word、pdf、excel、ppt 多种文档的在线预览
vue.js·word·vue-pdf·stylesheet·docx-preview·vue-office
vx1_Biye_Design1 小时前
基于Spring Boot+Vue的学生管理系统设计与实现-计算机毕业设计源码46223
java·vue.js·spring boot·spring·eclipse·tomcat·maven
vx_Biye_Design1 小时前
基于Spring Boot+vue的湖北旅游景点门票预约平台的设计--毕设附源码29593
java·vue.js·spring boot·spring cloud·servlet·eclipse·课程设计
hedley(●'◡'●)1 小时前
基于cesium和vue的大疆司空模仿程序
前端·javascript·vue.js·python·typescript·无人机
qq5_8115175151 小时前
web城乡居民基本医疗信息管理系统信息管理系统源码-SpringBoot后端+Vue前端+MySQL【可直接运行】
前端·vue.js·spring boot
百思可瑞教育1 小时前
构建自己的Vue UI组件库:从设计到发布
前端·javascript·vue.js·ui·百思可瑞教育·北京百思教育
百锦再1 小时前
Vue高阶知识:利用 defineModel 特性开发搜索组件组合
前端·vue.js·学习·flutter·typescript·前端框架
hdsoft_huge1 小时前
1panel面板中部署SpringBoot和Vue前后端分离系统 【图文教程】
vue.js·spring boot·后端