Hackforge Academy

Category: javascript

dom changing styles and classes

Published on 30 Sep 2026

Explanation

The style property allows JavaScript to modify inline CSS styles directly.

Code:

const box = document.getElementById('box');

box.style.backgroundColor = 'blue';
box.style.padding = '20px';

Explanation

classList.add() adds a CSS class to an element.

Code:

const box = document.getElementById('box');

box.classList.add('active');

Explanation

classList.remove() removes a CSS class from an element.

Code:

box.classList.remove('active');

Explanation

classList.toggle() adds a class if it is absent and removes it if it is present.

Code:

button.addEventListener('click', () => {
  box.classList.toggle('dark');
});

Explanation

Using CSS classes is generally cleaner for reusable styling than assigning many inline style properties from JavaScript.

Code:

.highlight {
  background: yellow;
  font-weight: bold;
}

// JavaScript
box.classList.add('highlight');

πŸš€ Learn Spring Boot with real-world projects

πŸ’‘ Build REST APIs step by step

🧠 Improve backend development skills

🎯 Get career-ready practical training

Join Our Free WhatsApp Community

Direct access to niche-specific mentors and peers on WhatsApp.

🐍

Python Community

Discuss Django, FastAPI, AI integration, and automation scripts with 15k+ developers.

Join Python Community
βš›οΈ

React Community

Master Next.js, Framer Motion, and State Management. Share your latest UI components.

Join React Community
β˜•

Java Community

Deep dives into Spring Boot, Microservices architecture, and high-performance backend ops.

Join Java Community