class:list is an Astro directive that builds a class attribute from a list of values: strings are added, objects add each key whose value is truthy, arrays are flattened, and false, null and undefined are skipped. Write <div class:list={['card', { active: isActive }, size]} /> and Astro renders a clean, deduplicated class string. It works on HTML elements and on components, and it uses the clsx library under the hood.
Key Takeaways
- Astro class:list accepts strings, arrays, objects and falsy values in any combination.
- Object keys are included when their value is truthy:
{ 'is-open': open }.- Duplicates are removed and the output has no stray spaces.
- Pass a
classprop through to the root element so parents can add classes.
What it accepts
---
// src/components/Example.astro
const isActive = true;
const size = 'lg';
const extra = undefined;
---
<div class:list={[
'card',
`card-${size}`,
{ 'card-active': isActive, 'card-disabled': false },
['shadow', 'rounded'],
extra,
isActive && 'ring',
]} />
Renders:
<div class="card card-lg card-active shadow rounded ring"></div>
false and undefined disappear, nested arrays are flattened, and duplicates are removed.
Toggling classes from props
The most common use is a component with variants:
---
// src/components/Badge.astro
interface Props {
tone?: 'neutral' | 'success' | 'danger';
outline?: boolean;
class?: string;
}
const { tone = 'neutral', outline = false, class: className } = Astro.props;
---
<span class:list={['badge', `badge-${tone}`, { 'badge-outline': outline }, className]}>
<slot />
</span>
<style>
.badge { display: inline-block; padding: 0.125rem 0.5rem; border-radius: 999px; font-size: 0.75rem; }
.badge-success { background: #dcfce7; color: #166534; }
.badge-danger { background: #fee2e2; color: #991b1b; }
.badge-outline { background: transparent; border: 1px solid currentColor; }
</style>
class is a reserved word in JavaScript, so it’s renamed to className when destructured. Passing it last lets the parent add layout classes: <Badge tone="success" class="ml-2">Paid</Badge>.
Scoped styles still apply
Astro adds its scoping attribute to elements in the component, so the classes produced by class:list are styled by the component’s <style> block exactly like a static class. You don’t need :global() for classes generated this way, only for elements created later by JavaScript.
With Tailwind CSS
class:list is handy with Tailwind for conditional utilities:
---
// src/components/NavLink.astro
interface Props { href: string }
const { href } = Astro.props;
const active = Astro.url.pathname === href;
---
<a
href={href}
aria-current={active ? 'page' : undefined}
class:list={[
'px-3 py-2 rounded-md text-sm font-medium',
active ? 'bg-gray-900 text-white' : 'text-gray-700 hover:bg-gray-100',
]}
>
<slot />
</a>
One limit to know: class:list deduplicates identical strings but doesn’t resolve conflicting utilities. ['p-2', 'p-4'] outputs both, and which wins depends on Tailwind’s CSS order. If components accept overriding utilities from parents, merge them with tailwind-merge before passing the result to class.
On framework components
On React, Vue, Svelte and other framework components, Astro turns class:list into a plain class prop before passing it. In React, read it as props.class, not className, unless you map it yourself.
class:list or class
Use a plain class when the value never changes. Use class:list as soon as any part is conditional; it’s clearer than template strings with ternaries, and it never leaves "undefined" in your HTML.
The Astro directives cheatsheet lists every template directive, and Tailwind CSS in Astro covers the setup. The styling docs explain how Komal themes organise styles, and every one of our Astro themes uses class:list in its components. For changing the tag itself rather than its classes, see dynamic tags in Astro.
FAQ
What is class:list in Astro?
A template directive that combines strings, arrays and objects into one class attribute, skipping falsy values and removing duplicates. It’s Astro’s built-in equivalent of the clsx helper.
Can I use class and class:list on the same element?
Put everything in class:list instead; include the static classes as the first string. Mixing both on one element isn’t the intended use.
Does class:list work with Astro scoped styles?
Yes. Classes produced by class:list are on elements Astro renders, so they receive the scoping attribute and match the component’s <style> rules.