Files
2nd/10_Wiki/Topic_CSS/CSS_Variables.md
T
koriweb 9609c04755 docs(10_Wiki): W3Schools 위키화 — HTML/CSS/JavaScript(core)
W3Schools 튜토리얼을 P-Reinforce v3.1 포맷으로 위키화(영어 본문, 한/영 섹션 헤더).
- Topic_HTML: 59문서 (튜토리얼+예제, 레퍼런스/메타 제외)
- Topic_CSS: 190문서 (메인 + Advanced/Flexbox/Grid/RWD 전체)
- Topic_JavaScript: 120문서 (코어 언어; Temporal/DOM상세/BOM/WebAPI/AJAX/jQuery/Graphics 등은 후속)
각 폴더 00_INDEX.md(MOC) 포함. 코드 verbatim, 미확인분은 "Not found in source" 표기.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-23 19:21:18 +09:00

156 lines
5.5 KiB
Markdown

---
id: css-variables-var
title: "CSS Variables var()"
category: "Frontend"
status: "draft"
verification_status: "conceptual"
canonical_id: ""
aliases: ["CSS variables", "var() function", "custom properties", "CSS custom properties", "root variables", "--variable"]
duplicate_of: ""
source_trust_level: "B"
confidence_score: 0.89
created_at: 2026-06-23
updated_at: 2026-06-23
review_reason: ""
merge_history: []
tags: ["css", "web", "frontend", "w3schools", "variables", "custom-properties", "var"]
raw_sources: ["https://www.w3schools.com/css/css3_variables.asp"]
applied_in: []
github_commit: ""
---
# [[CSS Variables var()]]
## 🎯 한 줄 통찰 (One-line insight)
CSS variables (custom properties) are declared with a `--name` and read back with `var(--name)`; declaring them once in `:root` gives document-wide reusable values that make stylesheets easier to maintain and update. [S1]
## 🧠 핵심 개념 (Core concepts)
- **Global vs local** — global variables are declared in the `:root` selector for document-wide access; local variables are declared within a specific selector for limited scope. [S1]
- **Naming rules** — variable names must begin with two dashes (`--`) and are case-sensitive. [S1]
- **The var() function** — the syntax is `var(--name, value)`, where the name is required and `value` is an optional fallback. [S1]
- **Why use them** — the main advantages are easier maintenance, improved readability, and simplified color updates across an entire page. [S1]
## 🧩 추출된 패턴 (Extracted patterns)
- **Theme tokens in :root** — declaring colors as `:root` variables creates a single place to change a whole page's palette. [S1]
- **Reuse via var()** — the same `var(--primary-bg-color)` reference reused across selectors keeps related values in sync. [S1]
- **Fallback safety** — `var(--name, value)` supplies a default when the named variable is not defined. [S1]
## 📖 세부 내용 (Details)
**Declaring variables** [S1]
Global variables are declared in the `:root` selector; local variables within a specific selector. Names start with `--` and are case-sensitive:
```css
:root {
--primary-bg-color: green;
}
.note {
--note-bg: yellow;
}
```
**The var() Function** [S1]
The `var()` function is used to insert the value of a CSS variable. Its syntax is `var(--name, value)``--name` is the required variable name, and `value` is an optional fallback used if the variable is not found.
**Example — global variables used across selectors** [S1]
```css
:root {
--primary-bg-color: #1e90ff;
--primary-color: #ffffff;
}
body {
background-color: var(--primary-bg-color);
}
.container {
color: var(--primary-bg-color);
background-color: var(--primary-color);
padding: 15px;
}
.container h2 {
border-bottom: 2px solid var(--primary-bg-color);
}
.container .note {
border: 1px solid var(--primary-bg-color);
padding: 10px;
}
```
**Example — swapping the color scheme by changing only the variables** [S1]
```css
:root {
--primary-bg-color: #8FBC8F;
--primary-color: #FFFAF0;
}
body {
background-color: var(--primary-bg-color);
}
.container {
color: var(--primary-bg-color);
background-color: var(--primary-color);
padding: 15px;
}
.container h2 {
border-bottom: 2px solid var(--primary-bg-color);
}
.container .note {
border: 1px solid var(--primary-bg-color);
padding: 10px;
}
```
**Benefits** [S1]
The page highlights three main advantages of CSS variables: easier maintenance, improved readability, and simplified color updates across entire pages.
## 🛠️ 적용 사례 (Applied in summary)
The page's examples are the applied cases: a `:root` palette referenced by `body`, `.container`, `.container h2`, and `.container .note`, and a second version showing how changing only the `:root` values re-themes the entire layout. No external project/commit applications found in the source.
## 💻 코드 패턴 (Code patterns)
Declare and use a global variable (language: CSS):
```css
:root {
--primary-bg-color: #1e90ff;
}
body {
background-color: var(--primary-bg-color);
}
```
var() with fallback (language: CSS):
```css
.example {
color: var(--name, value);
}
```
## ⚖️ 비교 및 선택 기준 (Comparison & decision criteria)
- **Global (`:root`) variables** — accessible through the entire document; use for site-wide theme tokens such as the primary palette. [S1]
- **Local variables** — declared inside a specific selector and usable only within it; use when a value should only apply to one section. [S1]
## ⚖️ 모순 및 업데이트 (Contradictions & updates)
No contradictions found in the source.
## ✅ 검증 상태 및 신뢰도
- **상태:** draft
- **검증 단계:** conceptual (실제 적용 사례 발견 시 applied/validated로 승격 가능)
- **출처 신뢰도:** B (W3Schools — widely used educational reference, not a primary standards body)
- **신뢰 점수:** 0.89
- **중복 검사 결과:** 신규 생성 (New discovery)
## 🔗 지식 그래프 (Knowledge Graph)
- **상위/루트:** [[CSS Tutorial]]
- **관련 개념:** [[CSS Overriding Variables]], [[CSS Colors]], [[CSS Selectors]], [[CSS Syntax]]
- **참조 맥락:** Referenced when centralizing reusable values (colors, sizes) for maintainable, themeable stylesheets.
## 📚 출처 (Sources)
- [S1] W3Schools — CSS Variables var() — https://www.w3schools.com/css/css3_variables.asp
## 📝 변경 이력 (Change history)
- 2026-06-23: Initial draft synthesized from the W3Schools "CSS Variables var()" page (Astra wiki-curation, P-Reinforce v3.1 format).