0

I am making a CSS navigation bar, which is stored in a folder called 'includes'. Basically what I want is, when a link is clicked, for it to become active. Im linking to this folder with "include('includes/header.php');"

enter image description here

My CSS is:

nav ul li a {
font-size:22px;
color:#fff;
height:67px;
line-height:67px;
text-decoration:none;
min-width:101px;
text-align:center;
float:left;
background: grey;
border-right:1px solid black;}

nav ul li a.current, nav ul li a:hover {padding-bottom:5px;}

The content of header.php is:

<header>
<div class="container">
<nav>
    <ul>
      <li><a href="index.php">Home</a></li>
      <li><a href="services.php">Services</a></li>
      <li><a href="#">Blog</a></li>
      <li><a href="/forum/index.php">Forums</a></li>
      <li><a href="contact.php">Contact</a></li>
      <li><a href="#">Account</a></li>
    </ul>
</nav>

How do I go about using this method, and having each link made active when clicked?

2
  • 1
    Could you share some of the content of your header.php code? Commented Jan 5, 2014 at 15:13
  • <header> <div class="container"> <h1><a href="index.php">FictionSolutions</a></h1> <nav> <ul> <li><a href="index.php">Home</a></li> <li><a href="services.php">Services</a></li> <li><a href="#">Blog</a></li> <li><a href="/forum/index.php">Forums</a></li> <li><a href="contact.php">Contact</a></li> <li><a href="#">Account</a></li> </ul> </nav> </div> </header> Commented Jan 5, 2014 at 15:15

2 Answers 2

1

You could do something like this for the header

<ul>
  <li><a <?php if (isThisTheCurrentPage) echo 'class="current"'?> ...>...</a></li>
  <li><a <?php if (isThisTheCurrentPage) echo 'class="current"'?> ...>...</a></li>
  <li><a <?php if (isThisTheCurrentPage) echo 'class="current"'?> ...>...</a></li>
  ...
</ul>

Once I had to do something similar, and I achieved this by setting some variables at the beginning of each php file. Then, the header.php must test these variables in the if statements.

Sign up to request clarification or add additional context in comments.

Comments

0

I did something like this in the past very similar to what Romain said:

I named each page something like and included the nav file:

<?php $page = 'one';
require_once'./template/nav.php';?>

Here is the content of my nav file:

<ul class="nav navbar-nav">
<li <?php echo ($page == 'one') ? "class='active'" : ""; ?> >
<a href="http://example.com">Home</a></li>
<li <?php echo ($page == 'two') ? "class='active'" : ""; ?> ><a
href="http://example.com/page-two">Page two</a></li>
<li <?php echo ($page == 'three') ? "class='active'" : ""; ?> ><a
href="http://example.com/page-three">page three</a></li>
</ul>

Hope that helps

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.