'Is there a way to to use the nav html code on every page without adding it to each html file? (keeping it DRY)
I usually use PHP where we have a header.php file and a footer.php. Which means you don't need to repeat the same blocks of code for each of these sections on your other pages.
I'm creating a site with just HTML that has a few pages, but I don't want to use the nav code on each of my html files.
Is there any way to achieve what I want with HTML?
Solution 1:[1]
I normally keep using php for this. Even if there is no php code, only the include. Else you need to use javascript for it: https://devdojo.com/tnylea/include-html-inside-of-html
But using php is the most easy and standard way. Why wouldn't you want to use php and only html?
EDIT: You could use iframe for it by the way.
Solution 2:[2]
The way this is done when one wishes not to use php is by using an iframe. I have included an example for you below!
iframe.html
<!doctype html>
<html lang="en">
<head>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
</head>
<body>
<nav class="nav justify-content-center|justify-content-end">
<a class="nav-link active" href="#">Active link</a>
<a class="nav-link" href="#">Link</a>
<a class="nav-link disabled" href="#">Disabled link</a>
</nav>
<!-- Bootstrap JavaScript Libraries -->
<script src="https://cdn.jsdelivr.net/npm/@popperjs/[email protected]/dist/umd/popper.min.js"
integrity="sha384-IQsoLXl5PILFhosVNubq5LC7Qb9DXgDA9i+tQ8Zj3iwWAwPtgFTxbJ8NT4GN1R8p"
crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js"
integrity="sha384-cVKIPhGWiC2Al4u+LWgxfKTRIcfu0JTxR+EQDz/bgldoEyl4H0zUF0QKbrJ0EcQF"
crossorigin="anonymous"></script>
</body>
</html>
home.html
<!doctype html>
<html lang="en">
<head>
<title>Iframe Nav Bar</title>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS v5.0.2 -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<style>
iframe {
width: 100vw;
height: 20vh;
}
</style>
</head>
<body>
<iframe src="iframe.html" frameborder="0"></iframe>
<!-- Bootstrap JavaScript Libraries -->
<script src="https://cdn.jsdelivr.net/npm/@popperjs/[email protected]/dist/umd/popper.min.js" integrity="sha384-IQsoLXl5PILFhosVNubq5LC7Qb9DXgDA9i+tQ8Zj3iwWAwPtgFTxbJ8NT4GN1R8p" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js" integrity="sha384-cVKIPhGWiC2Al4u+LWgxfKTRIcfu0JTxR+EQDz/bgldoEyl4H0zUF0QKbrJ0EcQF" crossorigin="anonymous"></script>
</body>
</html>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Web Dev |
