PEAR logo

HTML_CSS : The Definitive Guide

Chapter 11. Examples

Table of Contents

Basic usage
Simple selectors with one or more property
Combine selectors with one or more property
Parsing data sources usage
Parse a string that contains CSS information
Parse multiple data sources at once
Searching for usage
Searching for selectors and properties

Basic usage

Simple selectors with one or more property

This example give you ability to define some styles from scratch and produce a simple style sheet output directly to browser.

Here is the result we should expected :

  1. body {
  2.   background-color: #0c0c0c;
  3.   color: #ffffff;
  4. }
  5.  
  6. h1 {
  7.   text-align: center;
  8.   font: 16pt helvetica, arial, sans-serif;
  9. }
  10.  
  11. p {
  12.   font: 12pt helvetica, arial, sans-serif;
  13. }

Example 11.1. Simple selector definitions

  1. <?php
  2. require_once 'HTML/CSS.php';
  3.  
  4. $css = new HTML_CSS();
  5.  
  6. // define styles
  7. $css->setStyle('body', 'background-color', '#0c0c0c');
  8. $css->setStyle('body', 'color', '#ffffff');
  9. $css->setStyle('h1', 'text-align', 'center');
  10. $css->setStyle('h1', 'font', '16pt helvetica, arial, sans-serif');
  11. $css->setStyle('p', 'font', '12pt helvetica, arial, sans-serif');
  12.  
  13. // output the stylesheet directly to browser
  14. $css->display();
  15. ?>

Combine selectors with one or more property

This example give you ability to define another selector with same properties as the previous one already defined. The simple style sheet is output directly to browser.

Here p and div#black selectors share the same properties.

Here is the result we should expected :

  1. p, div#black {
  2.   font: 12pt helvetica, arial, sans-serif;
  3. }

Example 11.2. Combine selectors properties

  1. <?php
  2. require_once 'HTML/CSS.php';
  3.  
  4. $css = new HTML_CSS();
  5.  
  6. // define styles
  7. $css->setStyle('p', 'font', '12pt helvetica, arial, sans-serif');
  8. $css->setSameStyle('div#black', 'p');
  9.  
  10. // output the stylesheet directly to browser
  11. $css->display();
  12. ?>
HTML_CSS : The Definitive Guide v 1.5.0 : January 15, 2008