๐ป DEV/Tailwind CSS
[Tailwind CSS] 14. DarkMode (๋คํฌ๋ชจ๋)
Rising Oneโ
2025. 6. 13. 16:36
728x90
๋ฐ์ํ
SMALL
๋คํฌ๋ชจ๋๋ ํ๋ ์ด์์ฒด์ ์์ ๊ธฐ๋ณธ์ ์ผ๋ก ์ ๊ณตํ๋ ๊ธฐ๋ฅ์ด๋ค.
๋์ฑ์ด Light, Dark, System ๋ชจ๋๋ก ์ ๊ณตํ๋ค.
์์ฝ
1. ํ์ต๋ด์ฉ : DarkMode (๋คํฌ๋ชจ๋)
์์ธ
1. ํ์ต๋ด์ฉ : DarkMode (๋คํฌ๋ชจ๋)
โผ๏ธDarkMode ์์
- ๊ธฐ๋ณธ ์ ๋ต : ์ด์์ฒด์ ์ ํธ๋ ์ฌ์ฉ (media)
Tailwind๋ ๊ธฐ๋ณธ์ ์ผ๋ก ์ด์ ์ฒด์ ์ ๋คํฌ ๋ชจ๋ ์ ํธ๋์ ๋ฐ๋ผ ์คํ์ผ์ ์ ์ฉํฉ๋๋ค.
์ด๋ฅผ ์ํด CSS์ prefers-color-scheme ๋ฏธ๋์ด ์ฟผ๋ฆฌ๋ฅผ ์ฌ์ฉํฉ๋๋ค.
๋ณ๋์ ์ค์ ์์ด Tailwind์์ ๋ฐ๋ก ์ฌ์ฉํ ์ ์์ต๋๋ค.
- ์ ํ์ ์ ๋ต : ์๋์ผ๋ก ๋คํฌ ๋ชจ๋ ์ ํ (selector)
// tailwind.config.js
module.exports = {
darkMode: 'selector', // ์ ํ์ ์ ๋ต(์๋ ์ ํ)
// ...
}
โผ๏ธDarkMode ์ ์์ (tailwind.config.ts)
// npm ๋ค์ด๋ก๋
npm install next-themes
// npm ์ค์น
npm install @tailwindcss/forms @tailwindcss/typography @tailwindcss/aspect-ratio
---
// tailwind.config.ts
import type {Config} from 'tailwindcss';
import forms from '@tailwindcss/forms';
import typography from '@tailwindcss/typography';
import aspectRatio from '@tailwindcss/aspect-ratio';
const config: Config = {
content: [
'./app/**/*.{js,ts,jsx,tsx,mdx}',
'./pages/**/*.{js,ts,jsx,tsx,mdx}',
'./components/**/*.{js,ts,jsx,tsx,mdx}',
],
darkMode: 'class', // or 'media' or false
theme: {
extend: {
colors: {
primary: '#1E40AF',
secondary: '#64748B',
},
fontFamily: {
sans: ['Inter', 'sans-serif'],
},
},
},
plugins: [
forms,
typography,
aspectRatio,
],
};
export default config;
โผ๏ธDarkMode ์์
<div
class="bg-white dark:bg-slate-800 text-slate-900 dark:text-white p-6 rounded-lg shadow-md max-w-lg"
>
<h3 class="text-lg font-bold">Writes Upside-Down</h3>
<p class="text-slate-600 dark:text-slate-400">
The Zero Gravity Pen can be used to write in any orientation, including
upside-down. It even works in outer space.
</p>
</div>
728x90
๋ฐ์ํ
LIST