0

I'm building a fairly simple site, and I need to alter a background image based on what section the user is in. The main menu only has 2 levels. I'm unsure where I should hook into in order to be able to get the current section (i.e. the root menu item of the current page, see example below) so I can adjust the classes on the page. I was looking at this article as a possible solution, but I only want to alter CSS classes, and that seems like an awful lot of code for such a simple task.

Main Manu:
-- Section 1
   --- Page A
   --- Page B
   --- Page C
-- Section 2
   --- Page D
   --- Page E <- Active page, CSS class "section_2"
   --- Page F
  • If your theme is any good, you will find a lot of information in classes on <body> element, and in classes on all <div> elements. Open your site in Firebug or similar tool and see. Maybe the classes you need are already there? – Mołot May 13 '14 at 21:36
  • I'm using Bootstrap as the sub-theme. There are quite a few classes on body, but they're actually too specific, i.e. there's `page-node-70' as a class, but I need to figure out what that node's root menu item is. – ken.dunnington May 14 '14 at 13:19
0

If it is just additional classes you need, you can add additional and unique classes to menu items using the Menu CSS Names or Menu Attributes modules.

Or, you can effect the element of another DIV on hover using pure CSS like below...

HTML

<a id="menu-item" href="#">Menu Item</a>

<div id="container">
    <h1>Change This Background</h1>
</div>

CSS

#container {
    background:blue;
}

#menu-item:hover + #container {
    background:red;
}

See Fiddle here... Affect Another Div on Hover

  • 1
    I need to alter the classes on another element outside of the menu. I could do this with JavaScript very easily, but it seems like something Drupal should be able to make possible on the server. – ken.dunnington May 14 '14 at 13:20
  • Please see updated answer for CSS only solution. – Collins May 14 '14 at 13:35
0

I solved this by using the Context module. This easily allowed me to add a custom class to the body depending on a selected top-level menu item. Works like a charm.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.