PHPackages                             luizbills/css-generator - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. luizbills/css-generator

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

luizbills/css-generator
=======================

Write CSS programatically using PHP.

v4.0.1(3y ago)2136.9k↓19.4%1[1 issues](https://github.com/luizbills/css-generator.php/issues)MITPHPPHP &gt;=7.4.0

Since Sep 20Pushed 3y ago2 watchersCompare

[ Source](https://github.com/luizbills/css-generator.php)[ Packagist](https://packagist.org/packages/luizbills/css-generator)[ RSS](/packages/luizbills-css-generator/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (5)Dependencies (4)Versions (7)Used By (0)

CSS Generator
=============

[](#css-generator)

Write CSS programatically using PHP.

Install
-------

[](#install)

```
composer require luizbills/css-generator
```

Usage
-----

[](#usage)

### Create a generator

[](#create-a-generator)

```
require_once 'vendor/autoload.php';

use luizbills\CSS_Generator\Generator as CSS_Generator;

$options = [
    // default values
    // 'indent_style' => 'space', // you can change to 'tab'
    // 'indent_size' => 4 // 4 spaces by default
];

$css = new CSS_Generator( $options );

// define your css code (see below)

// output the generated code
echo "" . $css->get_output() . "";
```

### Add rules

[](#add-rules)

```
$css->add_rule( 'a', [ 'color' => 'red' ] );

$css->add_rule(
    [ 'p', 'div' ],
    [
        'margin' => '13px',
        'padding' => '9px'
    ]
);
```

Output:

```
a {
    color: red;
}
p,
div {
    margin: 13px;
    padding: 9px;
}
```

### Add global variables

[](#add-global-variables)

```
$css->root_variable( 'color1', 'red' );
$css->add_rule( 'a', [ 'color' => 'var(--color3)' ] );
$css->root_variable( 'color2', 'green' );
$css->root_variable( 'color3', 'blue' );
```

Output:

```
:root {
    --color1: red;
    --color2: green;
    --color3: blue;
}

a {
    color: var(--color3);
}
```

**Note:** all variables declared by `root_variable` will be placed at the beginning.

### Add comments

[](#add-comments)

```
$css->add_comment( 'Lorem ipsum...' )
```

Output:

```
/* Lorem ipsum... */
```

### Open and close blocks

[](#open-and-close-blocks)

```
$css->open_block( 'media', 'screen and (min-width: 30em)' );
$css->add_rule( 'a', [ 'color' => 'red' ] );
$css->close_block(); // close the last opened block
```

Output:

```
@media screen and (min-width: 30em) {
    a {
        color: red;
    }
}
```

### Escape selectors

[](#escape-selectors)

Sometimes you need to escape your selectors.

```

```

```
$css->add_rule( '#' . $css->esc( '@' ), [
    'animation' => 'shake 1s'
] );
$css->add_rule( '.' . $css->esc( '3dots' ) . '::after', [
    'content' => '"..."'
] );
$css->add_rule( '.' . $css->esc( 'red:hover' ) . ':hover', [
    'color' => 'red'
] );
```

Output:

```
#\@ {
    animation: shake 1s;
}
.\33 dots::after {
    content: "...";
}
.red\:hover:hover {
    color: red;
}
```

### Include anything (be careful)

[](#include-anything-be-careful)

```
$css->add_raw( 'a{color:red}' );
```

Output:

```
a{color:red}
```

### Minify your CSS

[](#minify-your-css)

```
echo $css->get_output( true ); // returns the compressed code
echo $css->get_output( false ); // returns the pretty code
```

License
-------

[](#license)

MIT License © 2022 Luiz Bills

###  Health Score

37

—

LowBetter than 83% of packages

Maintenance19

Infrequent updates — may be unmaintained

Popularity37

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity66

Established project with proven stability

 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 ~290 days

Recently: every ~363 days

Total

6

Last Release

1344d ago

Major Versions

v3.1.1 → v4.0.02022-09-12

PHP version history (2 changes)v3.0.1PHP &gt;=5.4.0

v4.0.0PHP &gt;=7.4.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/0f0376a2b2aa5e59636915111b54815f24b963f463ff8b959be535275ffd7ec2?d=identicon)[luizbills](/maintainers/luizbills)

---

Top Contributors

[![luizbills](https://avatars.githubusercontent.com/u/1798830?v=4)](https://github.com/luizbills "luizbills (71 commits)")

---

Tags

cssgeneratorphp

###  Code Quality

TestsCodeception

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/luizbills-css-generator/health.svg)

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

###  Alternatives

[jbzoo/composer-diff

See what has changed after a composer update.

53888.9k1](/packages/jbzoo-composer-diff)[baibaratsky/php-wmsigner

WebMoney Signer: a native PHP implementation of the WMSigner module

16399.8k3](/packages/baibaratsky-php-wmsigner)[aak74/bx-data

Классы для удобного доступа к данным в 1C-Bitrix

232.1k](/packages/aak74-bx-data)[phalapi/wechatmini

PhalApi 2.x 微信小程序扩展

121.1k](/packages/phalapi-wechatmini)

PHPackages © 2026

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