# AttributesTrait

> Provides fluent methods that modify the HTML attributes of the widget element.

[API reference](/docs/api/index.md) › [Bootstrap 6](/docs/extensions/bootstrap6/reference/index.md) › AttributesTrait

| trait | `yii2\extensions\bootstrap6\AttributesTrait` |
| --- | --- |
| Source | [`src/AttributesTrait.php`](https://github.com/yii2-extensions/bootstrap6/blob/main/src/AttributesTrait.php#L17) |

Used by 2 types:

- [`InputWidget`](/docs/extensions/bootstrap6/reference/inputwidget/index.md)
- [`Widget`](/docs/extensions/bootstrap6/reference/widget/index.md)

Provides fluent methods that modify the HTML attributes of the widget element.

The using class must declare a public `$options` array; every method modifies it in place and returns the same
instance, so calls chain on the value returned by `begin()`, `new`, or `Yii::createObject()`. The classes a widget
requires (for example `collapse` or `navbar`) are added while rendering, from a copy of `$options`, so these methods
never remove them.

## Methods

| Method | Description | Defined by |
| --- | --- | --- |
| [`addAttributes()`](#method-addAttributes) | Adds a set of HTML attributes; attributes already set with the same name are replaced. | `AttributesTrait` |
| [`addClass()`](#method-addClass) | Adds one or more CSS classes to the existing classes; duplicated classes are collapsed. | `AttributesTrait` |
| [`addCssStyle()`](#method-addCssStyle) | Adds CSS style properties to the `style` attribute. | `AttributesTrait` |
| [`attribute()`](#method-attribute) | Sets an HTML attribute value. | `AttributesTrait` |
| [`class()`](#method-class) | Replaces the existing CSS classes with the specified ones. | `AttributesTrait` |

## Method details

### addAttributes() <span id="method-addAttributes"></span>

```php
public function addAttributes(array<string, mixed> $attributes): static
```

Adds a set of HTML attributes; attributes already set with the same name are replaced.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$attributes` | `array<string, mixed>` | Attribute values indexed by attribute names, for example `['id' => 'my-id']`. |

**Returns**

`static`

### addClass() <span id="method-addClass"></span>

```php
public function addClass(BackedEnum|string|null ...$class): static
```

Adds one or more CSS classes to the existing classes; duplicated classes are collapsed.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `...$class` | [`BackedEnum`](https://www.php.net/manual/en/class.backedenum.php)`\|string\|null` | CSS class names to add; an enum case adds its value, and `null` is ignored. |

**Returns**

`static`

**See also**

- <https://html.spec.whatwg.org/#classes>

### addCssStyle() <span id="method-addCssStyle"></span>

```php
public function addCssStyle(array<string, string>|string $style, bool $overwrite = true): static
```

Adds CSS style properties to the `style` attribute.

Without an existing style, a `string` is stored as is and an array renders as `color: red;`. With an existing
style, both are merged and normalized to `name: value;` pairs: new properties are appended, and a property
already set keeps its position.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$style` | `array<string, string>\|string` | CSS style, either an array such as `['color' => 'red', 'font-weight' => 'bold']` or a string such as `color: red; font-weight: bold;`. |
| `$overwrite` | `bool` | Whether to replace the value of properties already set; when `false`, their existing value is kept. |

**Returns**

`static`

**See also**

- <https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/style>

### attribute() <span id="method-attribute"></span>

```php
public function attribute(string $name, mixed $value): static
```

Sets an HTML attribute value.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$name` | `string` | Attribute name. |
| `$value` | `mixed` | Attribute value, or `null` to omit the attribute when rendering. |

**Returns**

`static`

### class() <span id="method-class"></span>

```php
public function class(BackedEnum|string|null ...$class): static
```

Replaces the existing CSS classes with the specified ones.

Only the classes held in `$options` are replaced; the classes the widget requires are still added while
rendering.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `...$class` | [`BackedEnum`](https://www.php.net/manual/en/class.backedenum.php)`\|string\|null` | CSS class names to set; an enum case sets its value, and `null` is ignored. When no class remains, the `class` attribute is removed. |

**Returns**

`static`

**See also**

- <https://html.spec.whatwg.org/#classes>