Tailwind 基礎介紹
Tailwind 是一套 utility-first css 框架 透過它預定義好的 class,可以不用寫 css 就能做出漂亮的網站 🎉
CSS 狀態
需要依據元素狀態變更指定的 style,可以使用 : 語法
Pseudo-classes:
hover:<tailwind-class>|active:<tailwind-class>|focus:<tailwind-class>Pseudo-elements:
before:content-[]|after:<tailwind-class>Responsive breakpoints:
md:<tailwind-class>|lg:<tailwind-class>
Handling Hover, Focus, and Other States
排版常用
flexFlexgapGappPaddingmMarginspaceSpace Between
Breakpoints
Tailwind 定義好的 Breakpoint,只需要套上 class 不需要寫任何 css 也能做 RWD 😆
| Breakpoint | width | CSS |
|---|---|---|
| sm | 640px | @media (min-width: 768px) { ... } |
| md | 768px | @media (min-width: 768px) { ... } |
| lg | 1024px | @media (min-width: 1024px) { ... } |
| xl | 1280px | @media (min-width: 1280px) { ... } |
| 2xl | 1536px | @media (min-width: 1536px) { ... } |
常用指令
@layer
對 css 做分類,防止 css 覆蓋問題,能更好管理
base用於刻製純 HTML 元素的預設樣式等內容(p, h1, h2...)component覆蓋其他 class 的樣式,可重複使用utilities單一用途類,這些類應始終優先於任何其他樣式
@tailwind base;
@tailwind components;
@tailwind utilities;
@layer components {
.my-custom-style {
/* ... */
}
}
@apply
可以在 css 檔案裡面使用 Tailwind 語法
.select2-dropdown {
@apply rounded-b-lg shadow-md;
}
.select2-search {
@apply border border-gray-300 rounded;
}
.select2-results__group {
@apply text-lg font-bold text-gray-900;
}
也可以套用自定義 class
.foo {
color: blue;
}
.bar {
@apply foo;
}
@theme
可以在 css 檔案中引用 Tailwind Config 裡定義的參數
.content-area {
height: calc(100vh - theme("spacing.12"));
}
.content-area {
height: calc(100vh - theme("spacing[2.5]"));
}
自訂數值
使用中括號來設定任意的數值
w-[50vw]⇒width: 50vwh-[50px]⇒height: 50pxm-[15px]⇒margin: 15px
Tailwind theme 全域設定
依據tailwind.config.js 該檔案裡的設定
theme物件裡面會重新定義 Tailwind class 實際的 csstheme.extend可以繼承 Tailwind 預設的再加上自定義的 class
module.exports = {
theme: {
screens: {
sm: "640px",
md: "768px",
lg: "1024px",
xl: "1280px",
"2xl": "1536px",
},
extend: {
spacing: {
128: "32rem",
144: "36rem",
},
borderRadius: {
"4xl": "2rem",
},
},
},
};