HTML Element .style
| Since: | DOM Level 2(2000) |
|---|
A property for manipulating the inline styles of an HTML element. You can get and set CSS properties through this object.
Syntax
element.style.propertyName = "value";
var value = element.style.propertyName;
element.style.cssText = "property: value; property: value;";
var text = element.style.cssText;
// Use setProperty() / getPropertyValue() with CSS-notation property names.
element.style.setProperty("property-name", "value");
var value = element.style.getPropertyValue("property-name");
Properties and Methods
| Property / Method | Description |
|---|---|
| style.propertyName | Gets or sets a CSS property directly using camelCase. Hyphenated property names must be converted to camelCase — for example, background-color becomes backgroundColor. |
| style.cssText | Gets or sets the entire inline style as a string. When setting, all existing inline styles are overwritten. |
| style.setProperty("name", "value") | Sets a value using a CSS-notation property name (hyphens are allowed as-is). Pass "important" as the third argument to add !important. |
| style.getPropertyValue("name") | Gets a value using a CSS-notation property name. Returns an empty string if the property is not set in the inline style. |
| style.removeProperty("name") | Removes the specified CSS property from the inline style. |
Sample Code
<div id="card" style="padding: 10px;">product_x — zone_1</div> <p id="status">item_a</p> <button id="btn">Apply Style</button>
The following example shows how this works in practice:
var card = document.getElementById("card");
// Pattern 1: Set styles directly by property name (convert hyphens to camelCase)
card.style.backgroundColor = "#1a1a2e";
card.style.color = "white";
card.style.fontSize = "18px";
card.style.borderRadius = "8px";
// Pattern 2: Get an inline style value
console.log(card.style.backgroundColor); // Outputs "rgb(26, 26, 46)"
// Pattern 3: Set a property using CSS notation with setProperty()
card.style.setProperty("border", "2px solid #6200ea");
// Pattern 4: Get a property using CSS notation with getPropertyValue()
console.log(card.style.getPropertyValue("border")); // Outputs "2px solid rgb(98, 0, 234)"
// Pattern 5: Set multiple styles at once with cssText (overwrites all existing inline styles)
var btn = document.getElementById("btn");
btn.addEventListener("click", function() {
card.style.cssText = "background-color: #6200ea; color: white; padding: 20px; border-radius: 8px;";
});
// Pattern 6: Remove a property with removeProperty()
card.style.removeProperty("border");
console.log(card.style.getPropertyValue("border")); // Returns an empty string.
Overview
element.style is a property for manipulating the inline styles of an HTML element. Reading and writing via style.propertyName is the most straightforward approach, but hyphenated CSS property names must be converted to camelCase — for example, background-color becomes backgroundColor, and font-size becomes fontSize.
element.style only reflects inline styles. Styles applied via a CSS file or a <style> tag cannot be retrieved this way. To get the final computed style, use window.getComputedStyle().
Manipulating inline styles is convenient for debugging or temporary changes, but if you are simply toggling the appearance of an element, swapping classes with element.classList is generally easier to maintain.
Browser Compatibility
2 or earlier ×
4.5 or earlier ×
7 or earlier ×
Android Browser
37+ ○
4 or earlier ×
Chrome Android
36+ ○
17 or earlier ×
Firefox Android
79+ ○
3 or earlier ×If you find any errors or copyright issues, please contact us.