--- permalink: /reference/css-classes.html order: 06 --- # CSS Classes In HTML, a _CSS class_ defines a set of styles which can be applied to elements using the `class` attribute. CSS classes are a convenient tool for changing styles and playing animations programmatically. For example, a Stimulus controller might add a "loading" class to an element when it is performing an operation in the background, and then style that class in CSS to display a progress indicator: ```html
``` ```css .search--busy { background-image: url(throbber.svg) no-repeat; } ``` As an alternative to hard-coding classes with JavaScript strings, Stimulus lets you refer to CSS classes by _logical name_ using a combination of data attributes and controller properties. ## Definitions Define CSS classes by logical name in your controller using the `static classes` array: ```js // controllers/search_controller.js import { Controller } from "@hotwired/stimulus" export default class extends Controller { static classes = [ "loading" ] // … } ``` ## Attributes The logical names defined in the controller's `static classes` array map to _CSS class attributes_ on the controller's element. ```html
``` Construct a CSS class attribute by joining together the controller identifier and logical name in the format `data-[identifier]-[logical-name]-class`. The attribute's value can be a single CSS class name or a list of multiple class names. **Note:** CSS class attributes must be specified on the same element as the `data-controller` attribute. If you want to specify multiple CSS classes for a logical name, separate the classes with spaces: ```html
``` ## Properties For each logical name defined in the `static classes` array, Stimulus adds the following _CSS class properties_ to your controller: Kind | Name | Value ----------- | ---------------------------- | ----- Singular | `this.[logicalName]Class` | The value of the CSS class attribute corresponding to `logicalName` Plural | `this.[logicalName]Classes` | An array of all classes in the corresponding CSS class attribute, split by spaces Existential | `this.has[LogicalName]Class` | A boolean indicating whether or not the CSS class attribute is present
Use these properties to apply CSS classes to elements with the `add()` and `remove()` methods of the [DOM `classList` API](https://developer.mozilla.org/en-US/docs/Web/API/Element/classList). For example, to display a loading indicator on the `search` controller's element before fetching results, you might implement the `loadResults` action like so: ```js export default class extends Controller { static classes = [ "loading" ] loadResults() { this.element.classList.add(this.loadingClass) fetch(/* … */) } } ``` If a CSS class attribute contains a list of class names, its singular CSS class property returns the first class in the list. Use the plural CSS class property to access all class names as an array. Combine this with [spread syntax](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax) to apply multiple classes at once: ```js export default class extends Controller { static classes = [ "loading" ] loadResults() { this.element.classList.add(...this.loadingClasses) fetch(/* … */) } } ``` **Note:** Stimulus will throw an error if you attempt to access a CSS class property when a matching CSS class attribute is not present. ## Naming Conventions Use camelCase to specify logical names in CSS class definitions. Logical names map to camelCase CSS class properties: ```js export default class extends Controller { static classes = [ "loading", "noResults" ] loadResults() { // … if (results.length == 0) { this.element.classList.add(this.noResultsClass) } } } ``` In HTML, write CSS class attributes in kebab-case: ```html
``` When constructing CSS class attributes, follow the conventions for identifiers as described in [Controllers: Naming Conventions](controllers#naming-conventions).