tailwindcss @4和@3版本项目引入,及自定义配置

背景

tailwindcss已经升级到@4版本,并且已经成为了默认的版本,较于@3版本引用及自定义配置均有了变化,下面我们就对比下这两个版本的引用不同点

本文已 vue3 vite框架来做试验

vue3 vite tailwindcss@3 版本

初始化项目

js 复制代码
npm create vite@latest  --template vue
cd vue
npm install
npm run dev

安装tailwindcss@3 和 postcss 引入

js 复制代码
npm install -D tailwindcss@3  postcss autoprefixer 
// 初始化引用
npx tailwindcss init -p

以上配置后,会在根目录生成 postcss.config.js tailwind.config.js,src目录下创建index.css tailwind.css

postcss.config.js配置如下

js 复制代码
export default {
  plugins: {
    tailwindcss: {},
    autoprefixer: {},
  },
}

tailwind.config.js配置如下

js 复制代码
/** @type {import('tailwindcss').Config} */
export default {
  content: ["./index.html", "./src/**/*.{vue,js,ts,jsx,tsx}"],
  theme: {
    extend: {},
  },
  plugins: [],
}

tailwind.css配置如下

js 复制代码
/* src/index.css */
@tailwind base;
@tailwind components;
@tailwind utilities;

index.css配置如下

js 复制代码
@import url('tailwind.css');

main.ts引入 index.css

js 复制代码
import { createApp } from 'vue'
import './style.css'
import App from './App.vue'
// 注意此处
import "./index.css"

createApp(App).mount('#app')

app.vue中 写入tailwindcss 类名 即可,可以看见效果 如

js 复制代码
<div class="flex justify-center text-red-400">11111111</div>

自定义tailwindcss配置

tailwind.config.js配置如下

js 复制代码
/** @type {import('tailwindcss').Config} */
module.exports = {
  content: [
    "./index.html",
    "./src/**/*.{vue,js,ts,jsx,tsx}",
  ],
  theme: {
    extend: {
      // 自定义宽度类
      width: {
        ...Array.from({ length: 300 }, (_, i) => i + 1).reduce((acc, num) => {
          acc[num] = `${num}px`;
          return acc;
        }, {})
      },
      // 自定义高度类
      height: {
        ...Array.from({ length: 300 }, (_, i) => i + 1).reduce((acc, num) => {
          acc[num] = `${num}px`;
          return acc;
        }, {})
      },
      // 自定义 padding 类
      padding: {
        ...Array.from({ length: 150 }, (_, i) => i + 1).reduce((acc, num) => {
          acc[num] = `${num}px`;
          return acc;
        }, {})
      },
      // 自定义 margin 类
      margin: {
        ...Array.from({ length: 100 }, (_, i) => i + 1).reduce((acc, num) => {
          acc[num] = `${num}px`;
          return acc;
        }, {})
      },
      // 自定义 font-size 类
      fontSize: {
        ...Array.from({ length: 50 }, (_, i) => i + 1).reduce((acc, num) => {
          acc[num] = `${num}px`;
          return acc;
        }, {})
      }
    },
  },
  plugins: [],
};

以上配置 我们将fontSize margin 等改为了固定宽度,此时安装 postcss-px-to-viewport可以进行适配

js 复制代码
npm install postcss-px-to-viewport

postcss.config.js配置如下

js 复制代码
export default {
  plugins: {
    tailwindcss: {},
    autoprefixer: {},
    "postcss-px-to-viewport": {
      unitToConvert: "px", // 要转化的单位
      viewportWidth: 750, // UI设计稿的宽度
      unitPrecision: 6, // 转换后的精度,即小数点位数
      propList: ["*"], // 指定转换的css属性的单位,*代表全部css属性的单位都进行转换
      viewportUnit: "vw", // 指定需要转换成的视窗单位,默认vw
      fontViewportUnit: "vw", // 指定字体需要转换成的视窗单位,默认vw
      selectorBlackList: [], // 指定不转换为视窗单位的类名,
      minPixelValue: 1, // 默认值1,小于或等于1px则不进行转换
      mediaQuery: true, // 是否在媒体查询的css代码中也进行转换,默认false
      // replace: true, // 是否转换后直接更换属性值
      exclude: [/node_modules/], // 设置忽略文件,用正则做目录名匹配
      landscape: false // 是否处理横屏情况
    }
  },
}

我们写入css类别 ,查看适配已经配置完成

js 复制代码
  <div class="flex justify-center text-red-400 mr-20 w-100">11111111</div>

vue3 vite tailwindcss@4 版本

初始化项目

js 复制代码
npm create vite@latest  --template vue-2
cd vue
npm install
npm run dev

4版本采用插件安装

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

vite.config.ts配置如下

js 复制代码
import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
import tailwindcss from "@tailwindcss/vite";

// https://vite.dev/config/
export default defineConfig({
  plugins: [vue(), tailwindcss()],
});

src常见 index.css 并index.js 引入 index.css 配置如下

js 复制代码
@import "tailwindcss";

app.vue写入类名,效果如下已经引入

js 复制代码
<span class="flex text-red-400">tailwindcss4</span>

自定义样式

index.css

js 复制代码
@import "tailwindcss";

@config "../tailwind.config.js";

添加 tailwind.config.js 配置如下

js 复制代码
/** @type {import('tailwindcss').Config} */
module.exports = {
  content: [
    "./index.html",
    "./src/**/*.{vue,js,ts,jsx,tsx}",
  ],
  theme: {
    extend: {
      // 自定义宽度类
      width: {
        ...Array.from({ length: 300 }, (_, i) => i + 1).reduce((acc, num) => {
          acc[num] = `${num}px`;
          return acc;
        }, {})
      },
      // 自定义高度类
      height: {
        ...Array.from({ length: 300 }, (_, i) => i + 1).reduce((acc, num) => {
          acc[num] = `${num}px`;
          return acc;
        }, {})
      },
      // 自定义 padding 类
      padding: {
        ...Array.from({ length: 150 }, (_, i) => i + 1).reduce((acc, num) => {
          acc[num] = `${num}px`;
          return acc;
        }, {})
      },
      // 自定义 margin 类
      margin: {
        ...Array.from({ length: 100 }, (_, i) => i + 1).reduce((acc, num) => {
          acc[num] = `${num}px`;
          return acc;
        }, {})
      },
      // 自定义 font-size 类
      fontSize: {
        ...Array.from({ length: 50 }, (_, i) => i + 1).reduce((acc, num) => {
          acc[num] = `${num}px`;
          return acc;
        }, {})
      }
    },
  },
  plugins: [],
};
js 复制代码
  <span class="flex text-red-400 w-100">tailwindcss4</span>

postcss适配可以自行引入

相关推荐
wuhen_n1 小时前
网络请求在Vite层的代理与Mock:告别跨域和后端依赖
前端·javascript·vue.js
小彭努力中1 小时前
193.Vue3 + OpenLayers 实战:圆孔相机模型推算卫星拍摄区域
vue.js·数码相机·vue·openlayers·geojson
用户69371750013846 小时前
Google 正在“收紧侧加载”:陌生 APK 安装或需等待 24 小时
android·前端
蓝帆傲亦6 小时前
Web 前端搜索文字高亮实现方法汇总
前端
用户69371750013846 小时前
Room 3.0:这次不是升级,是重来
android·前端·google
漫随流水8 小时前
旅游推荐系统(view.py)
前端·数据库·python·旅游
踩着两条虫9 小时前
VTJ.PRO 核心架构全公开!从设计稿到代码,揭秘AI智能体如何“听懂人话”
前端·vue.js·ai编程
jzlhll12310 小时前
kotlin Flow first() last()总结
开发语言·前端·kotlin
用头发抵命10 小时前
Vue 3 中优雅地集成 Video.js 播放器:从组件封装到功能定制
开发语言·javascript·ecmascript
蓝冰凌11 小时前
Vue 3 中 defineExpose 的行为【defineExpose暴露ref变量】详解:自动解包、响应性与实际使用
前端·javascript·vue.js