I'm building a website and the menu is up there, working fine.
However, for it to be more user-friendly, I thought maybe I could put the login/logout button all the way to the right.
But there's a problem since I'm using Zend Framework, I can't assign a class to the <li> element, only to the <a> element.
As a normal working process, this list item is already always the last one, which makes it much easier to work with! I guess I could go ahead with some jQuery to detect this element and work around my problem, but then I discovered the "last-child" selector.
In my CSS file, I just added this:
#navigation UL LI:last-child {
float: right;
}
The "last-child" selector will make the last element of the selected type (here, li) follow the defined styles.
And there we have it. Instead of the element always being last but anywhere on the menu tabs, now the login (or logout) tab is always all the way to the far right!
Note that this is not supported in all browsers, but it simply won't change anything in others. Just a little bonus for those users who can keep their stuff updated!
Showing posts with label css. Show all posts
Showing posts with label css. Show all posts
Monday, August 1, 2011
Monday, June 20, 2011
[ZF] Adding ("current/active/highlight" and/or other) class to menu item
So I had this menu that finally decided to generate correctly from an XML file, cool. Now I thought of the two following thigs:
1. Adding CSS class to current menu element
For the first item, nothing better than to head over to Stack Overflow, as usual. As you can see in there, all you need is:
$uri = $this->_request->getPathInfo();
$active_nav = $this->view->navigation()->findByUri($uri);
$active_nav->active = true;
$active_nav->setClass("active");
However, this won't work in the bootstrap because the request parameters aren't available yet. If you have a menu generating like this, it's very likely you already have a method for generating it in your bootstrap file (e.g.: _initNavigation()). To access the request parameters and find the path info, add this beforehand:
$this->bootstrap('frontController');
$front = $this->getResource('frontController');
$front->setRequest(new Zend_Controller_Request_Http());
Then, just arrange the previous code to find the correct data:
$uri = $front->getRequest()->getPathInfo();
And there we go, the "active" class will be added to whichever tab you are on.
But wait, what if I wanted to add custom icons for each tab?
2. Adding icons to menu tabs
So you've tried adding the image to the label, like so:
<label><img src="/image/nav-1.png" alt="nav-1" />User</label>
... but it doesn't work.
The trick is rather easy, just add some stuff in your CSS file as a class, and add the following node to your XML file (I guess you could easily adapt it if you're loading data from an array):
<class>nav-1</class>
Only there's a slight problem: if you are on the active tab, the bootstrap will override the class and set it to "active".
I've actually found the solution on the first try (nice guess, I admit). In the bootstrap (see code above), just get the tab's current class and add "active" to it:
$current_class = $active_nav->getClass() . ' ';
$active_nav->setClass($current_class . 'active');
That's all there is to it, you can now have a dynamic and beautiful menu!
- Show which tab is active depending on the page
- Add icons to the tabs
1. Adding CSS class to current menu element
For the first item, nothing better than to head over to Stack Overflow, as usual. As you can see in there, all you need is:
$uri = $this->_request->getPathInfo();
$active_nav = $this->view->navigation()->findByUri($uri);
$active_nav->active = true;
$active_nav->setClass("active");
However, this won't work in the bootstrap because the request parameters aren't available yet. If you have a menu generating like this, it's very likely you already have a method for generating it in your bootstrap file (e.g.: _initNavigation()). To access the request parameters and find the path info, add this beforehand:
$this->bootstrap('frontController');
$front = $this->getResource('frontController');
$front->setRequest(new Zend_Controller_Request_Http());
Then, just arrange the previous code to find the correct data:
$uri = $front->getRequest()->getPathInfo();
And there we go, the "active" class will be added to whichever tab you are on.
But wait, what if I wanted to add custom icons for each tab?
2. Adding icons to menu tabs
So you've tried adding the image to the label, like so:
<label><img src="/image/nav-1.png" alt="nav-1" />User</label>
... but it doesn't work.
The trick is rather easy, just add some stuff in your CSS file as a class, and add the following node to your XML file (I guess you could easily adapt it if you're loading data from an array):
<class>nav-1</class>
Only there's a slight problem: if you are on the active tab, the bootstrap will override the class and set it to "active".
I've actually found the solution on the first try (nice guess, I admit). In the bootstrap (see code above), just get the tab's current class and add "active" to it:
$current_class = $active_nav->getClass() . ' ';
$active_nav->setClass($current_class . 'active');
That's all there is to it, you can now have a dynamic and beautiful menu!
Subscribe to:
Posts (Atom)