PHP Classes

PHP CSS Generator: Compose and generate CSS programmatically

Recommend this page to a friend!
  Info   View files Example   View files View files (4)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 332 All time: 7,114 This week: 404Up
Version License PHP version Categories
css-generator 1.0.0BSD License5.4HTML, PHP 5


This package can compose and generate CSS programmatically.

It provides a fluent interface to define CSS stylesheet rule properties by id, class or element name.

The class can define the selector with multiple path elements by adding child path elements or go back to the parent selector element.

The generated CSS stylesheet is returned as a string.

Picture of Aleksandar Zivanovic
  Performance   Level  
Name: Aleksandar Zivanovic <contact>
Classes: 16 packages by
Country: Serbia Serbia
Age: 29
All time rank: 16287 in Serbia Serbia
Week rank: 387 Up3 in Serbia Serbia Up
Innovation award
Innovation award
Nominee: 4x



ini_set('display_errors', true);

__DIR__ . '/class.CssGenerator.php';
__DIR__ . '/class.CssRule.php';

$css = new CssGenerator();

$css->defineIdRule('app') // #app -> defines rule with ID as selector and "app" as selector value
->set('width', '900px') // #app -> sets property width of #app
->set('margin', '0 auto') // #app -> sets property margin of #app
->defineClassSubRule('container') // #app .container -> creates CLASS rule as rule inside #app
->set('padding', '100px') // #app .container -> sets property padding of #app .container
->set('color', 'red') // #app .container -> sets property color of #app .container
->defineClassRule('small') // #app .container.small -> creates CLASS rule that is additional class of .container
->set('width', '20px') // #app .container.small -> sets property width of #app .container.small
->set('color', 'purple') // #app .container.small -> sets property color of #app .container.small
->getParent() // #app .container -> moves pointer to first parent
->defineClassChildRule('small') // #app .container > .small -> defines DIRECT CHILD rule to #app .container
->set('color', 'blue') // #app .container > .small -> sets property color of #app .container > .small
->getParent('app') // #app -> moves pointer to first parent with identifier value "app"
->set('background-color', 'silver'); // #app -> sets property background-color of #app

file_put_contents('style.css', $css->generate());

        <title>CSS Generator</title>
        <link rel="stylesheet" href="style.css" />
    <div id="app">
            <div class="container">
                <div class="small">
                    .container > .small
        <div class="container small">

  Files folder image Files  
File Role Description
Plain text file class.CssGenerator.php Class Generator Class
Plain text file class.CssRule.php Class CSS Rule Definition
Accessible without login Plain text file example.php Example Example script
Accessible without login Plain text file example_two.php Example Example script

 Version Control Unique User Downloads Download Rankings  
This week:0
All time:7,114
This week:404Up