0
0
mirror of https://github.com/sveltejs/svelte.git synced 2024-12-01 17:30:59 +01:00
svelte/site/content/tutorial/13-advanced-styling/01-classes/text.md

24 lines
582 B
Markdown
Raw Normal View History

2019-03-10 14:30:29 +01:00
---
title: The class directive
---
Like any other attribute, you can specify classes with a JavaScript attribute, seen here:
```html
<button
class="{current === 'foo' ? 'selected' : ''}"
2019-03-10 14:30:29 +01:00
on:click="{() => current = 'foo'}"
>foo</button>
```
This is such a common pattern in UI development that Svelte includes a special directive to simplify it:
```html
<button
class:selected="{current === 'foo'}"
2019-03-10 14:30:29 +01:00
on:click="{() => current = 'foo'}"
>foo</button>
```
The `selected` class is added to the element whenever the value of the expression is truthy, and removed when it's falsy.