0
0
mirror of https://github.com/sveltejs/svelte.git synced 2024-11-29 16:36:44 +01:00
svelte/documentation/tutorial/13-advanced-styling/01-classes/text.md

26 lines
628 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:
<!-- prettier-ignore -->
```svelte
2019-03-10 14:30:29 +01:00
<button
class={current === 'foo' ? 'selected' : ''}
on:click={() => current = 'foo'}
2019-03-10 14:30:29 +01:00
>foo</button>
```
This is such a common pattern in UI development that Svelte includes a special directive to simplify it:
<!-- prettier-ignore -->
```svelte
2019-03-10 14:30:29 +01:00
<button
class:selected={current === 'foo'}
on:click={() => current = 'foo'}
2019-03-10 14:30:29 +01:00
>foo</button>
```
The `selected` class is added to the element whenever the value of the expression is truthy, and removed when it's falsy.