Lesson 7 of 8

One CSS class

Goal

One change instead of a hundred and fifty

01

Explanation

If you restyle fifty cards with card.style inside a loop, you're making a hundred and fifty changes on every click, and you leave an inline style on every card that your CSS rules can only override with !important.

Adding a single class to the container does the same job with one change. The browser recalculates styles once, and the look stays in your CSS, where transitions, media queries and dark mode can work with it.

A common mistake is mixing reads and writes inside a loop. Reading offsetHeight after every change forces the browser to recalculate the layout right away on every pass. The right way is to read everything you need first and then make all the changes, or to write just once, as in this lesson.

02

Your task

Move the styles into a #list.compact .card rule, then make the #compact button toggle the compact class on #list alone, without touching the cards, and keep aria-pressed up to date.

Checks

5 checks
  1. A click changes only the list, not the cards
  2. Each click adds or removes the compact class on the list
  3. The class shrinks the cards' padding through CSS
  4. The cards have no inline styles
  5. aria-pressed follows the state
03

Hints

0/3
Track lessons0 of 8 done

Find out where your page really loses time, and how to win it back by adding elements in one go, making fewer calls and never computing the same thing twice.

Advanced level, 8 lessons, 1.5 hours in total.

index.html
Preview
index.html100%
–/5

Press “Run and check” to grade your code.

5 checks listed under “Your task”.

Track lessons0 of 8 done

Find out where your page really loses time, and how to win it back by adding elements in one go, making fewer calls and never computing the same thing twice.

Advanced level, 8 lessons, 1.5 hours in total.