CSS dinâmico com PHP

Simples exemplo de como utilizar variáveis de PHP dentro de uma folha de estilo (CSS), mantendo o HTML separado do CSS.

Vejam o exemplo:

Arquivo index.php:

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=iso-8859-1″ />
<title>CSS dinâmico com PHP</title>
<link href=”estilo.php” rel=”stylesheet” type=”text/css” />
</head>

<body>
<?php for ($x = 1; $x <= 5; $x++) : ?>
<p class=”size-<?php echo $x; ?>”>Exemplo de  CSS gerado com PHP</p>
<?php endfor; ?>
</body>
</html>

Arquivo estilo.php:

<?php
//Simples exemplo de como utilizar variáveis de PHP dentro de uma folha de estilo (CSS), mantendo o HTML separado do CSS.
//Autor: Flávio Torelli
//Contato: flavio.torelli@gmail.com

header(’Content-Type: text/css’);

$bg_color = “#ddd”;
$font_color = “#333″;
$font_family = “Trebuchet Ms, Arial, Helvetica, Sans-serif”;

//EXEMPLO:
?>

body {
background:<?php echo $bg_color; ?>;
color:<?php echo $font_color; ?>;
font-family:<?php echo $font_family; ?>;
}

<?php
//Criando as classes CSS dinamicamente.
for ($x = 1; $x <= 5; $x++) :

echo ”
.size-”.$x.” {
font-size:”.$x.”0px;
}
“;

endfor;

//Agora não há desculpas para utilizar estilos inline.
?>

Se gostou da matéria deixe um comentário or subscribe to the feed and get future articles delivered to your feed reader.

Comentários

Nenhum comentário ainda.

Deixe um comentário

(obrigatório)

(obrigatório)