19

Let's say for example that I'm building a website with many sections and I wanted these sections to be modular, so each section is it's own html file. So basically I could include this little module anywhere I want on the main html file or maybe I could simply include navbars and footers onto other HTML pages without having to rewrite lines of code.

Is this possible with just HTML alone?

isherwood
  • 58,414
  • 16
  • 114
  • 157
jollyjwhiskers
  • 341
  • 1
  • 3
  • 12
  • 1
    "Server Side Includes" perhaps? – David Feb 28 '19 at 11:40
  • 2
    the best you could do without js or a server side language is to use iframes – Pete Feb 28 '19 at 11:41
  • 1
    Possible duplicate of [Include another HTML file in a HTML file](https://stackoverflow.com/questions/8988855/include-another-html-file-in-a-html-file) – Wai Ha Lee Feb 28 '19 at 18:06
  • 1
    See HTML Modules https://github.com/w3c/webcomponents/blob/gh-pages/proposals/html-modules-explainer.md and https://groups.google.com/a/chromium.org/forum/#!msg/blink-dev/ewfRSdqcOd8/w_Fr6rJ3DQAJ and https://github.com/w3c/webcomponents/blob/gh-pages/proposals/html-module-spec-changes.md and background discussion at https://github.com/w3c/webcomponents/issues/645 and some issue discussion at https://github.com/w3c/webcomponents/issues/783 – sideshowbarker Mar 01 '19 at 10:09

7 Answers7

28

Edit: An option worthwhile exploring is the object tag. You can include another file (of any kind) onto your page.

This seems like a more suitable option compared to the rest of options below.

<object type="text/html" data="file.html"></object>

It works on similar basis as HTML5 Import.

The object tag is part of HTML 4 therefore it's supported since IE6+, Firefox 2+, Chrome 1+ etc.


Using HTML5 Import. It does have very limited support/browsers implementing it.

<link href="extern.html" rel="import" />

Other than that you will need Javascript at bare minimum to import another file if you want to do it from client-side. There are variety of options available to do it from back-end depending on your tech.

Another possibility as Pete highlighted is the use of iframes (although I would prefer not to use them).


The use of HTML5 Imports is highly discouraged as you can see on here.

Here's few notes taken from the above link:

  • MS Edge status: Under Consideration

  • Chrome status: Deprecated

  • Firefox status: not-planned

  • WebKit status: Not Considering

  • Firefox has no plans to support HTML imports though for now it can be enabled through the "dom.webcomponents.enabled" preference in about:config

Adrian
  • 8,271
  • 2
  • 26
  • 43
  • 7
    "Very limited" is an understatement: **Obsolete since Google Chrome 73** – Jonathon Reinhart Feb 28 '19 at 11:43
  • @JonathonReinhart Added a little more "discouragement" at the end of the answer. I do agree it is an understatement - couldn't think of a more suitable wording though. – Adrian Feb 28 '19 at 11:49
  • @Adriani6 thanks! I recall watching some tutorial on youtube that uses this sort of method to keep things modular a few years ago and I'm fairly certain they used a server-side language to get it done. Was just curious to see if this was possible with the bare minimum. This answers that question. – jollyjwhiskers Mar 01 '19 at 08:13
  • @jollyjwhiskers See the edit. This might be a solution you're looking for! – Adrian Mar 01 '19 at 08:43
  • HTML Imports simply don't exist anywhere, since they have been removed from the specification years ago. – connexo Sep 09 '22 at 14:11
4

No, this cannot be done with plain HTML.

Alternatives:

  • Run server-side code like PHP
  • Use a static site generator to build your page
  • Use javascript on the page to bring in common components (although this doesn't work well when you're trying to eliminate code duplication between pages)
Jonathon Reinhart
  • 132,704
  • 33
  • 254
  • 328
3

You can't do it with plain HTML but you can use javascript inside script tag in your html file like this jQuery example

<html>
   <head>
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
      <script>
         $(document).ready(function(){
            $('#header').load("header.html");
            $('#footer').load("footer.html");
         });
      </script>
   </head>
   <body>
      <div id="header"></div>
      your content........
      <div id="footer"></div>
   </body>
</html>
pavelbere
  • 964
  • 10
  • 21
1

What you can do is create a php file where you write the html code, and in the index.php call that same file. For exemple:

Tis is your index .php and the file footer.php is the other file with the html that you want.

<html>
<body> 
<h1>Welcome to my home page!</h1>
<p>Some text.</p>
<?php include 'footer.php';?>
</body>
</html>

Or

you can also create an iframe, give it a specific size, in your html file and in the src, you can refer to the html file that you want

<iframe src="../../something.html"></iframe>
Luis Luis Maia Maia
  • 681
  • 1
  • 10
  • 30
  • 1
    I was looking for a HTML+PHP solution. This worked for me and is conceptually simple. Interestingly, the included files were plain HTML with a `.html` extension and it still worked. Small suggestion: the html was static so I used `include_once` - not sure if it is more efficient or not. – rugplots Mar 22 '22 at 18:38
  • 1
    You can even batch several includes in the same `` block. – rugplots Mar 22 '22 at 18:44
1

I realize this question was answered years ago, but I think it was not the best answer. I just learned a very simple way to include html files inside of other html files that is much better.

The trick is to name the your main .html file as a .shtml file. It is still an html file, but now the server will look inside for other files that you can include this way.

<!--#include file="Preamble.txt" -->

Your Preamble.txt can be another .html file that you want to be included in your main file.

This is called SSI (Server Side Includes). I guess it makes sense that the server needs to look inside the .html file it is sending to a client. I always assumed that it was always automatically done that way and that you didn't need to specify it. Turns out only certain file types are checked for included files by the server.

BTW, I noted the following cryptic answer, but only understood it after I learned about the SSI from other sources. Unfortunately, I got so much bad information in the past, I never realized there was an easy way to make web pages modular. I was doing it wrong for over 15 years.

"Server Side Includes" perhaps? – David Feb 28, 2019 at 11:40

0

there is a old tag that call <frameset> it used exacly for what you want. but this days it deprecated

<frameset cols="50%,50%">
  <frame src="https://developer.mozilla.org/en-US/docs/Web/HTML/Element/frameset" />
  <frame src="https://developer.mozilla.org/en-US/docs/Web/HTML/Element/frame" />
</frameset>
pery mimon
  • 7,713
  • 6
  • 52
  • 57
0

If you’re building a static website you can’t use SSI

  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 08 '23 at 14:46