--- id: javascript-dom-changing-css title: "JavaScript DOM Changing CSS" category: "Frontend" status: "draft" verification_status: "conceptual" canonical_id: "" aliases: ["changing CSS", "style property", "element.style", "DOM styling", "style object"] 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: ["javascript", "js", "web", "frontend", "w3schools", "dom"] raw_sources: ["https://www.w3schools.com/js/js_htmldom_css.asp"] applied_in: [] github_commit: "" --- # [[JavaScript DOM Changing CSS]] ## π― ν μ€ ν΅μ°° (One-line insight) The HTML DOM lets JavaScript change the style of any element by assigning to its `style.property`, and events can trigger those style changes dynamically. [S1] ## π§ ν΅μ¬ κ°λ (Core concepts) - **Style via the style object** β change an element's CSS by setting a property on its `style` object. [S1] - **Events drive style changes** β the HTML DOM allows you to execute code when an event occurs; events are generated by the browser when "things happen" to HTML elements, such as clicks, page loads, and input-field changes. [S1] - **Reference exists** β the HTML DOM Style Object Reference documents all properties that can be set. [S1] ## π§© μΆμΆλ ν¨ν΄ (Extracted patterns) - **`document.getElementById(id).style.property = new style`** β the canonical style-change syntax. [S1] - **Inline event handler β style change** β wire an `onclick` attribute that mutates `style` on a target element. [S1] - **Visibility toggle** β switch `style.visibility` between `'hidden'` and `'visible'` to hide or show. [S1] ## π μΈλΆ λ΄μ© (Details) **Changing HTML Style** [S1] The syntax for changing the style of an HTML element is: [S1] ```javascript document.getElementById(id).style.property = new style ``` Change the color of a paragraph: [S1] ```html
Hello World!
``` **Using Events** [S1] The HTML DOM allows you to execute code when an event occurs. Events are generated by the browser when "things happen" to HTML elements, such as clicks, page loads, and changes to input fields. The following example changes a heading's color on click: [S1] ```html