Return to Snippet

Revision: 27308
at June 2, 2010 02:42 by browncardigan


Updated Code
<? header("Content-type: text/css"); ?>

/* all your other CSS rules can go here too! */

h2 {
display:block;
width:400px;
height:30px;
background-repeat:no-repeat;
}

h2 span {
display:none;
}

<?
$path = 'headers/';
$dir = $_SERVER['DOCUMENT_ROOT'] . $path;
if ($handle = opendir($dir)) {
    while (false !== ($file = readdir($handle))) {
        if ($file != "." && $file != "..") {
			$class = current(explode('.', $file));
			echo 'h2.' . $class . ' { ' . "\n";
			echo 'background-image:url("' . $path . $file . '"); ' . "\n";
			echo '}' . "\n\n";
		}
    }
    closedir($handle);
}
?>

Revision: 27307
at June 2, 2010 02:31 by browncardigan


Updated Code
<? header("Content-type: text/css"); ?>

/* all your other CSS rules can go here too! */

h2 {
display:block;
width:400px;
height:30px;
}

h2 span {
display:none;
}

<?
$path = 'headers/';
$dir = $_SERVER['DOCUMENT_ROOT'] . $path;
if ($handle = opendir($dir)) {
    while (false !== ($file = readdir($handle))) {
        if ($file != "." && $file != "..") {
			$class = current(explode('.', $file));
			echo 'h2.' . $class . ' { ' . "\n";
			echo 'background-image:url("' . $path . $file . '"); ' . "\n";
			echo '}' . "\n\n";
		}
    }
    closedir($handle);
}
?>

Revision: 27306
at June 2, 2010 02:30 by browncardigan


Updated Code
<? header("Content-type: text/css"); ?>
/* all your other CSS rules can go here too! */
h2 { display:block; width:400px; height:30px; }
h2 span { display:none; }
<?
$path = 'headers/';
$dir = $_SERVER['DOCUMENT_ROOT'] . $path;
if ($handle = opendir($dir)) {
    while (false !== ($file = readdir($handle))) {
        if ($file != "." && $file != "..") {
			$class = current(explode('.', $file));
			echo 'h2.' . $class . ' { ' . "\n";
			echo 'background-image:url("' . $path . $file . '"); ' . "\n";
			echo '}' . "\n\n";
		}
    }
    closedir($handle);
}
?>

Revision: 27305
at June 2, 2010 02:28 by browncardigan


Updated Code
<? header("Content-type: text/css"); ?>
/* all your other CSS rules can go here too! */
h2 { display:block; width:400px; height:30px; }
h2 span { display:none; }
<?
$path = 'headers/';
$dir = $_SERVER['DOCUMENT_ROOT'] . $path;
if ($handle = opendir($dir)) {
    while (false !== ($file = readdir($handle))) {
        if ($file != "." && $file != "..") {
			$class = current(explode('.', $file));
			echo 'h2.' . $class . ' { ' . "\n";
			echo 'background-image:url("../' . $path . $file . '"); ' . "\n";
			echo '}' . "\n\n";
		}
    }
    closedir($handle);
}
?>

Revision: 27304
at June 2, 2010 02:25 by browncardigan


Updated Code
<? header("Content-type: text/css"); ?>
/* all your other CSS rules can go here too! */
h2 { display:block; width:400px; height:30px; }
<?
$path = 'folder-of-header-images';
$dir = 'server-root' . $path;
if ($handle = opendir($dir)) {
    while (false !== ($file = readdir($handle))) {
        if ($file != "." && $file != "..") {
			$class = current(explode('.', $file));
			echo 'h2.' . $class . ' { ' . "\n";
			echo 'background-image:url("../' . $path . $file . '"); ' . "\n";
			echo '}' . "\n\n";
		}
    }
    closedir($handle);
}
?>

Revision: 27303
at June 2, 2010 02:23 by browncardigan


Initial Code
$path = 'folder-of-header-images';
$dir = 'server-root' . $path;
if ($handle = opendir($dir)) {
    while (false !== ($file = readdir($handle))) {
        if ($file != "." && $file != "..") {
			$class = current(explode('.', $file));
			echo 'h2.' . $class . ' { ' . "\n";
			echo 'background-image:url("../' . $path . $file . '"); ' . "\n";
			echo '}' . "\n\n";
		}
    }
    closedir($handle);
}

Initial URL


Initial Description
dynamic CSS file, where the background style attributes (in this case h2 headers) are auto-created from the files in a particular directory.

1. include the css file as per normal in your <head>
2. create new css.php file
3. add in custom css header - header("Content-type: text/css");
4. create directory full of heading images
5. add in code below
6. <h2> will automatically set the background image of "some-header.png" from the image directory
6. win life!

Initial Title
dynamic CSS background images

Initial Tags


Initial Language
PHP