JavaScript DOM manipulating styles
Manipulating styles in JavaScript is an essential aspect of dynamically updating the appearance of HTML elements. The style
property is used to get or set the inline styles of an element. This property provides a direct way to access and modify CSS styles via JavaScript.
Using the style
Property
Definition
style
: Thestyle
property of an element represents aCSSStyleDeclaration
object, which provides access to the inline styles of the element.
Syntax
Accessing Styles
const styleValue = element.style.propertyName;
Setting Styles
element.style.propertyName = 'value';
Here,
propertyName
is the name of the CSS property (in camelCase for JavaScript), andvalue
is the value you want to set.
Usage
Setting Inline Styles
<div id="myDiv">Content</div> <script> const div = document.getElementById('myDiv'); div.style.color = 'red'; // Sets the text color to red div.style.backgroundColor = 'yellow'; // Sets the background color to yellow div.style.fontSize = '20px'; // Sets the font size to 20 pixels </script>
This example sets various styles on the
<div>
element.Accessing Inline Styles
<div id="myDiv" style="color: red; background-color: yellow;">Content</div> <script> const div = document.getElementById('myDiv'); console.log(div.style.color); // Logs: 'red' console.log(div.style.backgroundColor); // Logs: 'yellow' </script>
This retrieves the value of the inline
color
andbackground-color
styles.Setting Multiple Styles
<div id="myDiv">Content</div> <script> const div = document.getElementById('myDiv'); div.style.cssText = 'color: blue; background-color: lightgreen; font-size: 16px;'; </script>
Using
cssText
allows you to set multiple styles at once by providing a string with CSS declarations.
Considerations
CSS Property Names: When using the
style
property, CSS property names are written in camelCase rather than kebab-case. For example, usebackgroundColor
instead ofbackground-color
.Inline Styles vs. CSS Classes: Modifying styles directly via the
style
property affects only inline styles. If you want to apply a set of styles defined in CSS classes, useclassList
methods to add or remove classes:// Adding a class div.classList.add('myClass'); // Removing a class div.classList.remove('myClass'); // Toggling a class div.classList.toggle('myClass');
Inline Styles Precedence: Inline styles set via the
style
property override styles defined in external or internal stylesheets due to their higher specificity.Performance Considerations: Frequent changes to inline styles can impact performance, especially in complex applications with many DOM elements. Consider using CSS classes for batch updates or animations.
Best Practices
Use CSS Classes for Complex Styling: For more complex or reusable styles, define them in CSS classes and use
classList
methods to toggle classes. This approach keeps your JavaScript cleaner and leverages the browser’s optimized CSS rendering.Direct Styling for Dynamic Changes: Use the
style
property for quick, dynamic changes that need to be made directly through JavaScript, such as adjusting styles based on user interactions or other dynamic conditions.Avoid Overusing Inline Styles: Rely on inline styles sparingly to avoid mixing style and behavior in your code. Inline styles should generally be used for cases where styles need to change dynamically and are not easily handled by classes.