HTML
Web Development
Coding
File Integration
HTML Embedding

How to include another HTML file in an HTML file

Codemia Knowledge Hub ·

To include one HTML file in another, use a server-side include (SSI or PHP) when you want the content served as part of the page, or a client-side method (iframe, object, or JavaScript such as jQuery's load()) when you cannot process pages on the server. Includes are typically used for shared headers, footers and navigation bars so the markup is not duplicated across pages.

Using the iframe element

An iframe (inline frame) embeds another HTML document inside the current one. It suits ads, widgets and other external content.

html
<iframe src="header.html" style="height: 100px; width: 100%; border: none;"></iframe>
  • Pros: Easy to implement; supports content from different origins.
  • Cons: Can slow page performance; less SEO-friendly; may have accessibility issues.

Using the object element

The object element is similar to iframe but offers more generic document embedding.

html
<object type="text/html" data="navbar.html" style="width: 100%; height: 50px;"></object>
  • Pros: An HTML5 standard approach for embedding different types of content.
  • Cons: Limited by cross-origin policies; not as widely adopted for all HTML content types.

Using server-side includes (SSI)

SSI directives are placed in HTML pages and evaluated by the server on request. They work well for content shared across many pages, such as headers, footers or menus.

html
<!--#include file="footer.html" -->
  • Pros: Processed on the server, so it is transparent to the client and reduces client-side requests.
  • Cons: Requires server configuration (for example, Apache or Nginx must be set up to handle SSI); works only on servers that support it.

Using JavaScript or jQuery

JavaScript, often with jQuery, can load HTML into the page dynamically without a reload.

html
1<div id="header"></div>
2<script>
3$(function(){
4    $("#header").load("header.html");
5});
6</script>
  • Pros: Flexible; good for single-page applications; works client-side.
  • Cons: Requires JavaScript on the client; not SEO-friendly unless rendered server-side first.

Using PHP or another server-side language

PHP includes are one of the most common methods in traditional web development.

php
<?php include 'navigation.html'; ?>
  • Pros: Very efficient; does not depend on client-side capabilities.
  • Cons: Tied to the server-side environment; needs a server that can interpret PHP.

Comparing the methods

MethodClient/Server-SideSEO FriendlyUsability & Maintainability
iframeClient-sidePoorEasy
objectClient-sidePoorModerate
SSIServer-sideGoodModerate
JavaScript/jQueryClient-sidePoorGood
PHPServer-sideGoodExcellent

Choosing a method

  • Performance: client-side solutions may render more slowly than server-side ones.
  • SEO: some methods do not serve content in a way search engines can easily read.
  • Security: make sure included files do not open vulnerabilities such as cross-site scripting (XSS).

The right choice depends on your server resources, security requirements and expected traffic.


Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Basis Lab
No course covers this one yet

Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.

Build my course
ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design