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>
相关推荐
触底反弹9 小时前
一文搞懂 Tailwind CSS 弹性布局:从原理到实战
前端·css·html
小白巨白13 小时前
玫瑰花园管理系统:AI识病+3D可视化,一套面向中小型玫瑰种植园的数字化管理工具
css·人工智能·计算机视觉·html5
慧一居士14 小时前
Element Plus 按需引入的配置使用说明和完整示例
前端·vue.js
CoderWeen18 小时前
我写了个能一步步点着看的 Dijkstra 可视化项目(Vue3 + Leaflet + Generator)
前端·javascript·vue.js
无人生还19 小时前
第 1 篇:从 Vue3 到 React 的思维转变
前端·vue.js·react.js
就业发动机19 小时前
我用 Tauri 做了一个约 1.5 MB 的开源屏幕标注工具:MarkerOn
vue.js·rust
爱分享的程序猿-Clark19 小时前
【前端分享】vue开发项目,如何实现 从A页面跳转到B页面,并传值!
前端·javascript·vue.js
徐小夕20 小时前
花了3天,我写了一款开源AI公众号编辑器
前端·vue.js·github
come1123420 小时前
Vue 2 与 Vue 3 语法区别及使用技巧
前端·javascript·vue.js
腻害兔21 小时前
【若依项目-产品经理视角】RuoYi-Vue-Pro 源码拆解:ERP 企业资源模块,一个轻量级进销存的完整实现?
前端·javascript·vue.js·人工智能·前端框架·产品经理·ai编程