在PHP文件中包含的PHP文件中包含CSS文件

时间:2012-10-26 10:18:01

标签: php html css

我有一个php文件(现在没有)使用任何php代码,它包含将在所有页面中使用的标题和主菜单的代码。即使我为h1创建了一个样式类,CSS文件也没有效果。显示“TEST”文本,但未应用样式。如何正确包含CSS文件?

mainMenu.php

<!--This code is included within the <head> of each page, and the stylesheet for the header and main menu are included here-->
<link href="styles/headerMenu.css" ref="stylesheet" type="text/css">

<!--Header and menu code-->
<div>
    <h1>TEST</h1>
</div>

的index.php

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8" />
    <title>untitled</title>
    <?php include ('./includes/mainMenu.php') ?>
</head>

<body>
</body>

</html>

5 个答案:

答案 0 :(得分:1)

我想这可能是因为您的菜单显示在<head>标记内。

CSS需要介于<head></head>之间,但其余内容需要位于<body>标记内

答案 1 :(得分:1)

<link href="styles/headerMenu.css" rel="stylesheet" type="text/css">

必须在<HEAD></HEAD>

<div>
    <h1>TEST</h1>
</div>

必须在<BODY></BODY>

您必须将此文件分成2个文件,并将它们包含在Head和Body ..

答案 2 :(得分:1)

找不到CSS文件。仔细检查链接并更正它:

<link href="menu.css" rel="stylesheet" type="text/css">
                        ^- REL not REF

另外,为防止出现其他问题,请从代码中删除<head><body>的开始和结束标记。输出HTML元素的方式,如果保留这些标记,则会创建错误的HTML。删除它们,您的页面将再次成为有效的HTML。

的index.php

<!DOCTYPE html>
<html>    

    <meta charset="utf-8" />
    <title>untitled</title>
    <?php include ('menu.php') ?>    

</html>

有效的HTML具有以下优点:您可以通过验证程序运行它以及早发现错误。

答案 3 :(得分:0)

不要在HEAD部分中包含HTML代码。仅在HEAD部分中包含CSS和JavaScript文件。你需要在一些php文件中加上css或images路径的前缀 例如
创建名为“conn_path.php”的新php文件

<?php
define('SITE_PATH',$_SERVER['DOCUMENT_ROOT'].'siteName/');
define('SITE_HTTP_PATH','http://localhost/'siteName/');
define('CSS_PATH','http://localhost/siteName/styles/');
define('IMAGE_PATH','http://localhost/siteName/images/');
?>

然后你的路径如下: - mainMenu.php

<?php include "conn_path.php" ?>
<link href="<?php echo CSS_PATH ;?>headerMenu.css" rel="stylesheet" type="text/css">

它将在整个项目中帮助你......

答案 4 :(得分:0)

使用必要(并重复使用)的html创建模板文件。还有<html><head><body>标签以及所有页面中必须包含的任何内容 - 作为样式表和菜单。

然后添加一个包含单个变量的内容部分。

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8" />
    <title>untitled</title>
    <link href="styles/headerMenu.css" rel="stylesheet" type="text/css">
</head>

<body>
    <!--Header and menu code-->
    <div>
        <h1>TEST</h1>
    </div>

    <?php echo $page_content; ?>
</body>

</html>

这样,任何页面内容都应该分配给$page_content而不是回显。