Closed7
tailwindCSS + Astro 3, @apply で、クラス短縮適用
概要
TailwindCSS + Astro 3、@applyのクラス短縮 メモです
- Astro版で、短縮表記の説明が無いようでしたので、調べました
- 下記、installation参考にしました
- TailwindCSS + Astro の準備は、前の記事に記載してます
環境
- TailwindCSS
- Astro 3
関連
- レイアウト参考
- 通常のクラス指定 ボタン (長いクラス指定方法)
<button class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded">
Normal-button</button>
- 短縮する方式
<button class="btn-green">Apply button</button>
短縮する方法
- global で、 btn-green定義しておく
- src/input.css
input.css
@tailwind base;
@tailwind components;
@tailwind utilities;
.btn {
@apply bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded;
}
.btn-green {
@apply bg-green-500 hover:bg-green-700 text-white font-bold py-2 px-4 rounded;
}
ビルド
-
/public/styles に出力する例です
-
wacth mode: --watch つける
npx tailwindcss -i ./src/input.css -o ./public/styles/output.css
#watch
npx tailwindcss -i ./src/input.css -o ./public/styles/output.css --watch
- layouts/Layout.astro
- output.css 読込む
<link href="/styles/output.css" rel="stylesheet">
- package.json に、コマンド追記する例
"scripts": {
...
"build-css": "npx tailwindcss -i ./src/input.css -o ./public/styles/output.css",
...
},
- コンパイル
yarn build-css
このスクラップは2023/09/02にクローズされました