--- id: css-variables-and-javascript title: "CSS Variables and JavaScript" category: "Frontend" status: "draft" verification_status: "conceptual" canonical_id: "" aliases: ["CSS custom properties JavaScript", "setProperty CSS variable", "getPropertyValue", "change CSS variable with JS", "CSS variables DOM"] duplicate_of: "" source_trust_level: "B" confidence_score: 0.88 created_at: 2026-06-23 updated_at: 2026-06-23 review_reason: "" merge_history: [] tags: ["css", "web", "frontend", "w3schools", "css-variables", "javascript", "dom"] raw_sources: ["https://www.w3schools.com/css/css3_variables_javascript.asp"] applied_in: [] github_commit: "" --- # [[CSS Variables and JavaScript]] ## 🎯 ν•œ 쀄 톡찰 (One-line insight) Because CSS variables (custom properties) have access to the DOM, JavaScript can read them with `getComputedStyle().getPropertyValue()` and overwrite them at runtime with `element.style.setProperty()`. [S1] ## 🧠 핡심 κ°œλ… (Core concepts) - **DOM access** β€” CSS variables have access to the DOM, which means you can change them with JavaScript. [S1] - **Reading a variable** β€” get the computed styles for an element with `getComputedStyle()`, then read a specific variable with `.getPropertyValue('--name')`. [S1] - **Setting a variable** β€” set a variable's value with `element.style.setProperty('--name', 'value')`. [S1] - **Root target** β€” the example targets the `:root` element via `document.querySelector(':root')`, so the changed variable affects everything that references it. [S1] ## 🧩 μΆ”μΆœλœ νŒ¨ν„΄ (Extracted patterns) - **Get pattern** β€” `getComputedStyle(el).getPropertyValue('--var')` returns the current value of a CSS variable. [S1] - **Set pattern** β€” `el.style.setProperty('--var', newValue)` reassigns a CSS variable at runtime, live-updating every rule that consumes it via `var()`. [S1] - **Single source, dynamic** β€” by mutating one variable on `:root`, JavaScript can re-theme the page without touching individual rules. [S1] ## πŸ“– μ„ΈλΆ€ λ‚΄μš© (Details) **Change Variables With JavaScript** [S1] CSS variables have access to the DOM, which means that you can change them with JavaScript. The page presents an example of a script that displays and changes the `--primary-bg-color` variable from the example used in the previous pages. (The page notes you do not need to be familiar with JavaScript yet, and links to the W3Schools JavaScript Tutorial.) [S1] The example script: [S1] ```javascript ``` **CSS `var()` function reference** [S1] | Function | Description | |----------|-------------| | `var()` | Inserts the value of a CSS variable | ## πŸ› οΈ 적용 사둀 (Applied in summary) The page's own applied example is a getter/setter script pair: `myFunction_get()` alerts the current `--primary-bg-color` value, and `myFunction_set()` reassigns it to `green` on the `:root` element. No external project/commit applications found in the source. ## πŸ’» μ½”λ“œ νŒ¨ν„΄ (Code patterns) Read a CSS variable (language: JavaScript): ```javascript var r = document.querySelector(':root'); var rs = getComputedStyle(r); var value = rs.getPropertyValue('--primary-bg-color'); ``` Set a CSS variable (language: JavaScript): ```javascript var r = document.querySelector(':root'); r.style.setProperty('--primary-bg-color', 'green'); ``` ## βš–οΈ λͺ¨μˆœ 및 μ—…λ°μ΄νŠΈ (Contradictions & updates) No contradictions found in the source. ## βœ… 검증 μƒνƒœ 및 신뒰도 - **μƒνƒœ:** draft - **검증 단계:** conceptual (μ‹€μ œ 적용 사둀 발견 μ‹œ applied/validated둜 승격 κ°€λŠ₯) - **좜처 신뒰도:** B (W3Schools β€” widely used educational reference, not a primary standards body) - **μ‹ λ’° 점수:** 0.88 - **쀑볡 검사 κ²°κ³Ό:** μ‹ κ·œ 생성 (New discovery) ## πŸ”— 지식 κ·Έλž˜ν”„ (Knowledge Graph) - **μƒμœ„/루트:** [[CSS Tutorial]] - **κ΄€λ ¨ κ°œλ…:** [[CSS Variables]], [[CSS Variables in Media Queries]], [[CSS @property]], [[CSS var Function]] - **μ°Έμ‘° λ§₯락:** Referenced when a page needs to read or mutate CSS custom properties dynamically from JavaScript (e.g. theme switching). ## πŸ“š 좜처 (Sources) - [S1] W3Schools β€” CSS Variables and JavaScript β€” https://www.w3schools.com/css/css3_variables_javascript.asp ## πŸ“ λ³€κ²½ 이λ ₯ (Change history) - 2026-06-23: Initial draft synthesized from the W3Schools "CSS Variables and JavaScript" page (Astra wiki-curation, P-Reinforce v3.1 format).