I've used HTML, CSS, and JS before but mostly just for projects that needed one or two pages. Now I'm working on a more complex project with a lot of pages. Every page needs to include an identical header/nav on top. Most pages also need links to various style sheets. I'm sure there must be an easier way then changing everything on every page individually. Is there a way to have all the pages use the same file for the header? (Also I'd prefer not to use JS or PHP just pure HTML/CSS which is why this answer, did not help)
-
You can't with pure HTML/CSS, or you need to duplicate the header/nav on each file. – Magicprog.fr Nov 07 '14 at 09:27
-
There isn't really an easier answer than using PHP. Even if you don't use PHP now, you'll probably want a contact form or some other functionality which will need PHP, so it's a losing battle! (Replace PHP with whatever language you like!) – Rich Bradshaw Nov 07 '14 at 09:27
-
@Magicprog.fr what would you suggest as the most simple method then? PHP? – iicaptain Nov 07 '14 at 09:28
-
@ivoilic Yes, the PHP seems to be the easiest way : `` but you can do this with an ajax (JS) request too. – Magicprog.fr Nov 07 '14 at 09:30
-
Do not do this using AJAX. Magicprog.fr's PHP is the easiest - just put that in the right place in your file. – Rich Bradshaw Nov 07 '14 at 09:32
-
1You could use Jekyll : http://jekyllrb.com/ – Fabrizio Calderan Nov 07 '14 at 09:34
-
How about some JS framework, like angular? – Vucko Nov 07 '14 at 09:43
1 Answers
OK, this looks like it actually needs an answer.
There are various ways to do what you want, and it depends on whether you are making a webpage or Single Page App.
If you making a single page app, then you would only have one html file with the header and footer anyway, then you'd use something like HTML Imports, or Ember or Angular to change the content. In this case, you don't have a problem, as the header/nav/footer only exist once anyway.
If you are not making that, then you go down two routes,
1) Use whatever server side language you want (PHP, ASP.NET, Python, Ruby, Node.js etc etc etc). If you are using PHP, make files called header.php, nav.php and footer.php. Then in your index.php file use:
<?php include('header.php'); ?>
<?php include('nav.php'); ?>
<h1>Title of Page</h1>
<?php include('footer.php'); ?>
2) The preferred option - grab a templating library, e.g. PHP's Twig, or use whatever is built into your framework, e.g. Laravel's Blade.
Use those tools to build up the page.
If you can't use PHP or any other server side language, then sort yourself out so you can. Hosting with PHP particularly is very cheap and very easy to sort out.

- 71,795
- 44
- 182
- 241
-
I'm not sure why this answer is downvoted. It's perfectly legitimate. Here's a +1 for you. – Terry Nov 07 '14 at 10:00
-
Downvoted because it just repeated the answer he referenced in his question: https://stackoverflow.com/questions/8211009/how-to-display-same-header-and-footer-on-different-pages - the OP knows how to include using PHP and asked for none JS or SSI based answers. – Ryan McDonough Nov 07 '14 at 10:03