Vue專案加入 Tailwind CSS
avatar-img
P工

Vue專案加入 Tailwind CSS

更新於 發佈於 閱讀時間約 3 分鐘

安裝所需元件

Tailwind CSS 是近年熱門流的CSS 樣式工具套件。class名稱簡單易理解是他的最大特色之一,在設計上不易被其他前端框架套件拘束。是一個入門門檻低的前端樣式好用工具。

使用npm安裝元件

npm install -D tailwindcss@latest postcss autoprefixer

初始tailwindcss

npx tailwindcss init -p

這個指令會產生兩個檔案:

  • tailwind.config.js
  • postcss.config.js

編輯設定

tailwind.config.js

將需要帶入CSS的程式檔加入設定

// tailwind.config.js
module.exports = {
content: ["./src/**/*.{html,js,ts,vue}"],
theme: {
extend: {},
},
variants: {
extend: {},
},
plugins: [],
}

postcss.config.js

// postcss.config.js
module.exports = {
plugins: {
tailwindcss: {},
autoprefixer: {},
},
}


在CSS裡加入tailwind

將css帶入程式

接下來建立input.css並加入以下設定

@tailwind base;
@tailwind components;
@tailwind utilities;

再用以下指令產生正式index.css

npx tailwindcss -i ./src/input.css -o ./src/index.css --watch

接下來在main.js或main.ts引入CSS檔

import { createApp } from "vue";
import "./index.css";

// ...
app.mount("#app");
// ...

CLI指令的--watch參數,提供在編輯時期的watch mode,即時產生所需的css設定加入./src/index.css

Rebuilding...
Done in 100ms.

當每次將tailwind的CSS Tag加入 class中,watch mode會隨著產生相應的設定增加到index.css,接著就可以看到index.css檔案裡內容的變化。



參考:

https://tailwindcss.com/docs/installation

https://v2.tailwindcss.com/docs/guides/vue-3-vite


avatar-img
P工
0會員
6內容數
程式打工族。前端網頁至韌體程式之間。還有其它廣泛的興趣。 正在學習德語及法律課程。
留言
avatar-img
留言分享你的想法!
P工 的其他內容
VUE 在localhost執行debug 如何自定Debug SeverIP或Port,解決CORS問題
有人在手機上打字飛快,而我就是一個打字不快的人自從開始用語音輸入之後很快就愛上這個功能。最近也試著想在Mac電腦裡,試著用語音輸入。 啟用語音設定 在設定頁裡輸Dictation 啟用語音輸入 在Language 的項目點選Editor 選擇辨識語言 在搜尋框裡搜尋辨識的語言。這裡我多選
VUE 在localhost執行debug 如何自定Debug SeverIP或Port,解決CORS問題
有人在手機上打字飛快,而我就是一個打字不快的人自從開始用語音輸入之後很快就愛上這個功能。最近也試著想在Mac電腦裡,試著用語音輸入。 啟用語音設定 在設定頁裡輸Dictation 啟用語音輸入 在Language 的項目點選Editor 選擇辨識語言 在搜尋框裡搜尋辨識的語言。這裡我多選