--- id: wiki-2026-0508-css-modules title: CSS Modules category: 10_Wiki/Topics status: verified canonical_id: self aliases: [CSS Module, Locally-Scoped CSS] duplicate_of: none source_trust_level: A confidence_score: 0.9 verification_status: applied tags: [css, frontend, scoping, build-tool] raw_sources: [] last_reinforced: 2026-05-10 github_commit: pending tech_stack: language: css framework: webpack/vite --- # CSS Modules ## 매 한 줄 > **"매 class 가 file-locally scoped"**. CSS Modules 매 build-time transform 으로 매 class name 을 unique hash 로 rewriting — 매 global namespace pollution 의 elimination + component-level encapsulation 의 enable. 매 2026 현재 Vite/Webpack/Next.js 매 native support, 매 CSS-in-JS runtime cost 의 alternative 로 주류. ## 매 핵심 ### 매 작동 원리 - `Button.module.css` 매 import 시 매 bundler 가 매 class 를 `Button_primary__a3fG2` 로 rename. - 매 import 결과 매 object — `{ primary: 'Button_primary__a3fG2' }`. - 매 component 매 `styles.primary` 로 reference — 매 collision-free. ### 매 vs alternatives - **vs global CSS**: 매 scoping 자동, 매 BEM 매 manual convention 의 replacement. - **vs CSS-in-JS**: 매 zero runtime, 매 build-time only — 매 bundle size + perf 우위. - **vs Tailwind**: 매 component-local custom design 매 적합, Tailwind 매 utility-first. ### 매 응용 1. Component library (Button, Input) 매 encapsulated styling. 2. Next.js 매 default-supported pattern — `*.module.css` 매 convention. 3. Design system 매 token + component 매 layered structure. ## 💻 패턴 ### Basic usage ```css /* Button.module.css */ .primary { background: #0070f3; color: white; padding: 8px 16px; border-radius: 4px; } .disabled { opacity: 0.5; cursor: not-allowed; } ``` ```tsx // Button.tsx import styles from './Button.module.css'; export function Button({ disabled, children }: Props) { return ( ); } ``` ### Composition (`composes`) ```css /* base.module.css */ .button { font: inherit; cursor: pointer; } /* Button.module.css */ .primary { composes: button from './base.module.css'; background: #0070f3; } ``` ### `clsx` 와 conditional classes ```tsx import clsx from 'clsx'; import styles from './Card.module.css'; export function Card({ variant, active }: Props) { return (