在所有网页上保留页眉和页脚?

时间:2015-01-15 14:10:32

标签: php html css web

我已经完成了我的网站主页设计,现在我已经移动了其他一些页面,我希望我的页眉和页脚在每个页面上显示相同。我尝试了这种基本方法来链接构成我的页眉/页脚的相同样式表在第二个HTML文件中(已经在主页中使用):

<link rel="stylesheet" href="footer.css" type="text/css"/>
<link rel="stylesheet" href="header.css" type="text/css"/>

我现在明白这不会起作用。服务器端脚本语言是我最好的选择吗?像PHP这样的东西?

如果有的话,是否有人能够将我与我在PHP中如何做到这一点的文章联系起来,我推测

 include

功能

感谢。

8 个答案:

答案 0 :(得分:2)

您目前只链接页眉和页脚的CSS。如果要将html包含为相同的内容,请创建两个单独的文件header.php和footer.php,然后将它们包含在每个网页中。

<?php include('path/to/header.php');?> // in the location you want the header in the page
<?php include('path/to/footer.php');?> // in the location you want the footer

基本上,你正在制作偏爱并将它们放在任何你想要的地方

答案 1 :(得分:1)

假设您有要包含在所有页面中的标题

<强>的header.php

My header

现在您可以将其包含在其他页面中:

<?php
include "header.php";
?>

并为页脚做同样的事情!

祝你好运!

答案 2 :(得分:0)

这将是最基本的PHP模板引擎:

<?php include 'header.html'; ?>
---HTML content
<?php include 'footer.html'; ?>

答案 3 :(得分:0)

将标题的代码放在单独的HTML文件中,然后在每个要显示的页面中使用以下内容:

<?php include '../header.html'; ?>

显然,将头文件放在自己的文件路径中。

答案 4 :(得分:0)

你必须创建另外两个文件:header.php&amp; footer.php。然后,您可以将它们包含在其他页面中:

<?php 
include "url/to/your/header.php"; 
include "url/to/your/footer.php";
?>

index.php中的示例:

<?php
include "views/header.php";
?>

// your content here html/php

<?php
include "views/footer.php";
?>

答案 5 :(得分:0)

创建2个文件“header.html”和“footer.html”,并将它们包含在php文件的顶部和底部。

<?php include("YourFile.html"); ?>

答案 6 :(得分:0)

您也可以使用聚合物。下载聚合物代码(javascript)并以这种方式包含其他文件:

<link rel="import" href="my-custom-element.html">

https://www.polymer-project.org/platform/html-imports.html

这是使用未来Web应用程序的最新方式。当您只关注HTML时,无需使用服务器端语言。

答案 7 :(得分:0)

创建一个名为header.php的文件,并将标题内容放在该文件中。然后创建另一个名为footer.php的文件,并将所有页脚内容放入其中。

然后,您可以将它们单独包含在所有页面中:

<?php include('header.php'); ?>

//content of that page here

<?php include('footer.php'); ?>