--- 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).