๐Ÿ’ป DEV/Tailwind CSS

[Tailwind CSS] 14. DarkMode (๋‹คํฌ๋ชจ๋“œ)

Rising Oneโ˜… 2025. 6. 13. 16:36
728x90
๋ฐ˜์‘ํ˜•
SMALL

tailwind CSS ๋กœ๊ณ 

 

 

๋‹คํฌ๋ชจ๋“œ๋Š” ํ˜„๋Œ€ ์šด์˜์ฒด์ œ์—์„  ๊ธฐ๋ณธ์ ์œผ๋กœ ์ œ๊ณตํ•˜๋Š” ๊ธฐ๋Šฅ์ด๋‹ค.

๋”์šฑ์ด 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