HTMLElement.prototype.style
Gets or sets the inline style of an element as a CSSStyleDeclaration object
Syntax
element.style.property = valueParameters
| Parameter | Type | Description |
|---|---|---|
| property | string | The CSS property to set |
| value | string | The value for the CSS property |
Return Value
A CSSStyleDeclaration object representing the element's inline style
Examples
const el = document.querySelector('.box') as HTMLElement
el.style.backgroundColor = 'blue'
el.style.padding = '20px'function fadeIn(el: HTMLElement, ms: number) {
el.style.opacity = '0'
el.style.transition = `opacity ${ms}ms`
requestAnimationFrame(() => { el.style.opacity = '1' })
}const card = document.querySelector('.card') as HTMLElement
Object.assign(card.style, {
border: '1px solid #ccc',
borderRadius: '8px',
padding: '16px'
})Understanding HTMLElement.prototype.style
The HTMLElement.prototype.style method in JavaScript gets or sets the inline style of an element as a CSSStyleDeclaration object. It belongs to the HTMLElement object and is one of the most widely used methods for working with htmlelement values in modern JavaScript and TypeScript applications.
The method signature is element.style.property = value. It accepts 2 parameters: property, value. When called, it returns a cssstyledeclaration object representing the element's inline style. Understanding when and how to use style() helps you write more expressive, readable code.
Common use cases for HTMLElement.prototype.style include data transformation, input validation, API response processing, and building reusable utility functions. It works well alongside related methods like dom-classlist-add, dom-setattribute, dom-getcomputedstyle, enabling you to chain operations together for complex data manipulation pipelines.
Browser support for HTMLElement.prototype.style is excellent across all modern browsers including Chrome, Firefox, Safari, and Edge. It is also fully supported in Node.js and Deno. For older environments, transpilation with Babel or a polyfill may be needed.
Browser Compatibility
Supported in all modern browsers (Chrome, Firefox, Safari, Edge) and Node.js. Part of the ECMAScript standard.
Related Methods
Element.classList.addAdds the given tokens to the element's class list
Element.prototype.setAttributeSets the value of an attribute on the specified element, adding it if it does not already exist
window.getComputedStyleReturns an object containing the values of all CSS properties of an element after applying active stylesheets and resolving computations
More HTMLElement Methods
Other methods in the HTMLElement object
Related Tools
More HTMLElement Methods
Explore JavaScript Methods
Browse our complete reference of 410 JavaScript methods with syntax, examples, and explanations.