← 전체 글

CSS 변수만으로 다크모드 토글 구현하기

핵심 아이디어

:root에 라이트 모드 색상을 정의하고, :root[data-theme="dark"]에서 값을 덮어쓴다. 버튼 클릭 시 document.documentElement의 data-theme 속성만 토글하면 된다.

:root {
  --bg: #ffffff;
  --fg: #111111;
}

:root[data-theme='dark'] {
  --bg: #111111;
  --fg: #f5f5f5;
}

body {
  background: var(--bg);
  color: var(--fg);
}

시스템 설정 존중하기

사용자가 아직 토글을 누르지 않았다면 prefers-color-scheme 미디어 쿼리로 시스템 설정을 따르고, 토글을 누른 이후에는 localStorage에 선택을 저장해서 다음 방문에도 유지되게 했다.

정리