Styling in Solid — SolidJS

class="card" for fixed classes. classList={{ "sold-out": stock() === 0 }} for classes that switch on and off, one classList.toggle per class, the condition…

Beauty Is a Wound is sold out, and its card has to look different: grey, with the price struck through. When stock arrives, the card goes back to normal. Nadia doesn't repaint the card. Every card wears the same frame, and she only clips a grey sold-out sticker onto that one, then unclips it when stock arrives.

Solid gives you three props for this kind of look: class for CSS classes that stay (the frame), classList for classes that switch on and off with a condition, and style for computed CSS values. classList is that set of sticker switches: one switch per class name, each following one condition.

class: classes that stay

<div class="card">...</div>

Same as in HTML, and Solid writes it straight into the template. The value may also be computed: class={stock() === 0 ? "card sold-out" : "card"}. Every time the result changes, Solid replaces the whole attribute: el.className = "card sold-out". For one condition that's still tidy. With two or three, the string gets hard to read.

classList: classes that switch on and off

A Nadia's Bookshop book card, with its own stock:

import { createSignal } from "solid-js"

function BookCard(props) {
  const [stock, setStock] = createSignal(props.initialStock)

  return (
    <div class="card" classList={{ "sold-out": stock() === 0, "low-stock": stock() > 0 && stock() < 3 }}>
      <h3>{props.title}</h3>
      <p class="price" style={{ "text-decoration": stock() === 0 ? "line-through" : "none" }}>${props.price}</p>
      <p>Stock: {stock()}</p>
      <button onClick={() => setStock(s => s - 1)} disabled={stock() === 0}>Buy 1</button>
    </div>
  )
}

With the CSS .sold-out { opacity: 0.5; filter: grayscale(1) } and .low-stock { border-color: #f59e0b }, the This Earth of Mankind card (starting stock 2) changes like this:

Event stock() class attribute
Page opens 2 card low-stock
Click Buy 1 1 card low-stock
Click Buy 1 again 0 card sold-out, the card turns grey

This is the lesson's exercise: a <div>Content</div> uses classList={{ active: active() }}, the signal starts at true, and a Toggle button runs setActive(v => !v). The first click takes the active class off, the second puts it back.

style: an object with kebab-case keys

style takes an object. Its keys are real CSS property names, kebab-case in quotes, exactly as in a CSS file:

<p style={{ "text-decoration": stock() === 0 ? "line-through" : "none", "font-size": "18px" }}>${props.price}</p>

class and classList on the same element

A card with a computed theme class, plus classList:

<div class={"card " + theme()} classList={{ "sold-out": stock() === 0 }}>

Beauty Is a Wound (stock 0) shows grey. Then the theme changes from light to dark. Take a guess first: is the card still grey or not?

The result in Solid 1.9, for Beauty Is a Wound (stock 0):

Event What runs class attribute
Page opens className = "card light", then toggle("sold-out", true) card light sold-out
Theme changes to dark className = "card dark" card dark

The card is no longer grey, though its stock is still 0. The computed class replaces the whole attribute and wipes sold-out with it. classList only acts when its own condition changes, and stock() === 0 is still true as before, so nothing puts it back. The rule: when an element uses classList, keep its class fixed, and move the changing classes into classList: class="card" classList={{ "sold-out": stock() === 0, dark: theme() === "dark" }}.

CSS Modules: class names that don't clash

If BookCard and Cart both have .title in their own CSS files, the rule loaded later overrides the other, because every plain CSS class applies to the whole page. CSS Modules prevent that: a file ending in .module.css is imported as an object, and each of its class names is replaced with a unique one.

import styles from "./BookCard.module.css"

<div class={styles.card} classList={{ [styles["sold-out"]]: stock() === 0 }}>

Vite (the bundler Solid projects use) turns .card into a name like _card_1x8p2_1, and styles.card holds that name. So class names are scoped per file automatically. It's a bundler feature, so it runs in a Vite project, not in the Playground.

Other options for writing CSS in a Solid project:

Way When to pick it
Plain CSS: import "./BookCard.css" small projects, or styles for the whole page (fonts, base colours). Its classes apply to the whole page, so add your own prefix, for example .book-card-title.
CSS Modules the one this lesson uses: many components with similar class names, no extra package.
Tailwind CSS you want to write styles right in class, for example class="p-4 rounded-lg". It needs the packages tailwindcss and @tailwindcss/vite.

⚠️ Classes that won't stick, won't come off, or get wiped

TL;DR: class="card" for fixed classes. classList={{ "sold-out": stock() === 0 }} for classes that switch on and off, one classList.toggle per class, the condition written as a call. style={{ "text-decoration": "line-through", opacity: 0.5 }} with kebab-case keys in quotes and units in the values; backgroundColor and "font-size": 18 are dropped by the browser. Don't mix a computed class with classList on one element: each time class changes, the classes from classList disappear. CSS Modules (import styles from "./BookCard.module.css") make class names unique per file, so they don't clash with other components.

⚡ classList: classes that switch on and off

Write:

const [active, setActive] = createSignal(true)

<div classList={{ active: active() }}>Content</div>
<button onClick={() => setActive(v => !v)}>Toggle</button>

Meaning: classList takes an object { className: condition }. The class is on while its condition is true, and comes off when it's false. Write the condition as a call, active(), so it's read again each time the signal changes.

This exercise clicks the button labelled exactly Toggle once, then twice from the start, so flip the value with setActive(v => !v).

⚡ style: kebab-case keys in quotes

Write:

<p style={{ "text-decoration": stock() === 0 ? "line-through" : "none", "font-size": "18px" }}>${props.price}</p>

Meaning: A camelCase key from React habits, like backgroundColor, is dropped by the browser with no error. A number with no unit is dropped too: write "18px", not 18.

⚡ A computed class wipes classList

Guess first: <div class={"card " + theme()} classList={{ "sold-out": stock() === 0 }}> with stock 0 shows grey. Then the theme changes. Is the card still grey?

No. The computed class replaces the whole attribute and wipes sold-out with it, while classList only acts when its own condition changes.

Meaning: When an element uses classList, keep its class fixed, and move the changing classes into classList.

⚡ CSS Modules: class names per file

Write:

import styles from "./BookCard.module.css"

<div class={styles.card}>

Meaning: A .module.css file is imported as an object, and each of its class names is replaced with a unique one. So class names are scoped per file automatically and don't clash with other components.

What you will learn