What's the quickest and easiest way to add the "active" class to a link, so it can be styled? I'm developing an app in CI, and I'd like a quick easy way to do this automatically.
jQuery is an option too...
What's the quickest and easiest way to add the "active" class to a link, so it can be styled? I'm developing an app in CI, and I'd like a quick easy way to do this automatically.
jQuery is an option too...
Depends on how you're outputting your link HTML.
If you're using the URL Helper module, then you can call the anchor()
function to create your links, and pass it an array of attributes as the third parameter, ie:
$this->load->helper('url');
echo anchor('url/path', 'Click here', array('class' => 'active'));
If you're just outputting the HTML manually in your templates/views, obviously you can just create the class attribute yourself in the HTML.
If you have a lot of navigation items you can do it this way (very simplified)...
<ul>
<li<?= if ( $_SERVER['REQUEST_URI'] == '/contact' ): ?> id="active"<?php endif; ?>><a href="">contact</a></li>
</ul>
You'll have to edit it for your needs...
If you don't have that many nav items an easier way is to give each page a body id and then use css to make it active.
<style type="text/css">
body#contact #contact-nav { font-weight:bold; }
</style>
<body id="contact">
<ul id="navigation">
<li id="contact-nav"><a href="">contact</a></li>
</ul>
You should really be using the CodeIgniter URI Class to do this instead of the $_SERVER['REQUEST_URI']
$this->uri->uri_string()
if ( $this->uri->uri_string() == '/contact' )
^^ that is the preferred way to do things due to some complexities that can happen with codeigniter's routing features