Frontend Master

JavaScript Curriculum

Styling via JavaScript
+50 XP

Styling via JavaScript

easy
~25 min·50 XP

The menu cards need hover effects, a dark mode toggle, loading states on the order button, and error states on the form. All of this is style-driven — JavaScript just adds and removes classes.

Styling via JavaScript

JavaScript has two ways to style elements: write inline styles directly via element.style, or toggle CSS classes. Knowing when to use each makes your code cleaner and your styles easier to maintain.

element.style — Direct Property Control

Click properties to see how they apply as inline styles:

style-builder.js
Preview
Espresso
£2.50
// Click properties to apply them
⚠️Inline styles beat almost everything
Styles set via `element.style` are inline styles — they have the highest specificity and override your stylesheet. Use them only for dynamic values that cannot be known in CSS (like a position calculated from a mouse event). For states like active, loading, or error — use classes.

classList.toggle — The Right Way to Style Dynamically

Toggle a CSS class and let your stylesheet do the visual work:

class-toggle.js

Toggle a class on the root element to switch the entire page theme via CSS variables.

classes: page
CSS
.page { --bg: #fff; --text: #333; background: var(--bg); color: var(--text); }
.page.dark { --bg: #0d1117; --text: #c9d1d9; }
JavaScript
const page = document.querySelector('.page')
btn.addEventListener('click', () => {
  page.classList.toggle('dark')
})
💡Keep styles in CSS, logic in JS
The pattern: define all visual states in your CSS (.card.selected, .btn.loading, .input.error). JavaScript only adds and removes class names — it never touches colour values or pixel measurements. This keeps your codebase clean and your styles easy to update.

getComputedStyle — Reading the Final Result

element.style only shows inline styles you set. getComputedStyle shows the real final value after all CSS is applied:

computed-style.js

A card div — display, padding, border-radius from your stylesheet.

Computed styles
displayflex
flex-directioncolumn
background-colorrgb(255, 255, 255)
border-radius10px
padding20px
box-shadow0 2px 8px rgba(0,0,0,0.08)
width280px
How to read it
const el = document.querySelector('div.card')
const styles = getComputedStyle(el)

// Read any property:
styles.getPropertyValue('font-size')
// → 'flex'
el.style — only inline styles you set via JS
getComputedStyle(el) — final resolved values after all CSS applied
ℹ️getComputedStyle vs element.style
If you set `el.style.fontSize = '1.5rem'` then `el.style.fontSize` → `'1.5rem'`. But if the font-size comes from your stylesheet, `el.style.fontSize` → `''` (empty). Use `getComputedStyle(el).getPropertyValue('font-size')` to always get the final value.

Your Challenge

Wire a dark mode toggle button that calls document.body.classList.toggle('dark'). In your CSS, define body.dark with overridden colours. Then use getComputedStyle(document.body).getPropertyValue('background-color') to confirm the change in the console.

Challenge

Style the order button using both approaches: first set el.style.backgroundColor directly, then reset it and use classList.toggle('active') instead. Which feels cleaner? Then use getComputedStyle to read the button's final font-size.

element.styleclassListclassList.togglegetComputedStyleinline-stylesCSS-classesstyle-separationdark-mode
Styling via JavaScript | Nexus Learn