如何为不同的php包含文件链接不同的样式表

时间:2011-03-18 02:52:54

标签: php html css include stylesheet

所以我将index.php页面划分为三个部分:top,middle,bottom。中间部分将有不同的html php inlude页面,因此需要不同的样式表。我是否链接了各个php包含页面或索引页面中的特定样式表?因为在索引页面中,不同的样式表似乎没有生效,为什么会这样?

1 个答案:

答案 0 :(得分:2)

假设您的about页面有一个自定义的css文件,您可以执行以下操作:

about.php

<?
$css = array('path_to_css_file', 'path_to_another_css_file');
require_once('header.php'); // aka the top
?>

[about page content goes here]

<?
require_once('footer.php'); // aka the bottom
?>

您可以在header.php文件中执行此操作:

的header.php

<html>
<head>
<title></title>
<link rel="stylesheet" type="text/css" href="main_style_sheet.css" />
<?
if (isset($css) && is_array($css))
  foreach ($css as $path)
    printf('<link rel="stylesheet" type="text/css" href="%s" />', $path);
?>
</head>
<body>

这样您只需加载给定页面所需的内容。