I am making a huge website, with on all pages a navbar. Is it possible to create the navbar in a .html file and import it into all the other pages, and if so, how?
-
Are you making it in php. If yes, then create a header.php and include it in all your pages like include 'header.php' – Manish Jangir Jan 01 '14 at 11:48
-
Yes if you're using php then use include keyword. – user2727841 Jan 01 '14 at 11:49
-
This is not a free _do your work_-site. Show some effort. – kleinfreund Jan 01 '14 at 11:49
-
Not with HTML alone but yes you can with JavaScript (jquery) – KV Prajapati Jan 01 '14 at 11:49
-
Well, you can in theory use ` – Shadow The GPT Wizard Jan 01 '14 at 11:53
-
@popnoodles I did add a warning. There are worse things than that, believe me. And server side was never mentioned here, so it's not a correct dupe. – Shadow The GPT Wizard Jan 01 '14 at 12:00
-
1"Is it possible to create the navbar in a .html file and import it into all the other pages" OP doesn't know the term Server Side Includes, they just know what they want, which is Server Side Includes. – Popnoodles Jan 01 '14 at 12:02
-
@ShadowWizard Out of interest can you tell me what worse ways there are than using frames to include common content? – Popnoodles Jan 01 '14 at 12:04
-
@popnoodles I don't mean in this specific topic, just worse things web developer can do with a site. – Shadow The GPT Wizard Jan 01 '14 at 12:07
5 Answers
You can do this with HTML alone using Server Side Includes. Simplest example:
index.html
<html><head><title>Test</title></head>
<body>
<!--#include file="navbar.shtml" -->
</body>
</html>
navbar.shtml
<ul class="nav">
<li><a href="/">Home</a></li>
<li><a href="/about">About</a></li>
</ul>
What you should never do is use framesets or iframes to do this. https://stackoverflow.com/a/15938545/822711
Please note, this will not work using the file://
protocol, it needs to run on a web server as it would in a live environment. This could be on a private or public server, or localhost using a server running on your computer such as wamp.

- 1
- 1

- 28,090
- 2
- 45
- 53
-
The link you posted refers to frames, not the HTML tag ` – Shadow The GPT Wizard Jan 01 '14 at 12:11
-
2I'm sorry, using any type of frame to load navigation is a horrendous idea for any site no matter how simple. – Jan 01 '14 at 12:15
-
Before give this answers have you checked it ?Please give a full example.Your code is not working! – Alimon Karim Jan 01 '14 at 12:43
-
1@user3085661 my code is not working where? On your localhost? On some random server? Did you read the wiki? Is your server set up to recognise SSI? "Is not working" is not a valid description of the problem – Popnoodles Jan 01 '14 at 12:53
-
@user3085661 best guess is you're trying to run **server side** code while opening the page as `file://`. I've updated my answer accordingly. Thanks. – Popnoodles Jan 01 '14 at 13:01
-
I prefer try to use Jquery,Like as
<!doctype html>
<html>
<head>
<title>Home page</title>
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script>
$(function(){
$('.header').load("header.html");
});
</script>
</head>
<body>
<div class="header"></div>
</body>
</html>
In same folder open a file with name header.html
.Same thing you can apply for footer.

- 4,354
- 10
- 43
- 68
-
Just so you know, answers that suggest using jQuery to do something simple that can easily be done at root level often get marked down. – Popnoodles Jan 01 '14 at 12:21
-
-
You're introducing a client-side framework to do something that is easily achievable server side, and works far better done server side. No FOUC, no extra page load, no extra framework. – Popnoodles Jan 01 '14 at 12:26
-
If he want to convert it in server side he have to convert file type,like as .php etc.In here he don't need to change anything without this small code. – Alimon Karim Jan 01 '14 at 12:28
-
*"If he want to convert it in server side he have to convert file type,like as .php"* No he/she doesn't. HTML can be included in HTML. – Popnoodles Jan 01 '14 at 12:30
-
Please give a full example in your answers.I want to see how it is working.Thank you. – Alimon Karim Jan 01 '14 at 12:44
-
-
This way to load html inside another html works...but that is very slow. Can you suggest some other way ? This way make page very slow. – Gaurav vijayvargiya Jan 06 '16 at 17:07
1) In html we can load other files into one html file using Iframe
<!DOCTYPE html>
<html>
<body>
<iframe src="header.html">
<p> display</p>
</iframe>
</body>
</html>
2) We can use jquery function to load the file into some specific div.
<script>
$(function(){
$('#header').load("header.html");
});
</script>
3) Use other languages like php , .net for that we use php include and require for that
**Apart from using iframe there is no other way in html that we can include one html file to another.**

- 2,647
- 4
- 24
- 38
with PHP it's possible, but you have to change the files to .php files.
put this in the main file:
<?php
include("navbar.php");
?>
I don't know a good way with HTML
-
I'll change to php than, If that's the only *good* option.. thanks :) – user3032715 Jan 01 '14 at 19:38
For this you need to add server side language if you are using PHP you can create a nav.php file. In this file you can add the complete HTML of your navigation and you can include this PHP file in your code instead of putting navigation HTML. Like this
<?php include("nav.php");?>

- 623,577
- 216
- 2,003
- 1,567

- 187
- 10
-
-
-
I mentioned .php extension.@Shadow Wizard is that any way to include file in HTML? – Rizwan Sultan Jan 01 '14 at 11:54
-
@RizwanSultan there are other server side languages out there, not only PHP. Besides, your whole answer is just unreadable block of code, proper answer should be formatted. I would do it myself but no point if you're not willing to spend that little extra effort to do that yourself. – Shadow The GPT Wizard Jan 01 '14 at 11:58
-
@Shadow Wizard thanks buddy now I add some more details it's bit confusing formatting in Stackoverflow I just start using it.But Hope I will learn formatting standards. – Rizwan Sultan Jan 01 '14 at 12:06
-
It's really not hard to format, thousands of people are doing it just fine every day... anyway others have done it for you. – Shadow The GPT Wizard Jan 01 '14 at 12:08