My approach involves utilizing PHP to identify the URL and match the page name (excluding the .php extension). By adding a common word like "contact" to multiple pages, each with the same class, I can easily add additional CSS properties using PHP.
To implement this method, it is necessary to save your pages with the file extension .php
.
Below is a demonstration. Customize the links and pages according to your requirements. The CSS class assigned to all links is .tab
, with an additional class called .currentpage
for the active link (following the PHP function), providing a space to override your CSS styles. Feel free to assign any names you prefer.
<?php # Utilizing REQUEST_URI
$currentpage = $_SERVER['REQUEST_URI']; ?>
<div class="nav">
<div class="tab
<?php
if(preg_match("/index/i", $currentpage)||($currentpage=="/"))
echo " currentpage";
?>"><a href="index.php">Home</a>
</div>
<div class="tab
<?php
if(preg_match("/services/i", $currentpage))
echo " currentpage";
?>"><a href="services.php">Services</a>
</div>
<div class="tab
<?php
if(preg_match("/about/i", $currentpage))
echo " currentpage";
?>"><a href="about.php">About</a>
</div>
<div class="tab
<?php
if(preg_match("/contact/i", $currentpage))
echo " currentpage";
?>"><a href="contact.php">Contact</a>
</div>
</div> <!--nav-->