HOWTO · JavaScript

How to Change CSS Property Using JavaScript

Learn how to change one CSS property with JavaScript, apply a CSS class, and choose a selector for one or more elements.

On this page

To change a CSS property with JavaScript, select the target element and set a property on its style object. For a group of related styles, add a CSS class instead. Use an ID or CSS selector for one element, and a class-based collection when you need to update every match. The browser provides the document object; jQuery is optional.

Change one CSS property with element.style

Use element.style when you need to set one inline style. CSS property names with hyphens use camelCase in JavaScript: border-color becomes borderColor. This small fixture makes the selection and assignment pattern runnable in a JavaScript environment; in a page, document is the browser’s actual document.

Run this example:

const input = { style: {} };
const document = {
  getElementById(id) {
    return id === "email" ? input : null;
  }
};

const element = document.getElementById("email");
if (element) {
  element.style.borderColor = "red";
}

The style assignment changes the element’s inline style. It does not edit a stylesheet rule, and it does not remove other styles. In browser code, the corresponding selection is document.getElementById("email"); check the returned value before reading or changing its properties.

Apply multiple styles with a CSS class

A class is easier to maintain when a state needs several coordinated declarations. Define a class in your stylesheet, then use classList.add() to apply it and classList.remove() to clear it. For example, an input-error class can set both a red border and red text. classList preserves other classes on the element.

Assigning element.className = "input-error" also changes its class attribute, but replaces the entire class string. Existing classes such as layout or validation classes are lost unless included in the new string. Use classList for individual additions and removals; use className when you intentionally want to replace all classes.

Select elements by class with getElementsByClassName

document.getElementsByClassName("input-blue-border") returns a live HTMLCollection of matching elements, not an array. It updates as matching elements enter or leave the document. To change every current match, iterate over the collection with a for...of loop and update each element’s style or class list. If you need array methods, convert the collection with Array.from(...) first.

The method can also be called on an element to find matching descendants within that element. Its argument is a class name without the leading dot; use .input-blue-border only with a CSS-selector API such as querySelector().

Select one element with getElementById or querySelector

document.getElementById("email") looks up an element by its ID and returns the matching element or null. IDs should be unique in the document; duplicate IDs are invalid markup, so do not rely on which duplicate is returned.

document.querySelector(".input-blue-border") accepts a CSS selector and returns the first matching element, or null if none matches. It can select by ID (#email), class (.input-blue-border), or tag (input). Despite accepting a selector that could match many elements, querySelector() returns only the first match. Check for null before changing its style or classes.

Choose a selector and handle missing or multiple matches

Choose the selector based on the target and the number of elements to update: use getElementById for one element with a unique ID, or querySelector() when a CSS selector is more convenient and only the first match is needed. Use getElementsByClassName when every element with a class should change. For one style declaration, set element.style; for a named state with multiple declarations, toggle a class.

A selector can fail when the element is not in the document yet, the ID or class is misspelled, or the markup has not loaded. Run selection after the relevant markup exists, or guard the returned value before accessing style or classList. For example, the browser pattern is const element = document.querySelector(".notice"); if (element) element.classList.add("is-visible");. To update only one item from a class collection, choose it deliberately; to update all items, iterate over the collection.

Summary

Select the intended element first. Set a single inline declaration with element.style, or use classList to apply a group of styles while retaining existing classes. getElementById targets an ID, querySelector() returns the first CSS-selector match, and getElementsByClassName gives a live collection for updating every class match. Guard a single-element result before using it, and iterate when the task applies to multiple elements.