codeigniter add active css class to link how to

codeigniter add active css class to link how to  using -'php,css,codeigniter'

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...

asked Sep 15, 2015 by WandaDtuslnq
0 votes

5 Answers

0 votes

You should really be using the CodeIgniter URI Class to do this instead of the $_SERVER['REQUEST_URI']


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

answered Sep 15, 2015 by AdeValenzue
0 votes

If you have a lot of navigation items you can do it this way (very simplified)...

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.

answered Sep 15, 2015 by AlissaKLHag
0 votes

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:

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.

answered Sep 15, 2015 by MayraBaughma
0 votes

To adding active class (class=”active”) to a link , I’ve done it by doing this:
In view

In controller


Maybe it's not your solution. but it works for me.

answered Sep 15, 2015 by TrinaMacquee
0 votes

You can do this way by creating helper with following

        return ($class == $controller) ? 'active' : '';

then apply it in menu view

  • Services
  • answered Sep 15, 2015 by Janice1235nh