The class() helper | laravel-html | Spatie

 SPATIE

  Laravel HTML
===============

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Laravel-html](https://spatie.be/docs/laravel-html/v2)  General-usage  The class() helper

 Version   v3   v2   v1

 Other versions for crawler [v3](https://spatie.be/docs/laravel-html/v3) [v2](https://spatie.be/docs/laravel-html/v2) [v1](https://spatie.be/docs/laravel-html/v1)

- [ Introduction ](https://spatie.be/docs/laravel-html/v2/introduction)
- [ Postcardware ](https://spatie.be/docs/laravel-html/v2/postcardware)
- [ Installation &amp; setup in Laravel ](https://spatie.be/docs/laravel-html/v2/installation-setup)
- [ Upgrading ](https://spatie.be/docs/laravel-html/v2/upgrading)
- [ Questions and issues ](https://spatie.be/docs/laravel-html/v2/questions-issues)
- [ Changelog ](https://spatie.be/docs/laravel-html/v2/changelog)

General usage
-------------

- [ Core concepts ](https://spatie.be/docs/laravel-html/v2/general-usage/core-concepts)
- [ HTML builder ](https://spatie.be/docs/laravel-html/v2/general-usage/html-builder)
- [ Element classes ](https://spatie.be/docs/laravel-html/v2/general-usage/element-classes)
- [ Element methods ](https://spatie.be/docs/laravel-html/v2/general-usage/element-methods)
- [ The class() helper ](https://spatie.be/docs/laravel-html/v2/general-usage/the-class-helper)

      You are viewing the documentation for **an older version** of this package. You can check the version you are using with the following command:

 `                                    composer show spatie/laravel-html                                                                                                                                                                                                                                    `

The class() helper
==================

The `class` method on `Html` is a helper to render a `class` attribute similar to [Vue.js' `:class` property](https://vuejs.org/v2/guide/class-and-style.html#Object-Syntax).

It expects an array (or a `Collection`), and will toggle a set of classes depending on the values of the array.

```
class(['foo', 'bar' => true, 'baz' => false]) }}>
```

```

```

If there's just a value (non-associative), the class will always be added. Otherwise, the class—specified by the array key—will only appear in the list if the value evaluates to true (non-strict).
