How can PHP and CSS be combined effectively to highlight active menu items in a navigation system?
To highlight active menu items in a navigation system, you can use PHP to determine which page is currently being viewed and then apply a specific CSS class to the corresponding menu item. This can be achieved by comparing the current page URL with the URLs of each menu item.
<?php
$current_page = $_SERVER['REQUEST_URI'];
?>
<ul class="navigation">
<li <?php if(strpos($current_page, 'home.php') !== false) echo 'class="active"'; ?>><a href="home.php">Home</a></li>
<li <?php if(strpos($current_page, 'about.php') !== false) echo 'class="active"'; ?>><a href="about.php">About</a></li>
<li <?php if(strpos($current_page, 'services.php') !== false) echo 'class="active"'; ?>><a href="services.php">Services</a></li>
<li <?php if(strpos($current_page, 'contact.php') !== false) echo 'class="active"'; ?>><a href="contact.php">Contact</a></li>
</ul>
Keywords
Related Questions
- Are there any best practices or tutorials available for implementing dynamic data updates in PHP forms?
- What are the potential pitfalls of using the switch statement in PHP for conditional formatting in a table?
- What are the potential pitfalls of not using $this->variable to access class variables in PHP methods?