【问题标题】:How to link different style sheets for different php include files如何为不同的php包含文件链接不同的样式表
【发布时间】:2011-03-18 02:52:54
【问题描述】:

所以我将 index.php 页面分为三个部分:顶部、中间、底部。中间部分将有不同的 html php inlude 页面,因此需要不同的样式表。我是否在单个 php 包含页面或索引页面中链接特定样式表?因为在index页面中,不同的样式表好像没有生效,这是为什么呢?

【问题讨论】:

  • 您应该将样式表的 URL 放在一个变量中,以便您的模板引擎正确使用。
  • 我想我的问题是,当我在 index.php 页面中链接它们时,为什么不识别中间部分 php 包含文件的样式表?
  • 我们需要查看代码来回答这个问题。
  • 为什么不能使用一个样式表?因为样式表设置在 html 的头部
  • 你最好创建一个包含所有变体的单个 CSS 文件,压缩它(尽可能多地 - 谷歌的在线工具),然后在所有页面的标题中包含 HTML对于那个文件。单个文件可能更大,但从好的方面来说,它应该缓存在浏览器中并整体减少带宽负载。

标签: php html css include stylesheet


【解决方案1】:

假设您的 about 页面有一个自定义 css 文件,您可以这样做:

关于.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>

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

【讨论】:

    猜你喜欢
    • 2011-06-05
    • 2014-03-27
    • 2014-11-29
    • 2012-03-11
    • 2014-12-29
    • 2017-05-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-13
    相关资源
    最近更新 更多