PHP Classes

PHP CSS Generator: Compose and generate CSS programmatically

Recommend this page to a friend!
     
  Info   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 337 All time: 7,098 This week: 79Up
Version License PHP version Categories
css-generator 1.0.0BSD License5.4HTML, PHP 5
Description 

Author

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
Innovation award
Innovation award
Nominee: 4x

Example

<?php

error_reporting
(E_ALL);
ini_set('display_errors', true);

require_once
__DIR__ . '/class.CssGenerator.php';
require_once
__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());

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


  Files folder image Files (4)  
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

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 0%
Total:337
This week:0
All time:7,098
This week:79Up