1. Tailwindcss
https://tailwindcss.com/docs/installation
- Utility-first CSS framework,
- ์ฆ ๋ฏธ๋ฆฌ ์ ์๋ CSS์ ํน์ฑ๋ค์
class๋ฅผ ํตํด ๋ถ๋ฌ์ ์ ์ฉ์ํค๋ ํํ์ CSS ํ๋ ์์ํฌ - ์ํ๋
html์์์class์ ๋ถ์ฌํด์ฃผ๋ฉด ๋ฐ๋ก ์ฌ์ฉ์ด ๊ฐ๋ฅํ๋ค
- ์ฆ ๋ฏธ๋ฆฌ ์ ์๋ CSS์ ํน์ฑ๋ค์
Tailwind CSS: CSS ํ๋ ์์ํฌ. ๋ฏธ๋ฆฌ ์ ์๋ ํด๋์ค๋ฅผ ์ฌ์ฉํ์ฌ HTML ์คํ์ผ์ ์ฝ๊ฒ ์ ์ฉํ ์ ์์- e.g.) bg-blue-500 ํด๋์ค๋ฅผ ์ ์ฉํ๋ฉด, ๋ฐ๋ก CSS ์คํ์ผ๋ง ์์ด๋ ํ๋ ๋ฐฐ๊ฒฝ์ ๋ง๋ค ์ ์์
- e.g.) px-4, mx-4 ํด๋์ค๋ค์ ์ ์ฉํ๋ฉด, ๋ฐ๋ก CSS ์คํ์ผ๋ง ์์ด๋ padding, margin ๊ด๋ฆฌ ๊ฐ๋ฅ
ํด๋์ค ๊ธฐ๋ฐ: Utility-first๋ผ๋ ๊ฐ๋ ํ์ ํด๋์ค ์ด๋ฆ์ผ๋ก ์คํ์ผ๋ง ๊ฐ๋ฅ. HTML๊ณผ CSS์ ๋ถ๋ฆฌ๋ฅผ ์ต์ํํ๊ณ ์ฌ์ฌ์ฉ์ฑ์ ๋์์ปค์คํ ์ค์ : tailwind.config.js ํ์ผ์ ํตํด ์ฌ์ฉ์ ์ ์ ์คํ์ผ ๋ฐ ๊ตฌ์ฑ ์ ๊ณต์ผ๊ด๋ ๋์์ธ: ๋ฏธ๋ฆฌ ์ ์๋ ํด๋์ค๋ก ์ผ๊ด์ ์๋ ๋์์ธ ๊ตฌํ ๊ฐ๋ฅ๋ฐ์ํ ์คํ์ผ๋ง: ๋ฏธ๋์ด ํฌ๊ธฐ์ ๋ง๋ ๋ฐ์ํ ๋์์ธ์ ์ง์ํ๋ฉฐ, ํด๋์ค ์ ๋์ฌ๋ก ํธ๋ฆฌํ๊ฒ ์ ์ด ๊ฐ๋ฅํ๋ถํ ์๋ฃ: ๊ฑฐ๋ํ ์ปค๋ฎค๋ํฐ์ ํ๋ถํ ์๋ฃ, ๊ทธ๋ฆฌ๊ณ Tailwind๋ฅผ ์ด์ฉํ ์ฌ๋ฌ๊ฐ์ง ์ปดํฌ๋ํธ๋ค- Tailwind CSS ๊ณต์ ๋ฌธ์: https://tailwindcss.com/docs/installation
cf. VSCode ํ์ฅ๊ธฐ๋ฅ
- Tailwind CSS IntelliSense : ํด๋์ค๋ช ์๋์์ฑ
- Tailwind Docs : ๋ฌธ์ ์ฐพ์๋ณผ ๋ Ctrl + Shipt + P๋ก ๊ฒ์
1.1 Tailwind ์ฃผ์ ๊ฐ๋
- Utility-First Fundamentals (์ ํธ๋ฆฌํฐ-ํผ์คํธ ๊ธฐ๋ณธ ์์น)
- Handling Hover, Focus, and Other States (ํธ๋ฒ, ํฌ์ปค์ค ๋ฐ ๋ค๋ฅธ ์ํ ์ฒ๋ฆฌ)
- Responsive Design (๋ฐ์ํ ๋์์ธ)
- Dark Mode (๋คํฌ๋ชจ๋)
- Reusing Styles (์คํ์ผ ์ฌ์ฌ์ฉ)
- Adding Custom Styles (์ปค์คํ ์คํ์ผ ์ถ๊ฐ)
- Functions & Directives (ํจ์ ๋ฐ ๋๋ ํฐ๋ธ)
- cf. Tailwind CSS ๊ณต์ ๋ฌธ์ ์ฐธ๊ณ : https://tailwindcss.com/docs/utility-first
1.2 Tailwind CSS ์ฅ๋จ์
[์ฅ์ ]
- ๋น ๋ฅธ ๊ฐ๋ฐ: ํด๋์ค ๊ธฐ๋ฐ ์คํ์ผ๋ง์ ํตํด ๋ณด๋ค ๋น ๋ฅด๊ฒ ๊ฐ๋ฐํ ์ ์์
- ์ปค์คํฐ๋ง์ด์ง: Tailwind ์ค์ ํ์ผ์ ํตํด ํ๋ก์ ํธ์ ๋ง๊ฒ ์คํ์ผ ์กฐ์ ๊ฐ๋ฅ
- ์ฌ์ฌ์ฉ์ฑ: ํด๋์ค๋ฅผ ์ฌ์ฌ์ฉํด์ ์ผ๊ด๋ ๋์์ธ ๊ตฌํ ๊ฐ๋ฅ
- ๋ฐ์ํ: ํด๋์ค ์ ๋์ฌ๋ก ๋ฐ์ํ ๋์์ธ์ ๊ฐํธํ๊ฒ ๊ตฌํํ ์ ์์ (ex. lg:text-red-500, md:p-4)
[๋จ์ ]
- ํ์ผ ํฌ๊ธฐ: Tailwind๋ฅผ ์ฌ์ฉํด์ ๊ตฌํํ๋ฉด ํด๋์ค๊ฐ ๋ณต์กํด์ง๊ณ , ํ์ผ ํฌ๊ธฐ๊ฐ ์ปค์ง ์ ์์
- ํ์ต ๊ณก์ : ์ฒ์์๋ ํด๋์ค ๊ธฐ๋ฐ ์คํ์ผ๋ง์ ์ต์ํ์ง ์์, ์ด๊ธฐ ํ์ต ๋น์ฉ์ด ๋ง์ด ๋ค์ด๊ฐ
2. ๊ธฐ๋ณธ ์ฌ์ฉ๋ฒ
https://tailwindcss.com/docs/installation
1npm install -D tailwindcss2npx tailwindcss init
tailwindcss๋ฅผ ์ฌ์ฉํ๊ณ ์ ํ๋ ๊ฒฝ๋ก, ํ์ผ ์์ฑ(ํ์ฅ์๋ช ์ ๋์์ฐ๋ฉด ์๋จ)
1/** @type {import('tailwindcss').Config} */2module.exports = {3content: ['./src/**/*.{html,js,jsx,ts,tsx}'],4theme: {5extend: {},6},7plugins: [],8}
์ ์ญ CSS์ ๋ค์ ์ฝ๋ ์ถ๊ฐ (CRA์์๋ index.css ๋งจ ์์ ์ถ๊ฐ)
1/* tailwind ๊ธฐ๋ณธ ์คํ์ผ ๋ฐ ํ์ฅ๋ ์คํ์ผ ์ฃผ์ */2@tailwind base;34/* tailwind ๊ธฐ๋ณธ ์ปดํฌ๋ํธ ํด๋์ค ๋ฐ ํ์ฅ๋ ์ปดํฌ๋ํธ ํด๋์ค ์ฃผ์ */5@tailwind components;67/* tailwind ๊ธฐ๋ณธ ์ ํธ๋ฆฌํฐ ํด๋์ค ๋ฐ ํ์ฅ๋ ์ ํธ๋ฆฌํฐ ํด๋์ค ์ฃผ์ */8@tailwind utilities;
2.1 Background
1import React from 'react'23export default function Background() {4return (5<div className="bg-gray-700" style={{ height: 600 }}>6<div style={{ height: 100 }}>7<div style={{ height: 'inherit' }} class="bg-sky-500/100"></div>8<div style={{ height: 'inherit' }} class="bg-sky-500/75"></div>9<div style={{ height: 'inherit' }} class="bg-sky-500/50"></div>10</div>11</div>12)13}
2.2 TailwindExample
1import Background from './Background'23export default function TailwindExample() {4return (5<div>6<Background />7</div>8)9}
2.3 App.js
1// Webpack CSS import2// import 'onsenui/css/onsenui.css';3// import 'onsenui/css/onsen-css-components.css';4// import OnesenUIExample from './components/OnsenUI/OnesenUIExample';5// import AntDesignExample from './components/AntDesign/AntDesignExample';6// import 'semantic-ui-css/semantic.min.css';7// import ReactSemanticUIExample from './components/ReactSemanticUI/ReactSemanticUIExample';8// import 'bootstrap/dist/css/bootstrap.min.css';9// import ReactBootstrapExample from './components/ReactBootstrap/ReactBootstrapExample';10// import MaterialUIExample from './components/MaterialUI/MaterialUIExample';11import TailwindExample from './components/Tailwind/TailwindExample'1213export default function App() {14return (15<>16<TailwindExample />17{/* <MaterialUIExample /> */}18{/* <ReactBootstrapExample /> */}19{/* <ReactSemanticUIExample /> */}20{/* <AntDesignExample /> */}21{/* <OnesenUIExample /> */}22</>23)24}
3. Next.js์์ Tailwind CSS ์ฌ์ฉ
(1) yarn์ผ๋ก Tailwind CSS ์ค์น ํ, Tailwind CSS ๊ตฌ์ฑ ํ์ผ ์์ฑ์ ์ํด tailwindcss init ์ปค๋งจ๋ ์ ๋ ฅ
1yarn add -D tailwindcss postcss autoprefixer2npx tailwindcss init -p
(2) PostCSS ์ค์ ํ์ผ์ Tailwind ์ถ๊ฐ
1// postcss.config.js2module.exports = {3plugins: {4tailwindcss: {},5autoprefixer: {},6},7}
(3) TailwindCSS๋ฅผ ์ ์ฉํ ํ
ํ๋ฆฟ ํ์ผ ๊ฒฝ๋ก๋ฅผ tailwind.config.js ์ค์ ํ์ผ์ ๋ช
์ํ๋ค
1import type { Config } from 'tailwindcss'23const config: Config = {4content: [5'./src/pages/**/*.{js,ts,jsx,tsx,mdx}',6'./src/components/**/*.{js,ts,jsx,tsx,mdx}',7'./src/app/**/*.{js,ts,jsx,tsx,mdx}',8],9theme: {10extend: {},11},12plugins: [],13}14export default config
(4) Tailwind CSS๋ฅผ ์ฌ์ฉํ๊ธฐ ์ํ CSS ํ์ผ ์ ์์ฑํ๊ณ tailwindcss๋ฅผ import ํ๋ค
1/* globals.css */2@tailwind base;3@tailwind components;4@tailwind utilities;
(5) ํ๋ก์ ํธ๋ฅผ ์์ํ๊ณ , tailwind ํด๋์ค๋ฅผ ์ ์ฉํ๋ค
4. ์์ฃผ ์ฌ์ฉํ๋ Tailwind CSS ํด๋์ค
Tailwind CSS ํด๋์ค์๋ ์์, ํฐํธ, ์ฌ๋ฐฑ, ๊ธธ์ด, ๊ทธ๋ฆฌ๋, ํ๋ ์ค, ํ ๋๋ฆฌ ๋ฑ ์ฌ๋ฌ๊ฐ์ง ์์ฑ ์ ์ ๊ฐ๋ฅ
bg-{color}: ๋ฐฐ๊ฒฝ์์ ์ง์ ํ๋ ํด๋์ค๋ก, ์์ ์ฝ๋ ๋ฐ ์ด๋ฆ ์ ์ (e.g. bg-indigo-500)text-{color}: ํ ์คํธ ์์์ ์ง์ ํ๋ ํด๋์ค๋ก, ์์ ์ฝ๋ ๋ฐ ์ด๋ฆ ์ ์ (e.g. text-red-700)font-{weight}: ํฐํธ ๊ตต๊ธฐ๋ฅผ ์ง์ ํ๋ ํด๋์ค๋ก, ํฐํธ ๊ตต๊ธฐ ๋ฐ ์ด๋ฆ ์ ์ (e.g. font-bold, font-[700])p-{size}, m-{size}: ํจ๋ฉ ๋ฐ ๋ง์ง์ ์ง์ ํ๋ ํด๋์ค๋ก, ๊ฐ๊ฐ์ ํจ๋ฉ/๋ง์ง ํฌ๊ธฐ ์ ์ (e.g. p-2, m-2)w-{size}, h-{size}: ๋๋น ๋ฐ ๋์ด๋ฅผ ์ง์ ํ๋ ํด๋์ค๋ก, ๊ฐ๊ฐ์ ๋๋น/๋์ด ํฌ๊ธฐ ์ ์ (e.g. w-1/2, h-64)grid, flex: ๊ทธ๋ฆฌ๋์ ํ๋ ์ค๋ฅผ ์ ์ํ ๋ ์ฌ์ฉํ๋ ํด๋์ค (e.g. grid grid-rows-4)rounded-{radius}: ์์๋ฅผ ๋ฅ๊ธ๊ฒ ๋ง๋๋ ํด๋์ค (e.g. rounded-lg)border-{width}: ํ ๋๋ฆฌ์ ๊ตต๊ธฐ๋ฅผ ์ ์ํ ๋ ์ฌ์ฉํ๋ ํด๋์ค (e.g. border-4)
4.1 Utility-First Fundamentals
- TailwindCSS๋ ํด๋์ค ๊ธฐ๋ฐ์ ์คํ์ผ๋ง์ ์ฌ์ฉํ๋ฉฐ, ๋ค์ํ ์ ํธ๋ฆฌํฐ ํด๋์ค๋ฅผ ํตํด ์คํ์ผ ์ ์ฉ
bg-blue-500: ๋ฐฐ๊ฒฝ์์ ํ๋์์ผ๋ก ์ง์ text-white: ํ ์คํธ ์์์ ํฐ์์ผ๋ก ์ง์ p-4: ํจ๋ฉ์ ํฌ๊ธฐ 4๋ก ์ง์
4.2 Handling Hover, Focus, and Other States
- Tailwind CSS๋ hover, focus ๋ฐ ๋ค๋ฅธ ์ํ์ ๋ํ ํด๋์ค๋ค์ ์ ๊ณตํ์ฌ ์ํธ์์ฉํ๋ ์คํ์ผ ์ฒ๋ฆฌ ๊ฐ๋ฅ
hover:bg-red-700: ๋ง์ฐ์ค ํธ๋ฒ ์ํ์์ ๋ฐฐ๊ฒฝ์ ๋ณ๊ฒฝfocus:bg-red-700: ํฌ์ปค์ค์ ๋ฐฐ๊ฒฝ์ ๋ณ๊ฒฝfocus:outline-none: ํฌ์ปค์ค์ ๊ธฐ๋ณธ ์์๋ผ์ธ ์ ๊ฑฐ
4.3 Responsive Design
- Tailwind๋ ๋ฐ์ํ ๋์์ธ์ ์ํ ํด๋์ค๋ฅผ ์ ๊ณตํ๋ฉฐ, ํ๋ฉด ํฌ๊ธฐ์ ๋ฐ๋ผ ์คํ์ผ ์กฐ์ ๊ฐ๋ฅ
- e.g.
md:bg-red-500์ ์ค๊ฐ ํฌ๊ธฐ ํ๋ฉด์์ ๋ฐฐ๊ฒฝ์์ ๋นจ๊ฐ์ผ๋ก ๋ณ๊ฒฝ
4.4 Dark Mode
- Tailwind ๋คํฌ ๋ชจ๋๋ฅผ ๊ตฌํํ๊ธฐ ์ํ ํด๋์ค๋ฅผ ์ ๊ณตํ๋ฉฐ,
- ๋คํฌ ํ ๋ง์ ๋ผ์ดํธ ํ ๋ง ๊ฐ ์ ํ ์ฝ๊ฒ ์ฒ๋ฆฌํ ์ ์์
dark:bg-gray-800: ๋คํฌ๋ชจ๋์์ ๋ฐฐ๊ฒฝ์์ ์ด๋ก๊ฒ ๋ณ๊ฒฝdark:text-white: ๋คํฌ๋ชจ๋์์ ํ ์คํธ ์์ ๋ณ๊ฒฝ
4.5 Reusing Styles
- Tailwind๋ ํด๋์ค๋ฅผ ์กฐํฉํด์ ์คํ์ผ์ ์ฌ์ฌ์ฉํ ์ ์์ผ๋ฉฐ, ์ฌ์ฉ์ ์ ์ ํด๋์ค๋ฅผ ์์ฑํ ์๋ ์์
- ์์ฃผ ์ฌ์ฉํ๋ ํด๋์ค ์ฝ๋๋ค์ ๋งค๋ฒ ์ปดํฌ๋ํธ๋ง๋ค ๋๊ฐ์ด ๋ณต์ฌํ๋ ๊ฒ์ด ์๋๋ผ,
- ํ๋์ ํด๋์ค๋ก ์ ๋ฆฌํด์ ์ฌ์ฌ์ฉํ ์ ์์
btn-primary: ๋ฏธ๋ฆฌ ์ ์๋ ํด๋์ค๋ฅผ ์กฐํฉํ์ฌ ์ฌ์ฌ์ฉ๊ฐ๋ฅํ ์คํ์ผ์ ์์ฑํจ
4.6 Adding Custom Styles
tailwind.config.jsํ์ผ์ ํตํด์ ์ฌ์ฉ์ ์ ์ ์คํ์ผ ๋ฐ ์ปค์คํ ํด๋์ค๋ฅผ ์ถ๊ฐํ ์ ์์- colors์ ์ํ๋ ์์์ ์ถ๊ฐํ์ฌ, tailwind์์ ์ฌ์ ์ ์ ์ํ ์์์ด ์๋, ์ฌ์ฉ์๊ฐ ์ํ๋ ์์์ผ๋ก ์ปค์คํ ๊ฐ๋ฅ
- e.g.
bg-blue: ๊ธฐ์กด tailwind ์์์ด ์๋, #1fb6ff ์์์ผ๋ก ๋์ด
4.7 Functions & Directives
- Tailwind์ ํจ์์ ๋๋ ํฐ๋ธ๋ Tailwind๋ฅผ ๋์ฑ ํ์ฅ ๊ฐ๋ฅํ๊ฒ ๋ง๋ค์ด์ฃผ๊ณ , ์ปค์คํฐ๋ง์ด์งํ ๋ ์ ์ฉ
- Tailwind์์๋ ํ
๋ง ๊ฐ์ ๋์ ์ผ๋ก ์ฝ์ ์ ์๋ theme() ํจ์์
- ๋ฏธ๋์ด ์ฟผ๋ฆฌ๋ฅผ ๋์ ์ผ๋ก ์์ฑํ ์ ์๋ screen() ํจ์๋ฅผ ์ ๊ณต
- ๋ํ, Tailwind๋ง์ @๊ท์น(at-rule) ๋๋ ํฐ๋ธ๋ฅผ ์ฌ์ฉํด์ ์กฐ๊ฑด์ ๋ฐ๋ผ ์์์ ์คํ์ผ์ ์กฐ์ ๊ฐ๋ฅ
@tailwind: HTML ์์์ Tailwind CSS ํด๋์ค๋ฅผ ๋์ ์ผ๋ก ์ถ๊ฐ ๊ฐ๋ฅ@layer: CSS ๊ท์น์ ๋ ์ด์ด๋ณ๋ก ๊ตฌ์ฑํ ์ ์๊ฒ ํจ@apply: CSS ํด๋์ค๋ฅผ ๋์ ์ผ๋ก ์ ์ฉํ ์ ์์@config: Tailwind CSS ๊ตฌ์ฑ ์ต์ ๋ณ๊ฒฝ ๊ฐ๋ฅ
5. Tailwind CSS Tutorial
5.1 Tailwind Playground ์ค์ต
- https://play.tailwindcss.com/ ์์ Tailwind ์ค์ตํด๋ณด๊ธฐ
1<!-- 1.rem, px -->2<!-- <div class="w-4 w-[20px] w-4">111</div> -->34<!-- 2. bg, border, border-color, rounded -->5<!-- <div class="bg-pink-400 w-64 h-64 border-8 border-black rounded-[25px]">111</div> -->67<!-- 3. w, h, p(ํจ๋ฉ), m(๋ง์ง) -->8<!-- <div class="w-64 h-64 bg-pink-400 w-[350px] h-[350px] p-4 p-[20px] m-4 mx-10 my-20">111</div> -->910<!-- 4. text-color, text-size, font-bold, cursor -->11<!-- <div class="w-64 h-64 bg-black text-pink-400 text-[50px] font-[100] cursor-pointer">111</div> -->1213<!-- 5. flex, flex-col, justify, item, gap -->14<!-- <div class="h-64 w-64 bg-pink-400 text-[50px] text-black flex justify-center itewms-center">111</div> -->1516<!-- <div class="h-[750px] w-[750px] bg-black flex flex-col justify-center items-center gap-4">17<div class="flex h-64 w-64 items-center justify-center bg-pink-400 text-[50px] text-black">111</div>18<div class="flex h-64 w-64 items-center justify-center bg-pink-400 text-[50px] text-black">222</div>19</div> -->2021<!-- 6. hover, transition -->22<!-- <div class="h-[750px] w-[750px] flex-col items-center justify-center gap-4 bg-black">23<div24class="hover:bg-purple-400 transition flex h-64 w-64 items-center justify-center bg-pink-400 text-[50px] text-black"25>2611127</div>28<div29class="hover:text-purple-400 transition-colors flex h-64 w-64 items-center justify-center bg-pink-400 text-[50px] text-black"30>3122232</div>33</div> -->3435<!-- e.g. ํฝํธ๋ก๊ทธ๋จ -- ์ ํธ๋ฑ + ํก๋จ๋ณด๋ -->36<section class="bg-neutral-700 flex flex-row justify-between items-center h-[250px] w-full group">37<div class="flex flex-row gap-4 group-hover:bg-yellow-400">38<div class="w-4 h-[150px] bg-white"></div>39<div class="w-4 h-[150px] bg-white"></div>40<div class="w-4 h-[150px] bg-white"></div>41<div class="w-4 h-[150px] bg-white"></div>42<div class="w-4 h-[150px] bg-white"></div>43</div>44<div class="flex flex-row justify-center items-center gap-3">45<div class="w-32 h-32 bg-red-500 rounded-full border-2 border-black hover:bg-red-300 transition"></div>46<div class="w-32 h-32 bg-yellow-500 rounded-full border-2 border-black hover:bg-yellow-300 transition"></div>47<div class="w-32 h-32 bg-green-500 rounded-full border-2 border-black hover:bg-green-300 transition"></div>48</div>49<div class="flex flex-row gap-4 group-hover:bg-pink-400">50<div class="w-4 h-[150px] bg-white"></div>51<div class="w-4 h-[150px] bg-white"></div>52<div class="w-4 h-[150px] bg-white"></div>53<div class="w-4 h-[150px] bg-white"></div>54<div class="w-4 h-[150px] bg-white"></div>55</div>56</section>
5.2 ์ฐธ๊ณ : Prettier
1npm install -D prettier prettier-plugin-tailwindcss
ํด๋น ํ๋ฌ๊ทธ์ธ์ tailwindcss ๊ณต์ Prettier ํ๋ฌ๊ทธ์ธ์ ๋๋ค. ํด๋์ค๋ช ์ tailwindcss๊ฐ ์ ํ ๊ธฐ์ค๋๋ก ๋์ดํด์ฃผ๋ ํ๊ฒฐ ์ฝ๋๊ฐ ๊น๋ํด์ง
- https://tailwindui.com/templates?ref=sidebar
- ํ ํ๋ฆฟ๋ ์ ๊ณตํจ