PEAR logo

HTML_CSS : The Definitive Guide

Basic concepts

What is a rule or "rule set" ?
Rule structure
Declaration block
Grouping declarations
Grouping selectors
CSS comments

What is a rule or "rule set" ?

A rule or "rule set" is a statement that tells browsers how to render particular elements on an HTML page. A rule set consists of a selector followed by a declaration block.

Rule structure

Figure 4.1. Rule structure

Rule structure

Declaration block

The HTML_CSS::setStyle() method is the only one to handle a declaration block.

For example, to declare a such rule :

  1. p { padding: 5px; }
Here is the php code to create the same sentence with HTML_CSS :
  1. <?php
  2. require_once 'HTML/CSS.php';
  3.  
  4. $css = new HTML_CSS();
  5.  
  6. $css->setStyle('p', 'padding', '5px');
  7.  
  8. $css->display();
  9. ?>

Grouping declarations

You can use more than one declaration within a declaration block. Each declaration must be separated with a semicolon ";".

For example, to declare a such rule :

  1. p { padding: 5px; margin: 1px; }

Or, with whitespace added to aid readability :

  1. p {
  2.   padding: 5px;
  3.   margin: 1px;
  4. }
Here is the php code to create the same sentence with HTML_CSS :
  1. <?php
  2. require_once 'HTML/CSS.php';
  3.  
  4. $css = new HTML_CSS();
  5.  
  6. $css->setStyle('p', 'padding', '5px');
  7. $css->setStyle('p', 'margin', '1px');
  8.  
  9. $css->display();
  10. ?>
Notice that we use twice HTML_CSS::setStyle() method call to declare each declaration block for the same selector.

Grouping selectors

When several selectors share the same declarations, they may be grouped together to save writing the same rule more than once. Each selector must be separated by a comma. For example :

  1. h1, h2, h3, h4 { padding: 1em; }
  2. .highlight p, .highlight ul { margin-left: .5em; }
  3. #main p, #main ul { padding-top: 1em; }
To produce such result, we will need help of 3 new methods : HTML_CSS::setSameStyle(), HTML_CSS::createGroup() and HTML_CSS::setGroupStyle().
  1. <?php
  2. require_once 'HTML/CSS.php';
  3.  
  4. $css = new HTML_CSS();
  5.  
  6. // two selector groups only
  7. $css->setStyle('.highlight p', 'margin-left', '.5em');
  8. $css->setSameStyle('.highlight ul', '.highlight p');
  9.  
  10. // or even this way
  11. $g2 = $css->createGroup('#main p, #main ul');
  12. $css->setGroupStyle($g2, 'padding-top', '2em');
  13.  
  14. // more than two selector groups
  15. $g1 = $css->createGroup('h1, h2, h3, h4');
  16. $css->setGroupStyle($g1, 'padding', '1em');
  17.  
  18. $css->display();
  19. ?>

We should take care than grouping two selectors may be write either with HTML_CSS::setSameStyle() or with couple HTML_CSS::createGroup() and HTML_CSS::setGroupStyle(). When we have to group three or more selectors, there is only one possibility: couple HTML_CSS::createGroup() and HTML_CSS::setGroupStyle().

CSS comments

You can insert comments in CSS to explain your code. Like HTML comments, CSS comments will be ignored by the browser. A CSS comment begins with "/*", and ends with "*/". Comments can appear before or within rule sets as well as across multiple lines.

HTML_CSS 1.0.0 has not yet ability to handle comment such as :

  1. /* Comment here */
  2. p
  3. {
  4. margin: 1em; /* Comment here */
  5. padding: 2em;
  6. /* color: white; */
  7. background-color: blue;
  8. }
  9.  
  10. /*
  11. multi-line
  12. comment here
  13. */
[Warning] Warning
The common mistake that people make when writing comments is to expect getting all comments describe with such code below : it's an error.
  1. $css->setComment('comment here');
  2.  
  3. $css->setStyle('p', 'margin', '1em');
  4. $css->setStyle('p', 'padding', '2em');
  5. $css->setComment('color: white;');
  6. $css->setStyle('p', 'background-color', 'blue');
  7.  
  8. $css->setComment('
  9. multi-line
  10. comment here
  11. ');

You will only get such result:

  1. /*
  2. multi-line
  3. comment here
  4.  */
  5.  
  6. p {
  7.   margin: 1em;
  8.   padding: 2em;
  9.   background-color: blue;
  10. }
Only one comment is allowed due to usage of parent class method HTML_Common::setComment().
HTML_CSS : The Definitive Guide v 1.5.0 : January 15, 2008