--- id: css-variables-in-media-queries title: "CSS Variables in Media Queries" category: "Frontend" status: "draft" verification_status: "conceptual" canonical_id: "" aliases: ["CSS custom properties media queries", "responsive CSS variables", "override variable in @media", "--fontsize media query", "variable breakpoints"] 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", "media-queries", "responsive"] raw_sources: ["https://www.w3schools.com/css/css3_variables_mediaqueries.asp"] applied_in: [] github_commit: "" --- # [[CSS Variables in Media Queries]] ## 🎯 ν•œ 쀄 톡찰 (One-line insight) CSS variables can be reassigned inside `@media` rules, so a single `var()`-driven design responds to screen size simply by overriding the variable's value at a breakpoint. [S1] ## 🧠 핡심 κ°œλ… (Core concepts) - **Media queries enable responsive styling** β€” they allow you to set different styles for different devices/screen sizes. [S1] - **Variables are overridable per breakpoint** β€” you can change a variable's value inside a media query, and every rule that reads it via `var()` updates automatically. [S1] - **Local vs. root scope** β€” a variable defined on `.container` (e.g. `--fontsize`) can be overridden for `.container` inside a media query, while a variable on `:root` (e.g. `--primary-bg-color`) can be overridden globally inside the same query. [S1] ## 🧩 μΆ”μΆœλœ νŒ¨ν„΄ (Extracted patterns) - **Breakpoint override pattern** β€” redefine the variable inside `@media screen and (min-width: …)` to switch its value above that width. [S1] - **Single-property responsiveness** β€” changing `--fontsize` from `20px` to `40px` at `min-width: 450px` makes the font-size responsive without touching the `font-size` declaration itself. [S1] - **Multi-variable override** β€” multiple variables (a local `--fontsize` on `.container` and a global `--primary-bg-color` on `:root`) can be overridden together within one media query. [S1] ## πŸ“– μ„ΈλΆ€ λ‚΄μš© (Details) Media queries let you set different style values for different devices. The page demonstrates overriding a CSS variable's value based on screen width: the `--fontsize` variable is `20px` on small screens and becomes `40px` on screens that are `450px` or wider. [S1] **Example 1 β€” override a single variable at a breakpoint** [S1] ```css :root { --primary-bg-color: #1e90ff; --primary-color: #ffffff; } body { background-color: var(--primary-bg-color); } .container { --fontsize: 20px; color: var(--primary-bg-color); background-color: var(--primary-color); padding: 15px; font-size: var(--fontsize); } .container h2 { border-bottom: 2px solid var(--primary-bg-color); } @media screen and (min-width: 450px) { .container { --fontsize: 40px; } } ``` **Example 2 β€” override multiple variables in the media query** [S1] A second example modifies multiple variables inside the media query, additionally changing `--primary-bg-color` from the blue hex value to `lightblue` at the larger breakpoint: [S1] ```css :root { --primary-bg-color: #1e90ff; --primary-color: #ffffff; } body { background-color: var(--primary-bg-color); } .container { --fontsize: 20px; color: var(--primary-bg-color); background-color: var(--primary-color); padding: 15px; font-size: var(--fontsize); } .container h2 { border-bottom: 2px solid var(--primary-bg-color); } @media screen and (min-width: 450px) { .container { --fontsize: 40px; } :root { --primary-bg-color: lightblue; } } ``` **CSS `var()` function reference** [S1] | Function | Description | |----------|-------------| | `var()` | Inserts the value of a CSS variable | ## πŸ› οΈ 적용 사둀 (Applied in summary) The page's applied examples are a responsive card (`.container`): font size scales from 20px to 40px at the 450px breakpoint (Example 1), and in Example 2 the global background color also flips to `lightblue` above that width. No external project/commit applications found in the source. ## πŸ’» μ½”λ“œ νŒ¨ν„΄ (Code patterns) Override a variable at a breakpoint (language: CSS): ```css .container { --fontsize: 20px; font-size: var(--fontsize); } @media screen and (min-width: 450px) { .container { --fontsize: 40px; } } ``` ## βš–οΈ λͺ¨μˆœ 및 μ—…λ°μ΄νŠΈ (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 and JavaScript]], [[CSS Media Queries]], [[CSS @property]] - **μ°Έμ‘° λ§₯락:** Referenced when building responsive designs whose tokens (font sizes, colors) change at viewport breakpoints via custom properties. ## πŸ“š 좜처 (Sources) - [S1] W3Schools β€” CSS Variables in Media Queries β€” https://www.w3schools.com/css/css3_variables_mediaqueries.asp ## πŸ“ λ³€κ²½ 이λ ₯ (Change history) - 2026-06-23: Initial draft synthesized from the W3Schools "CSS Variables in Media Queries" page (Astra wiki-curation, P-Reinforce v3.1 format).