Skip to main content Accessibility Feedback


Get and set inline styles for an element.

<p id="sandwich" style="background-color: green; color: white;">
let sandwich = document.querySelector('#sandwich');

// Get a style
// If this style is not set as an inline style directly on the element, it returns an empty string
let bgColor =; // this will return "green"
let fontWeight =; // this will return ""

// Set the background-color style property = 'purple';

You can also get and set a string representation of the entire inline style property on the element itself with the property.

// Get the styles on an element
// returns "background-color: green; color: white;"
let styles =;

// Completely replace the inline styles on an element = 'font-size: 2em; font-weight: bold;';

// Add additional styles += 'color: purple;';