--- title: Toggler description: Toggler makes it easy to toggle CSS or animate any element with a click. js: js/foundation.toggler.js mui: true video: 'wHpZCrpKlBc' --- ## Toggle a CSS class To setup a class toggle, start by adding the attribute `data-toggler` to an element. The value of `data-toggler` is the class you want to toggle (like `.classname` and `classname`). Also give the element a unique ID so it can be targeted.

Watch this part in video

edit on codepen button
```html ``` Then, add `data-toggle` to any element, with the ID of the target as the value of the attribute. Now, any time you click on this element, the class will toggle on and off on the target. ```html

Expand!

```

Expand!

--- ## Toggle with Animation Instead of toggling a class, you can also toggle visibility. When toggled, the element comes into or out of view using a Motion UI class. Instead of `data-toggler`, add the attribute `data-animate`. The value of the attribute is the *in animation* you want, followed by the *out animation*.

Watch this part in video

edit on codepen button
```html_example

Toggle Panel

Hello!

Lorem ipsum dolor sit amet, consectetur adipisicing elit. Dicta quas optio alias voluptas nobis iusto mollitia asperiores incidunt reprehenderit doloribus voluptatibus officiis minus, inventore, quasi nisi. Consequuntur, quidem. Sint, dicta?

``` --- ## Mark as Closable To create an element that can be closed once, add the attribute `data-closable`. Then add a click trigger inside the element using `data-close`.

Watch this part in video

edit on codepen button
```html_example

Lorem ipsum dolor sit amet, consectetur adipisicing elit. Labore praesentium sint alias dolorum qui vel quaerat, libero consequatur non esse asperiores veritatis commodi, odit eum ipsam nemo dicta iste aliquam.

``` --- ### With Alternate Animation `data-closable` can be configured with a custom exit animation.

Watch this part in video

edit on codepen button
```html_example

Lorem ipsum dolor sit amet, consectetur adipisicing elit. Labore praesentium sint alias dolorum qui vel quaerat, libero consequatur non esse asperiores veritatis commodi, odit eum ipsam nemo dicta iste aliquam.

``` --- ### Toggle on focus The `data-toggle` attribute only toggles classes/visibility on click. You can also have the toggle fire when an element is *focused* or *unfocused* using `data-toggle-focus`.

Watch this part in video

edit on codepen button
```html_example ``` --- ## Multiple Targets The `data-toggle`, `data-close`, and `data-open` attributes can now target multiple elements! The syntax is simple; just pass a *space* separated list to the `data-x` attribute like so:

Watch this part in video

edit on codepen button
```html ``` Then the elements with ids of `foo`, `bar`, and `baz` will be toggled any time your button, (or any other element you choose), is clicked.
Photo of Uranus.
Photo of Uranus.
Photo of Uranus.