---
title: "Explain the HTML DOM Style Object in JavaScript"  
description: "here's we will learn about the HTML DOM Style Object in JavaScript with proper example."  
author: "Ashutosh Patel"  
published: 2025-02-17  
updated: 2025-02-17  
canonical: https://www.mindstick.com/articles/338562/explain-the-html-dom-style-object-in-javascript  
category: "javascript"  
tags: ["javascript", "javascript object"]  
reading_time: 3 minutes  

---

# Explain the HTML DOM Style Object in JavaScript

The style object in JavaScript allows you to dynamically change the CSS styles of [HTML elements](https://answers.mindstick.com/qa/93760/how-to-find-the-html-elements-by-using-javascript) using the `.style` [property](https://www.mindstick.com/articles/198559/an-ownership-of-property-in-hua-hin). It provides access to both inline styles and computed [styles of an element](https://www.mindstick.com/forum/158691/how-can-you-change-the-css-styles-of-an-element-using-jquery).

#### How to Access the Style Object?

**Using** `element.style` **(Inline Styles)**

- The `.style` property is used to **set or get** inline CSS styles.

```html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
<title>Style Object Example</title>
<style>
       .highlight { color: red; font-weight: bold; }
   </style>
</head>
<body>
<p id="myDiv" class="active">This is the Para 1.</p>
<script>

let myDiv = document.getElementById("myDiv");
myDiv.style.color = "red"; // Changes text color to red
myDiv.style.backgroundColor = "yellow"; // Sets background color
myDiv.style.fontSize = "20px"; // Sets font size

</script>
</body>
</html>
```

**Using** `window.getComputedStyle(element)` **(Computed Styles)**

- Retrieves the **actual (computed) styles** applied to an element, even styles set by [external](https://answers.mindstick.com/qa/31963/which-program-has-been-launched-by-the-union-ministry-of-external-affairs-mea-to-bring-indian-foreign-policy-to-students-across-the-country) stylesheets.

```html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
<title>Style Object Example</title>
<style>
       .highlight { color: red; font-weight: bold; }
   </style>
</head>
<body>
<p id="myDiv" class="active">This is the Para 1.</p>
<script>

let myDiv = document.getElementById("myDiv");
myDiv.style.color = "red"; // Changes text color to red
myDiv.style.backgroundColor = "yellow"; // Sets background color
myDiv.style.fontSize = "20px"; // Sets font size
let computedStyle = window.getComputedStyle(myDiv);
console.log(computedStyle.color); // Returns the actual computed color (rgb(255, 0, 0))

</script>
</body>
</html>
```

#### Properties of the Style Object

The `.style` object provides access to various CSS [properties](https://www.mindstick.com/blog/673/properties) using JavaScript.

| **JavaScript Property** | **CSS Equivalent** |
| --- | --- |
| color | color |
| backgroundColor | [background](https://www.mindstick.com/articles/65/how-to-change-background-color-of-tab-control-in-c-sharp)-color |
| fontSize | font-size |
| border | border |
| margin | margin |
| padding | padding |
| display | display |
| [visibility](https://www.mindstick.com/blog/301300/how-to-improve-your-visibility-in-zero-click-search-results) | visibility |
| opacity | opacity |

#### Methods of the Style Object

`element.style.setProperty()` **– Set a CSS Property**

It allows setting styles using CSS syntax.

```javascript
myDiv.style.setProperty("background-color", "blue");
```

`element.style.getPropertyValue()` **– Get a CSS Property**

It retrieves the value of a specific CSS property.

```javascript
let bgColor = myDiv.style.getPropertyValue("background-color");
console.log(bgColor); // Outputs: blue
```

`element.style.removeProperty()` **– Remove a CSS Property**

It deletes a specific style property.

```javascript
myDiv.style.removeProperty("background-color");
```

####

#### Example: Change Styles Dynamically

```html
<!DOCTYPE html>
<html lang="en">
<head>
   <title>Style Object Example</title>
   <style>
       #myDiv {
           width: 200px;
           height: 100px;
           background-color: lightgray;
           text-align: center;
           padding: 20px;
       }
   </style>
</head>
<body>
   <div id="myDiv">Hello, World!</div>
   <button onclick="changeStyle()">Change Style</button>
   <script>
       function changeStyle() {
           let myDiv = document.getElementById("myDiv");
           myDiv.style.backgroundColor = "purple";
           myDiv.style.color = "white";
           myDiv.style.fontSize = "24px";
           myDiv.style.border = "2px solid black";
       }
   </script>
</body>
</html>
```

## Conclusion

- Use `.style` to dynamically change inline styles.
- Use `getComputedStyle()` to retrieve the last applied styles from CSS files.
- Useful for [interactive](https://answers.mindstick.com/qa/38931/who-has-won-the-2016-fifa-interactive-world-cup-fiwc) UI effects, [animations](https://www.mindstick.com/forum/160680/how-to-use-css-transitions-and-animations-in-the-webpage), and dynamic styling.

Also, Read: [Explain the HTML DOMTokenList in JavaScript](https://www.mindstick.com/blog/305273/explain-the-html-domtokenlist-in-javascript)

---

Original Source: https://www.mindstick.com/articles/338562/explain-the-html-dom-style-object-in-javascript

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
