# BaseCSSClass

> Provides reusable normalization and validation for HTML class attributes.

[API reference](/docs/api/index.md) › [HTML Helper](/docs/extensions/html-helper/reference/index.md) › [Base](/docs/extensions/html-helper/reference/base/index.md) › BaseCSSClass

| abstract class | `UIAwesome\Html\Helper\Base\BaseCSSClass` |
| --- | --- |
| Source | [`src/Base/BaseCSSClass.php`](https://github.com/ui-awesome/html-helper/blob/main/src/Base/BaseCSSClass.php#L23) |

1 subclass:

- [`UIAwesome\Html\Helper\CSSClass`](/docs/extensions/html-helper/reference/cssclass/index.md)

Provides reusable normalization and validation for HTML `class` attributes.

## Methods

| Method | Description | Defined by |
| --- | --- | --- |
| [`add()`](#method-add) static | Adds one or more CSS classes to an attribute array with validation and merging logic. | `BaseCSSClass` |
| [`render()`](#method-render) static | Renders a validated CSS class name using a base format and an allowed value list. | `BaseCSSClass` |

## Method details

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

```php
public static function add(mixed[] &$attributes, mixed[]|string|Stringable|UnitEnum|null $classes, bool $override = false): void
```

Adds one or more CSS classes to an attribute array with validation and merging logic.

Replaces the existing `class` attribute when `$override` is `true`; otherwise merges unique class names.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$attributes` | `mixed[]` | Attribute array to modify. Passed by reference and updated in place. |
| `$classes` | `mixed[]\|string\|`[`Stringable`](https://www.php.net/manual/en/class.stringable.php)`\|`[`UnitEnum`](https://www.php.net/manual/en/class.unitenum.php)`\|null` | Classes to add. |
| `$override` | `bool` | Whether to override (`true`) or merge (`false`, default) with existing classes. |

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

```php
public static function render(string|UnitEnum $class, string $baseClass, list<scalar|UnitEnum|null> $allowed): string
```

Renders a validated CSS class name using a base format and an allowed value list.

Validates the value against `$allowed` and renders it with `sprintf()`.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$class` | `string\|`[`UnitEnum`](https://www.php.net/manual/en/class.unitenum.php) | Class value to validate and render. |
| `$baseClass` | `string` | Base class format string (for example, `btn-%s`). |
| `$allowed` | `list<scalar\|`[`UnitEnum`](https://www.php.net/manual/en/class.unitenum.php)`\|null>` | List of allowed class values for validation. |

**Returns**

`string`: Formatted and validated CSS class name. [`Enum::normalizeValue()`](/docs/extensions/html-helper/reference/base/baseenum/index.md#method-normalizeValue) for enum normalization logic.

**Throws**

- [`InvalidArgumentException`](https://www.php.net/manual/en/class.invalidargumentexception.php): if the class value is not in the allowed list.