如何指定多个样式表

时间:2013-03-09 17:04:34

标签: php html css

所以我一直在我买的PHP书中练习一些练习,他们建议设置一个标题并将其保存为单独的文档,然后当你想在你的页面中使用它时,你只需要使用

<?php
   require('header.html');
?>

虽然这节省了用于修改每个页面标题的代码的垃圾,我希望为不同的页面使用不同的样式表..但我不知道如何,因为当调用header.html时,它会自动加载它自己的样式表..

有没有像“IF”声明我可以用来确定我可以使用哪一个?样式表规范必须在<head>

2 个答案:

答案 0 :(得分:3)

好的,这是一个例子。

index.php将被访问的文件

<?php
    $stylesheets = [
        '/stylesheets/common.css',
        '/stylesheets/themes.css',
    ];
?>
<html>
    <head>
        <?php include("head.php"); ?>
    </head>
</html>

head.php

<?php foreach ($stylesheets as $stylesheet): ?>
    <link rel="stylesheet" type="text/css" href="<?= $stylesheet ?>" />
<?php endforeach; ?>

输出应该是这样的:

<html>
    <head>
        <link rel="stylesheet" type="text/css" href="/stylesheets/common.css" />
        <link rel="stylesheet" type="text/css" href="/stylesheets/themes.css" />
    </head>
</html>

<强>为什么吗

因为您为$stylesheets分配了一组值( URI到样式表)。当您包含文件head.php时,变量$stylesheets可用,因为它在包含之前已声明/已分配。

head.php然后简单地迭代值并将它们喷出。通过更改$stylesheets的值,您可以在结果页面中添加或删除样式表引用。

但这充满了问题;没有错误检查等,并且需要做更多工作才能在生产中可靠地使用这样的东西(或者还需要更多的工作

答案 1 :(得分:0)

不要包含标题的那一部分。这样你也可以正确地标题和关键每页,即

<html>
<head>
<title Yada yada</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="">
<meta name="keywords" content="">
<meta name="author" content="">
<link href="style/customstyle.css" rel="stylesheet">
<?php require_once('header.html'); ?>

您的网站

然后在您的header.html中完成您的代码

<link href="style/style.css" rel="stylesheet">
....
</head>
<body>
<div class....>
 Your Header (topbar /menu etc..