This demo assumes the author is writing a multi-page web site and wishes to use the same menu on all of the pages. For the convenience of this demo, these files are at the same directory level. None are in sub-directories.
First, create and style the page, including the menu, in an .html version of the page.
For example:
home.html
<!DOCTYPE html>
<html lang="en-ca">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<link rel="stylesheet" href="./mycss.css" type="text/css">
<title>HOME (page 1/5)</title>
<!--
This is an HTML page with four major subdivisions: header, nav, main, footer.
The navigation subdivision is the focus of this example.
The site, including the nav menu, would be styled using CSS in the external stylesheet.
-->
</head>
<body id="home"> <!-- id indicates page; is used by menu CSS to indicate active page. No JS needed. -->
<div class="outer">
<header>
<figure>
<img src="http://placehold.it/450x115/" alt="MyCompany Logo" width="450" height="115">
<h1>My Home Maintenance Co.</h1>
<figcaption>
<p>No home repair job in the Valley area is too big for us.</p>
</figcaption>
</figure>
</header>
<nav>
<ul>
<li class="home"><a href="home.html">home</a></li>
<li class="aboutus"><a href="aboutus.html">about us</a></li>
<li class="services"><a href="services.html">services</a></li>
<li class="projects"><a href="projects.html">projects</a></li>
<li class="contact"><a href="contact.html">contact</a></li>
</ul>
</nav>
<main>
<div></div> <!-- Use whatever tags are appropriate for content. -->
</main>
<footer>
<div></div> <!-- Use whatever tags are appropriate for content. -->
</footer>
</div>
</body>
</html>
This sample CSS styles the menu only.
mycss.css
/* This demo stylesheet ONLY styles the nav menu. */
a {
color:#33348e;
text-decoration:none;
text-transform:uppercase;
}
nav {
background-color:#33348e;
font-family:verdana,arial,times,serif;
font-size:.875em;
overflow:hidden;
padding-left:4%;
}
nav ul {
padding:0;
margin:0;
}
nav ul li {
list-style-type:none;
float:left;
position:relative;
padding:0;
margin:0;
}
nav ul li a {
display:block;
color:#fff;
padding:.875em .625em;
margin:0;
}
nav ul li a:hover,
nav ul li a:focus {
background-color:#fff;
color:#33348e;
text-decoration:underline;
font-weight:normal;
}
/* These selectors style the active page's menu */
#home .home a,
#aboutus .aboutus a,
#services .services a,
#projects .projects a,
#contact .contact a {
color:#33348e;
background-color:#fff;
pointer-events:none;
}
If opened in one’s browser, the page should look approximately like this:
Second:
Make a copy of the .html file and give the copy a .php suffix.
Put comment marks around the HTML that is to go in a PHP include file. Like this:
Then cut the text from between the comment marks and paste it into a new file with a .php suffix. In this example the filename is mynav.php. IMPORTANT! Change the suffixes of the links in “mynav.php” from .html to .php.
Why the cut-and-paste routine? Because I am fussy about uniformly formatted code. If the original HTML code is nicely formatted, the code will appear just as properly formatted when imported from the .php file.
Third:
Now go back to the HTML page with the .php suffix and add a php call between the comment marks. As a helpful gesture, do not delete the comment marks. Leave them so anyone who “Views Source Only…” can tell that the code between those comment marks is being summoned from a PHP file.
</header>
<!-- BEGIN mynav.php INCLUDE -->
<?php include "./mynav.php"; ?>
<!-- END mynav.php INCLUDE -->
<main>
The complete HTML for the home.php and mynav.php files follows:
home.php
<!DOCTYPE html>
<html lang="en-ca">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<link rel="stylesheet" href="./mycss.css" type="text/css">
<title>HOME (page 1/5)</title>
<!--
This is an HTML page with four major subdivisions: header, nav, main, footer.
The navigation subdivision is the focus of this example.
The site, including the nav menu, would be styled using CSS in the external stylesheet.
-->
</head>
<body id="home"> <!-- id indicates page; is used by menu CSS to indicate active page. No JS needed. -->
<div class="outer">
<header>
<figure>
<img src="http://placehold.it/450x115/" alt="MyCompany Logo" width="450" height="115">
<h1>My Home Maintenance Co.</h1>
<figcaption>
<p>No home repair job in the Valley area is too big for us.</p>
</figcaption>
</figure>
</header>
<!-- BEGIN mynav.php INCLUDE -->
<?php include "./mynav.php"; ?>
<!-- END mynav.php INCLUDE -->
<main>
<div></div> <!-- Use whatever tags are appropriate for content. -->
</main>
<footer>
<div></div> <!-- Use whatever tags are appropriate for content. -->
</footer>
</div>
</body>
</html>
mynav.php
<nav>
<ul>
<li class="home"><a href="home.php">home</a></li>
<li class="aboutus"><a href="aboutus.php">about us</a></li>
<li class="services"><a href="services.php">services</a></li>
<li class="projects"><a href="projects.php">projects</a></li>
<li class="contact"><a href="contact.php">contacts</a></li>
</ul>
</nav>
Upload the two .php files to a server to test the menu. It should look and behave exactly like the .html version.
This menu css uses a technique popularly known as “matching classes” to target the menu item of the current/active page. It highlights the menu item whose ID appears in the <body>
tag and matches the classname in the nav list item. (I’m using an ID in the <body>
tag instead of a classname. ) NO JavaScript required.
And that, in a nutshell, is all there is to it.