0

just wondering if there was like a stylesheet for 'html'. What if I have a second 'html' file which contains a lot of the same 'html' as the first one. Instead of copying and pasting all the different bit can I use something like a stylesheet to store and make that 'html' appear on the second page?

2 Answers2

1

PHP METHOD: Just rename your html file from fileName.html to fileName.php so you can use php include for templating your site.

Here's an example of keeping the same navbar, say navbar.php on all pages:

navbar.php:

<ul>
  <li>Home</li>
  <li>About</li>
  <li>Contact</li>
</ul>

The above will be added to every page including the file.

INDEX.PHP:

<div class="nav">
    <?php include("pathTofile/navbar.php"); ?>
</div>

<div class="banner">
    ....
</div>

ABOUT.PHP:

<div class="nav">
    <?php include("pathTofile/navbar.php"); ?>
</div>

<div class="someDiv">
    ....
</div>

CONTACT.PHP:

<div class="nav">
    <?php include("pathTofile/navbar.php"); ?>
</div>

<div class="someDiv">
    ....
</div>

So if you need to make some edit to your navbar, you can just edit the navbar.php file and the changes will be reflected on all pages.

AndrewL64
  • 13,967
  • 6
  • 36
  • 64
0

This isn't possible using HTML alone but many server-side and client-side frameworks like PHP, Node.js, and Django allow you to insert one HTML file into another for example using the same header across a website.

Or you could use JavaScript to import the file. Here is an example using the jQuery library:

<html>
  <head>
    <script>
      $("#header").load("header.html");
    </script>
  </head>
  <body>
    <div id="header"></div>
  </body>
</html>
Philip Kirkbride
  • 17,347
  • 30
  • 101
  • 195