PHPackages                             flying/css-classes - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. flying/css-classes

ActiveLibrary

flying/css-classes
==================

CSS classes list manipulation library

v2.0.0(1y ago)0531MITPHPPHP ^8.0

Since May 5Pushed 1y ago1 watchersCompare

[ Source](https://github.com/FlyingDR/css-classes)[ Packagist](https://packagist.org/packages/flying/css-classes)[ RSS](/packages/flying-css-classes/feed)WikiDiscussions master Synced today

READMEChangelogDependencies (2)Versions (4)Used By (0)

css-classes
===========

[](#css-classes)

Simple library to manage a list of CSS classes.

It simplifies tasks of constructing a list of CSS classes from given information, adding and removing classes from the list.

Installation
------------

[](#installation)

Simply install it using Composer:

```
composer require flying/css-classes
```

Standalone usage
----------------

[](#standalone-usage)

Functionality is exposed via [`Classes`](src/Classes.php) and [`MutableClasses`](src/MutableClasses.php) classes, both implements same [`ClassesInterface`](src/ClassesInterface.php) interface.

Further documentation uses immutable `Classes` implementation as an example unless explicitly defined otherwise, but `MutableClasses` have exactly same usage in except of mutability.

### Accepted values

[](#accepted-values)

Any method that is mean to accept CSS classes actually accepts arbitrary number of arguments of any type (`mixed ...$classes`).

Given arguments are processed in this way:

- Iterables are expanded
- Nested values are flattened
- Any values besides non-empty strings are ignored

### Immutability

[](#immutability)

v1 provides only immutable implementation. Since v2 there are both immutable `Classes` and mutable `MutableClasses` implementations.

### List creation

[](#list-creation)

Classes list can be constructed using either by creating new `Classes` instance or by using `Classes::from()` static method. Both methods are [accepted](#accepted-values) variable amount arguments of any type.

Example:

```
$classes = Classes::from('foo', 'bar baz', ['a', 'b', 'c']);
// Outputs: foo bar baz a b c
echo $classes;
$classes = $classes->without('baz');
// Outputs: foo bar a b c
echo $classes;
$classes = $classes->filter(fn(string $class): bool => strlen($class) > 1);
// Outputs: foo bar
echo $classes;
$classes = $classes->with('x', null, true, false, 42, 1.2345, ['y', ['z']]);
// Outputs: foo bar x y z
echo $classes;
// Outputs: 5
echo count($classes);
```

Fluent interface is also possible to use:

```
$classes = Classes::from('foo', 'bar baz', ['a', 'b', 'c'])
    ->without('baz')
    ->filter(fn(string $class): bool => strlen($class) > 1)
    ->with('x', null, true, false, 42, 1.2345, ['y', ['z']]);
// Outputs: foo bar x y z
echo $classes;
// Outputs: 5
echo count($classes);
```

### Classes list modification

[](#classes-list-modification)

These methods can be used to modify the list of CSS classes:

- `with(...$classes)` - Add given CSS classes to the list
- `without(...$classes)` - Remove given CSS classes from the list
- `filter(callable $filter)` - Filter CSS classes list using provided filter. `$filter` signature is `(string $class): bool`
- `clear()` - Remove all CSS classes from the list. For [immutable](#immutability) `Classes` implementation it is equivalent of `new Classes()`.

### Classes list exporting

[](#classes-list-exporting)

Classes list can be exported as a plain array using `toArray()` method.

Also, `ClassesInterface` interface extends `ArrayAggregate` and `Stringable` interfaces, so it is also possible to iterate over the list of CSS classes and cast its value into string.

### Other methods

[](#other-methods)

`ClassesInterface` extends `Countable` interface, so number of available CSS classes can be calculated using `count()` PHP function or by calling `count()` method.

It is also possible to check if given CSS class is available in the list by using `has(string $class)` method.

### Standalone `classes()` function

[](#standalone-classes-function)

For simple cases when it is only needs to create CSS classes list as a string from available data it is also possible to use `\Flying\Util\Css\classes()` function.

`classes()` function is available since v1.1.0

Usage in Twig
-------------

[](#usage-in-twig)

Library includes `\Flying\Util\Css\Twig\ClassesExtension` Twig extension that exposes `classes` function that provides same functionality as a [main class](#standalone-usage).

It can be used as a simple construction of the classes list:

```
{%- set category = 'some-category' -%}
{%- set active = true -%}
{%- set current = false -%}
{# "main-class some-category is-active" will be generated #}

```

But it is also possible to use CSS classes list modification methods:

```
{%- set categories = {foo: 'Foo', bar: 'Bar'} -%}
{%- set current = 'foo' -%}

    {%- set cl = classes('category') -%}
    {% for id, name in categories %}
        {# For "foo" category: "category ctg-foo current" will be generated #}
        {# For "bar" category: "category ctg-bar" will be generated #}
        {{ name }}
    {% endfor %}

```

Refer to the [Twig documentation](https://twig.symfony.com/doc/3.x/api.html#using-extensions) on how to add the extension to the Twig.

For Symfony, there is a [separate bundle](https://github.com/FlyingDR/css-classes-bundle) to simplify integration.

License
-------

[](#license)

Library is licensed under MIT license.

###  Health Score

28

—

LowBetter than 57% of packages

Maintenance35

Infrequent updates — may be unmaintained

Popularity10

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity51

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~224 days

Total

3

Last Release

604d ago

Major Versions

v1.1.0 → v2.0.02024-07-25

### Community

Maintainers

![](https://www.gravatar.com/avatar/2219fdfdf70c7118270dfa18596299660a51d4cced164e98d139dd03b3579128?d=identicon)[Flying](/maintainers/Flying)

---

Top Contributors

[![FlyingDR](https://avatars.githubusercontent.com/u/822673?v=4)](https://github.com/FlyingDR "FlyingDR (17 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/flying-css-classes/health.svg)

```
[![Health](https://phpackages.com/badges/flying-css-classes/health.svg)](https://phpackages.com/packages/flying-css-classes)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
