Styles and Classes in Javascript

Styles and Classes in Javascript

Before we get into JavaScript’s ways of dealing with styles and classes – here’s an important rule. Hopefully it’s obvious enough, but we still have to mention it.

Before we get into JavaScript’s ways of dealing with styles and classes – here’s an important rule. Hopefully it’s obvious enough, but we still have to mention it.

There are generally two ways to style an element:

  1. Create a class in CSS and add it: <div class="...">
  2. Write properties directly into style<div style="...">.

JavaScript can modify both classes and style properties.

We should always prefer CSS classes to style. The latter should only be used if classes “can’t handle it”.

For example, style is acceptable if we calculate coordinates of an element dynamically and want to set them from JavaScript, like this:

let top = /* complex calculations */;
let left = /* complex calculations */; = left; // e.g '123px', calculated at run-time = top; // e.g '456px'

For other cases, like making the text red, adding a background icon – describe that in CSS and then add the class (JavaScript can do that). That’s more flexible and easier to support.

className and classList

Changing a class is one of the most often used actions in scripts.

In the ancient time, there was a limitation in JavaScript: a reserved word like "class" could not be an object property. That limitation does not exist now, but at that time it was impossible to have a "class" property, like elem.class.

So for classes the similar-looking property "className" was introduced: the elem.className corresponds to the "class"attribute.

For instance:

<body class="main page">

  alert(document.body.className); // main page

If we assign something to elem.className, it replaces the whole string of classes. Sometimes that’s what we need, but often we want to add/remove a single class.

There’s another property for that: elem.classList.

The elem.classList is a special object with methods to add/remove/toggle a single class.

For instance:

<body class="main page">
  // add a class

  alert(document.body.className); // main page article  </script> </body>

So we can operate both on the full class string using className or on individual classes using classList. What we choose depends on our needs.

Methods of classList:

  • elem.classList.add/remove("class") – adds/removes the class.
  • elem.classList.toggle("class") – adds the class if it doesn’t exist, otherwise removes it.
  • elem.classList.contains("class") – checks for the given class, returns true/false.

Besides, classList is iterable, so we can list all classes with for..of, like this:

<body class="main page">
  for (let name of document.body.classList) {
   alert(name); // main, and then page

Element style

The property is an object that corresponds to what’s written in the "style" attribute. Setting"100px" works the same as if we had in the attribute style a string width:100px.

For multi-word property the camelCase is used:

background-color  =>
z-index           =>
border-left-width =>

For instance: = prompt('background color?', 'green');

Prefixed properties

Browser-prefixed properties like -moz-border-radius-webkit-border-radius also follow the same rule: a dash means upper case.

For instance: = '5px'; = '5px';

Resetting the style property

Sometimes we want to assign a style property, and later remove it.

For instance, to hide an element, we can set = "none".

Then later we may want to remove the style.display as if it were not set. Instead of delete we should assign an empty string to it: = "".

// if we run this code, the <body> will blink = "none"; // hide

setTimeout(() => = "", 1000); // back to normal

If we set style.display to an empty string, then the browser applies CSS classes and its built-in styles normally, as if there were no such style.display property at all.

Full rewrite with style.cssText

Normally, we use style.* to assign individual style properties. We can’t set the full style like"color: red; width: 100px", because is an object, and it’s read-only.

To set the full style as a string, there’s a special property style.cssText:

<div id="div">Button</div>

<script> // we can set special style flags like "important" here red !important; background-color: yellow; width: 100px; text-align: center; ; alert(; </script>

This property is rarely used, because such assignment removes all existing styles: it does not add, but replaces them. May occasionally delete something needed. But we can safely use it for new elements, when we know we won’t delete an existing style.

The same can be accomplished by setting an attribute: div.setAttribute('style', 'color: red...').

Mind the units

Don’t forget to add CSS units to values.

For instance, we should not set to 10, but rather to 10px. Otherwise it wouldn’t work:

  // doesn't work! = 20;
  alert(; // '' (empty string, the assignment is ignored)

  // now add the CSS unit (px) - and it works = '20px';   alert(; // 20px

  alert(; // 20px   alert(; // 20px  </script> </body>

Please note: the browser “unpacks” the property style.margin in the last lines and infers style.marginLeft and style.marginTop from it.

Computed styles: getComputedStyle

So, modifying a style is easy. But how to read it?

For instance, we want to know the size, margins, the color of an element. How to do it?

The style property operates only on the value of the "style" attribute, without any CSS cascade.

So we can’t read anything that comes from CSS classes using

For instance, here style doesn’t see the margin:

 <style> body { color: red; margin: 5px } </style>

 The red text  <script>   alert(; // empty   alert(; // empty  </script> </body>

…But what if we need, say, to increase the margin by 20px? We would want the current value of it.

There’s another method for that: getComputedStyle.

The syntax is:

getComputedStyle(element, [pseudo])


Element to read the value for.


A pseudo-element if required, for instance ::before. An empty string or no argument means the element itself.

The result is an object with styles, like, but now with respect to all CSS classes.

For instance:

 <style> body { color: red; margin: 5px } </style>

 <script>   let computedStyle = getComputedStyle(document.body);

  // now we can read the margin and the color from it

  alert( computedStyle.marginTop ); // 5px   alert( computedStyle.color ); // rgb(255, 0, 0)  </script>


Computed and resolved values

There are two concepts in CSS:

computed style value is the value after all CSS rules and CSS inheritance is applied, as the result of the CSS cascade. It can look like height:1em or font-size:125%.

resolved style value is the one finally applied to the element. Values like 1em or 125% are relative. The browser takes the computed value and makes all units fixed and absolute, for instance: height:20px or font-size:16px. For geometry properties resolved values may have a floating point, like width:50.5px.

A long time ago getComputedStyle was created to get computed values, but it turned out that resolved values are much more convenient, and the standard changed.

So nowadays getComputedStyle actually returns the resolved value of the property, usually in px for geometry.

getComputedStyle requires the full property name

We should always ask for the exact property that we want, like paddingLeft or marginTop or borderTopWidth. Otherwise the correct result is not guaranteed.

For instance, if there are properties paddingLeft/paddingTop, then what should we get for getComputedStyle(elem).padding? Nothing, or maybe a “generated” value from known paddings? There’s no standard rule here.

There are other inconsistencies. As an example, some browsers (Chrome) show 10px in the document below, and some of them (Firefox) – do not:

 body {
  margin: 10px;
 let style = getComputedStyle(document.body);
 alert(style.margin); // empty string in Firefox

Styles applied to :visited links are hidden!

Visited links may be colored using :visited CSS pseudoclass.

But getComputedStyle does not give access to that color, because otherwise an arbitrary page could find out whether the user visited a link by creating it on the page and checking the styles.

JavaScript may not see the styles applied by :visited. And also, there’s a limitation in CSS that forbids to apply geometry-changing styles in :visited. That’s to guarantee that there’s no sideway for an evil page to test if a link was visited and hence to break the privacy.


To manage classes, there are two DOM properties:

  • className – the string value, good to manage the whole set of classes.
  • classList – the object with methods add/remove/toggle/contains, good for individual classes.

To change the styles:

  • The style property is an object with camelCased styles. Reading and writing to it has the same meaning as modifying individual properties in the "style" attribute. To see how to apply important and other rare stuff – there’s a list of methods at MDN.
  • The style.cssText property corresponds to the whole "style" attribute, the full string of styles.

To read the resolved styles (with respect to all classes, after all CSS is applied and final values are calculated):

  • The getComputedStyle(elem, [pseudo]) returns the style-like object with them. Read-only.

Hope this tutorial will surely help and you! Thanks For Visiting, Keep Visiting.

☞ The Complete JavaScript Course 2019: Build Real Projects!

☞ Vue JS 2 - The Complete Guide (incl. Vue Router & Vuex)

☞ JavaScript Bootcamp - Build Real World Applications

☞ The Web Developer Bootcamp

☞ New ES2019 Features Every JavaScript Developer Should Know

☞ Best JavaScript Frameworks, Libraries and Tools to Use in 2019

☞ 12 tips for writing clean and scalable JavaScript

☞ 10 JavaScript Charting Libraries for Every Data Visualization Need

Originally published on

javascript css css3 web-development

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Hire CSS Developer

Want to develop a website or re-design using CSS Development? We build a website and we implemented CSS successfully if you are planning to **[Hire CSS Developer]( "Hire CSS Developer")**...

Hire Web Developer

Looking for an attractive & user-friendly web developer?, a leading web, and mobile app development company, offers web developers for hire through flexible engagement models. You can **[Hire Web...

Why Web Development is Important for your Business

With the rapid development in technology, the old ways to do business have changed completely. A lot more advanced and developed ways are ...

Important Reasons to Hire a Professional Web Development Company

    You name the business and I will tell you how web development can help you promote your business. If it is a startup or you seeking some...

Developing for the Mobile-Web: Part 1 — CSS Media Queries

Developing our web pages for the mobile web has never been more important than it is now. I grew up in the early stages of the internet.