<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Darren Krape &#187; CSS</title>
	<atom:link href="http://www.darrenkrape.com/tag/css/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.darrenkrape.com</link>
	<description>- web design and life stuff</description>
	<lastBuildDate>Mon, 16 Aug 2010 01:34:01 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>CSS drop-down menus: keeping the top level selected when hovering over a sub-menu, now in jQuery</title>
		<link>http://www.darrenkrape.com/categories/design-and-development/css-drop-down-menus-2/</link>
		<comments>http://www.darrenkrape.com/categories/design-and-development/css-drop-down-menus-2/#comments</comments>
		<pubDate>Sun, 06 Sep 2009 15:17:38 +0000</pubDate>
		<dc:creator>Darren</dc:creator>
				<category><![CDATA[Design and Development]]></category>
		<category><![CDATA[Journal]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://www.darrenkrape.com/?p=233</guid>
		<description><![CDATA[Awhile back I wrote a JavaScript tutorial on how to keep the top level menu item selected when hovering over a drop down menu. Since then I&#8217;ve become a strong user and proponent of the JavaScript library jQuery. Not only is it easy to use, it also can help simplify your code-base very significantly. I&#8217;ve [...]]]></description>
			<content:encoded><![CDATA[<p>Awhile back I wrote a <a href="http://www.darrenkrape.com/fun/journal/drop-down-menus-persistent-top-level-menu-styling/">JavaScript tutorial</a> on how to keep the top level menu item selected when hovering over a drop down menu. Since then I&#8217;ve become a strong user and proponent of the JavaScript library <a href="http://www.jquery.com">jQuery</a>. Not only is it easy to use, it also can help simplify your code-base very significantly. I&#8217;ve explained the updated code below. As you can see, it is much simpler.<br />
<span id="more-233"></span></p>
<ul>
<li><a class="code_example" href="/examples/dropdown2/">View this code in action</a></li>
<li><a class="jump" href="#complete_code">View complete code</a></li>
</ul>
<h3>How this works</h3>
<p>There are two main components of the JavaScript, first the code for all browsers to keep the top level selected and a second code block to add the hover state for IE6 (since it only supports <code>:hover</code> pseudo-classes on anchors, not list elements as we use here).</p>
<p>First, you&#8217;ll need to add the jQuery equivalent of &#8220;onLoad&#8221;: <code>$(function() {</code>. Fortunately, the multiple onLoad and caching issues we addressed in the earlier tutorial are now handled in jQuery, so no need to use the &#8220;onEvent&#8221; function from the earlier tutorial. You&#8217;ll also sometimes see this as <code>$(document).ready(function() {</code>. The functionality is the same, so feel free to use either syntax.</p>
<h4>Code for persistent top-level menu formatting</h4>
<p>Next, the general code for maintaining the hover formatting on the top level. Basically, this function iterates through each of the drop-down <code>&lt;ul&gt;</code>s and adds a <code><a href="http://docs.jquery.com/Events/hover">hover</a></code> function to each. So, when a user hovers over a drop down menu, jQuery finds the parent of that drop down, finds all anchor tags, slices out all the anchors but the first and, finally, toggles the &#8220;active&#8221; class.</p>
<pre><code>$("#menu ul").each(function(i){
  $(this).hover(function(){
    $(this).parent().find("a").slice(0,1).addClass("active");
  },function(){
    $(this).parent().find("a").slice(0,1).removeClass("active");
  });
});
</code></pre>
<h4>Internet Explorer 6 functionality</h4>
<p>To make this work in IE6, we need to add some additional code. First, we use jQuery&#8217;s built in <a href="http://docs.jquery.com/Utilities/jQuery.browser">browser sniffer</a> to only target <abbr title="Microsoft Internet Explorer">MSIE</abbr> versions below 7. It then works fundimentally like the code above and the Suckerfish drop downs from the <a href="http://www.darrenkrape.com/fun/journal/drop-down-menus-persistent-top-level-menu-styling/">earlier tutorial</a>. JavaScript cycles through each drop-down <code>&lt;ul&gt;</code> and adds a function that tells the its parent anchor tag to take on a particular class when the browser detects a mouseover on the menu.</p>
<pre><code>if($.browser.msie &amp;&amp; ($.browser.version &lt; 7)) {
  $("#menu").each(function(i){
    $(this).find("li").hover(function(){
      $(this).addClass("sfhover");
    },function(){
      $(this).removeClass("sfhover");
    });
  });
}
</code></pre>
<h4>In closing</h4>
<p>Be sure to add the closing parenthesize and bracket for the jQuery on ready state: <code>});</code>.</p>
<h3 id="complete_code">Complete Code</h3>
<pre class="complete"><code>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;

&lt;head&gt;

  &lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /&gt;
  &lt;title&gt;Menu Example&lt;/title&gt;

  &lt;style type="text/css" media="screen"&gt;

  body {
    margin: 20px;
    background: #fff;
    font-family: Verdana, Helvetica, sans-serif;
    font-size: 9pt;
  }

  * { margin: 0; padding: 0; }
  li { list-style: none; }
  a { text-decoration: none; }

  #menu li {
    float: left;
    display: block;
    font-size: 8pt;
  }

  #menu li a {
    position: relative;
    padding: 6px 10px;
    display: block;
    font-weight: bold;
    color: #333;
  }

  #menu li a:hover, #menu li a.active {
    background: #999;
    color: white;
  }

  #menu li ul {
    left : -999em;
    position : absolute;
  }

  #menu li ul li { float : none; }

  #menu li ul a {
    width : 136px;
    padding : 8px 10px;
    background : #f4f4f4;
    border-bottom : 1px solid white;
    font-weight : normal;
  } 

  #menu li ul a:hover {
    background: #FFCC99;
    color: #333;
  }

  #menu li:hover ul, #menu li.sfhover ul {
    left: auto;
    border-top: 1px solid #999;
  }

  &lt;/style&gt;

  &lt;script type="text/javascript" src="jquery.js"&gt;  &lt;/script&gt;

  &lt;script type="text/javascript" src="jquery.js"&gt;
  &lt;!--

  $(function() {

    //Preserves the mouse-over on top-level menu elements when hovering over children
    $("#menu ul").each(function(i){
      $(this).hover(function(){
        $(this).parent().find("a").slice(0,1).addClass("active");
      },function(){
        $(this).parent().find("a").slice(0,1).removeClass("active");
      });
    });

    // IE6 Fix: Drop-down fix due to lack of support for :hover on list elements
    if($.browser.msie &amp;&amp; ($.browser.version &lt; 7)) {
      $("#menu").each(function(i){
        $(this).find("li").hover(function(){
          $(this).addClass("sfhover");
        },function(){
          $(this).removeClass("sfhover");
        });
      });
    }

  });

  // --&gt;
  &lt;/script&gt;

&lt;/head&gt;

&lt;body&gt;

  &lt;ul id="menu"&gt;
    &lt;li&gt;&lt;a href="#"&gt;Styles&lt;/a&gt;

    &lt;ul&gt;
      &lt;li&gt;&lt;a href="#"&gt;Red/White&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href="#"&gt;Ros?©/Blush&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href="#"&gt;Sparkling&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href="#"&gt;Dessert&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href="#"&gt;Fortified&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href="#"&gt;Fruit&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href="#"&gt;Ice Wine&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;

    &lt;/li&gt;
    &lt;li&gt;&lt;a href="#"&gt;Whites&lt;/a&gt;

    &lt;ul&gt;
      &lt;li&gt;&lt;a href="#"&gt;Albari?±o&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href="#"&gt;Chardonnay&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href="#"&gt;Chenin blanc&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href="#"&gt;Muscat&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href="#"&gt;Pinot blanc&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href="#"&gt;Pinot gris&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href="#"&gt;Riesling&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href="#"&gt;Sauvignon blanc &lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href="#"&gt;S?©millon&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;

    &lt;/li&gt;
    &lt;li&gt;&lt;a href="#"&gt;Reds&lt;/a&gt;

    &lt;ul&gt;
      &lt;li&gt;&lt;a href="#"&gt;Cabernet Sauvignon &lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href="#"&gt;Malbec &lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href="#"&gt;Merlot &lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href="#"&gt;Pinot noir &lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href="#"&gt;Syrah/Shiraz &lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href="#"&gt;Zinfandel&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;

    &lt;/li&gt;
    &lt;li&gt;&lt;a href="#"&gt;Noted Regionals&lt;/a&gt;

    &lt;ul&gt;
      &lt;li&gt;&lt;a href="#"&gt;Amarone&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href="#"&gt;Beaujolais&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href="#"&gt;Burgundy &lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href="#"&gt;Chianti &lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href="#"&gt;Madeira &lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href="#"&gt;Port &lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href="#"&gt;Sancerre &lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href="#"&gt;Tokaji &lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href="#"&gt;Vinho Verde &lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;

    &lt;/li&gt;
    &lt;li&gt;&lt;a href="#"&gt;Key Countries&lt;/a&gt;

    &lt;ul&gt;
      &lt;li&gt;&lt;a href="#"&gt;France&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href="#"&gt;Italy&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href="#"&gt;Spain&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href="#"&gt;United States&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href="#"&gt;Argentina&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href="#"&gt;Australia&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href="#"&gt;South Africa&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;

    &lt;/li&gt;
  &lt;/ul&gt;

&lt;/body&gt;
&lt;/html&gt;
</code></pre>
]]></content:encoded>
			<wfw:commentRss>http://www.darrenkrape.com/categories/design-and-development/css-drop-down-menus-2/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>CSS drop-down menus: how to keep the top level selected when hovering over a sub-menu</title>
		<link>http://www.darrenkrape.com/categories/design-and-development/drop-down-menus-persistent-top-level-menu-styling/</link>
		<comments>http://www.darrenkrape.com/categories/design-and-development/drop-down-menus-persistent-top-level-menu-styling/#comments</comments>
		<pubDate>Thu, 12 Apr 2007 16:31:32 +0000</pubDate>
		<dc:creator>Darren</dc:creator>
				<category><![CDATA[Design and Development]]></category>
		<category><![CDATA[Journal]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://www.forma3.com/v4/journal/drop-down-menus-persistent-top-level-menu-styling/</guid>
		<description><![CDATA[<p><img src="/i/articles/drop-down.png" alt="CSS drop-down menus: how to keep the top level selected when hovering over a sub-menu" class="article_highlight" />Pure <abbr title="Cascading Style Sheet">CSS</abbr>-based drop-down menus are a great thing, if for no other reason than their sheer simplicity and flexibility. However, they have two main drawbacks.</p>

<p>1. They don't work in Internet Explorer 6 due to the browser's poor support for the <code>:hover</code> pseudo-class.</p>
<p>2. When the mouse cursor is over a drop-down, the top level navigation item does not stay highlighted under most conditions.</p>

<p>Fortunately, both problems can be solved with some simple JavaScript. The first problem is easily corrected with the excellent <a href="http://www.htmldog.com/articles/suckerfish/dropdowns/" class="ext">Son of SuckerFish drop-down code</a>. The second problem can be solved using the equally small amount of code described below.</p>

<p>The best part of this code its use of <abbr title="Document Object Model">DOM</abbr> hooks in your <abbr title="Extensible HyperText Markup Language">XHTML</abbr> document to add the functionality making it self-contained and allowing full separation of content and design.</p>]]></description>
			<content:encoded><![CDATA[<p>Pure <abbr title="Cascading Style Sheet">CSS</abbr>-based drop-down menus are a great thing, if for no other reason than their sheer simplicity and flexibility. However, they have two main drawbacks.</p>
<ol>
<li>They don&#8217;t work in Internet Explorer 6 due to the browser&#8217;s poor support for the <code>:hover</code> pseudo-class.</li>
<li>When the mouse cursor is over a drop-down, the top level navigation item does not stay highlighted under most conditions.</li>
</ol>
<p>Fortunately, both problems can be solved with some simple JavaScript. The first problem is easily corrected with the excellent <a class="ext" href="http://www.htmldog.com/articles/suckerfish/dropdowns/">Son of SuckerFish drop-down code</a>. The second problem can be solved using the equally small amount of code described below.</p>
<p>The best part of this code its use of <abbr title="Document Object Model">DOM</abbr> hooks in your <abbr title="Extensible HyperText Markup Language">XHTML</abbr> document to add the functionality making it self-contained and allowing full separation of content and design.</p>
<p>For the sake of simplicity, and reducing the number of called functions, I&#8217;ve combined the SuckerFish JavaScript with my own.</p>
<ul>
<li><a class="code_example" href="/examples/drop_down_1.html">View this code in action</a></li>
<li><a class="jump" href="#complete_code">View complete code</a></li>
</ul>
<p><span id="more-45"></span></p>
<h3>How this works</h3>
<p>The code is pretty simple, but for novice JavaScriptors (such as myself), here is a set-by-step guide on how the function works.</p>
<h4>Internet Explorer 6 Functionality: The SuckerFish Code</h4>
<p>First, we declare the function and add the SuckerFish code for Internet Explorer 6 compatibility. Since this functionality is already described in their article, I&#8217;ll just note two small changes I&#8217;ve made.</p>
<ul>
<li>The original SuckerFish code had the ID hard-coded into the function. The below version has a variable instead of the hard-coded ID, allowing us to use the function on several different drop-downs.</li>
<li>I&#8217;ve corrected an error in the regular expression which was causing the class name to remain on the list element when the user moves their cursor off the drop-down (this error and the fix used here was <a class="ext" href="http://blogs.thesitedoctor.co.uk/tim/2007/02/23/Firefox+Class+Name+And+Space+Idiosyncrasy.aspx">originally pointed out by Tim Gaunt</a>).</li>
</ul>
<pre><code>menuHover = function(nav) {
  var sfEls = document.getElementById(nav).getElementsByTagName("li");
  for (var i=0; i&lt;sfEls.length; i++) {
    sfEls[i].onmouseover=function() {
      this.className+=" sfhover";
    }
    sfEls[i].onmouseout=function() {
      this.className=this.className.replace(new RegExp("\\s?sfhover\\b"), "");
    }
  }
</code></pre>
<h4>Code for persistent top-level menu formatting</h4>
<p>Now we get into the code that maintains the hover state for top-level navigation elements when the user has moved the cursor over a drop-down list. Fundamentally, this works the same way as the SuckerFish function, but with some adjustments. The system cycles through each drop-down <code>&lt;ul&gt;</code> and adds a function that tells the its parent anchor tag to take on a particular class when the browser detects a mouseover on the menu.</p>
<p>First, we create a variable selecting each sub-navigation list by using the main <code>&lt;ul&gt;</code> ID. This will allow us to add a mouseover function to each of the drop-downs.</p>
<pre><code>var listItem = document.getElementById(nav).getElementsByTagName('ul');</code></pre>
<p>Next, we cycle through each of the <code>&lt;ul&gt;</code> drop-downs so we can add a function on each one.</p>
<pre><code>for(var i=0;i&lt;listItem.length;i++) {</code></pre>
<p>If the browser detects the mouse cursor over the current drop-down it will pull into an array all anchors found in the parentNode (which is, basically, whatever the parent element is in the <abbr title="Document Object Model">DOM</abbr>, in this case the parent node is a <code>&lt;ul&gt;</code>). It then assigns the first anchor, presumably the anchor in the top-level navigation, a new class name. In this case, the class name &#8220;anchor&#8221;.</p>
<pre><code>listItem[i].onmouseover=function() {
  var changeStyle = this.parentNode.getElementsByTagName('a');
  changeStyle[0].className+=" active";
}
</code></pre>
<p>Now, when the user moves the cursor away, we simply remove the class name and the anchor is restored to its original style.</p>
<pre><code>listItem[i].onmouseout=function() {
  var changeStyle = this.parentNode.getElementsByTagName('a');
  changeStyle[0].className=this.className.replace(new RegExp("\\s?active\\b"), "");
}
</code></pre>
<h4>Playing well with others: onLoad Function</h4>
<p>Now we need to call our function on page load so the menus will work from the start. To do this we&#8217;ll use <a class="ext" href="http://ejohn.org/projects/flexible-javascript-events/">John Resig&#8217;s addEvent on load function</a>. This is useful for two reasons:</p>
<ol>
<li>Using this function will allow us to add as many on load events as we like (or what the browser will handle) without worrying that the various functions will interfere with each other.</li>
<li>John&#8217;s function helps avoid the Internet Explorer caching problem that can cause performance problems for end users.</li>
</ol>
<p>Here is the code:</p>
<pre><code>function addEvent( obj, type, fn ) {
  if ( obj.attachEvent ) {
    obj['e'+type+fn] = fn;
    obj[type+fn] = function(){obj['e'+type+fn]( window.event );}
    obj.attachEvent( 'on'+type, obj[type+fn] );
  } else
    obj.addEventListener( type, fn, false );
  }
function removeEvent( obj, type, fn ) {
  if ( obj.detachEvent ) {
  obj.detachEvent( 'on'+type, obj[type+fn] );
  obj[type+fn] = null;
  } else
    obj.removeEventListener( type, fn, false );
  }

addEvent(window, 'load', function () { menuHover('menu'); });
</code></pre>
<p>Take a look at <a class="ext" href="http://ejohn.org/projects/flexible-javascript-events/">John Resig&#8217;s post &#8220;Flexible Javascript Events&#8221;</a> on this function to get a better idea how it works.</p>
<h4>The <abbr title="Cascading Style Sheet">CSS</abbr> and <abbr title="Extensible HyperText Markup Language">XHTML</abbr></h4>
<p>Last but not least, we need our <abbr title="Cascading Style Sheet">CSS</abbr> and <abbr title="Extensible HyperText Markup Language">XHTML</abbr> on which we&#8217;ll apply our JavaScript. The following is almost exactly like the SuckerFish code so I will note the one, very small, different.</p>
<p>In order to apply the highlighted state to the top-level navigation anchor, we need to give it a class name. Fortunately, since we already have a style declaration for the hover state, we&#8217;ll just append another declaration name to the original style, as you can see below.</p>
<pre><code>#menu li.dropdown a:hover, #menu li.dropdown a.active {
  color: white;
  background: #999 url(../i/arrow_down.gif) no-repeat right 50%;
}
</code></pre>
<h3>Areas for improvement</h3>
<ol>
<li>This will only work if your top-level navigation style is on an anchor tag. If your main style is on the &lt;li&gt; then the JavaScript won&#8217;t select it in order to add the custom class. A future improvement might include passing a variable with the tag or class the function should reference.</li>
<li>The class name JavaScript references is hard coded into the function. Fortunately the script will pull the class relative to the element in the <abbr title="Document Object Model">DOM</abbr>, so you can have two classes with the same name with different styles so long as they are declared on different elements higher in the cascade.To illustrate, if using the following code:
<pre><code>&lt;ul id="menu"&gt;
  &lt;li&gt;
    &lt;a href="#" class="active"&gt;Link&lt;/a&gt;
  &lt;/li&gt;
&lt;/ul&gt;</code></pre>
<p>This function will apply the style <code>#menu ul li .active</code> and not the style <code>#content ul li .active</code>.</p>
<p>Nonetheless, passing a variable with the highlighted class name to the function would help make this function truly flexible.</li>
</ol>
<h3 id="complete_code">Complete Code</h3>
<pre class="complete"><code>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;

&lt;head&gt;

  &lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /&gt;
  &lt;title&gt;Menu Example&lt;/title&gt;

  &lt;style type="text/css" media="screen"&gt;

  body {
    margin: 20px;
    background: #fff;
    font-family: Verdana, Helvetica, sans-serif;
    font-size: 9pt;
  }

  * { margin: 0; padding: 0; }
  li { list-style: none; }
  a { text-decoration: none; }

  #menu li {
    float: left;
    display: block;
    font-size: 8pt;
  }

  #menu li a {
    position: relative;
    padding: 6px 10px;
    display: block;
    font-weight: bold;
    color: #333;
  }

  #menu li a:hover, #menu li a.active {
    background: #999;
    color: white;
  }

  #menu li ul {
    left : -999em;
    position : absolute;
  }

  #menu li ul li { float : none; }

  #menu li ul a {
    width : 136px;
    padding : 8px 10px;
    background : #f4f4f4;
    border-bottom : 1px solid white;
    font-weight : normal;
  } 

  #menu li ul a:hover {
    background: #FFCC99;
    color: #333;
  }

  #menu li:hover ul, #menu li.sfhover ul {
    left: auto;
    border-top: 1px solid #999;
  }

  &lt;/style&gt;

  &lt;script type="text/javascript"&gt; &lt;!--
  menuHover = function(nav) {
    var sfEls = document.getElementById(nav).getElementsByTagName("li");
    for (var i=0; i&lt;sfEls.length; i++) {
      sfEls[i].onmouseover=function() {
        this.className+=" sfhover";
      }
      sfEls[i].onmouseout=function() {
        this.className=this.className.replace(new RegExp("\\s?sfhover\\b"), "");
      }
    }

    var listItem = document.getElementById(nav).getElementsByTagName('ul');
    for(var i=0;i&lt;listItem.length;i++) {
      listItem[i].onmouseover=function() {
        var changeStyle = this.parentNode.getElementsByTagName('a');
        changeStyle[0].className+=" active";
      }

      listItem[i].onmouseout=function() {
        var changeStyle = this.parentNode.getElementsByTagName('a');
        changeStyle[0].className=this.className.replace(new RegExp("\\s?active\\b"), "");
      }
    }
  }

  function addEvent( obj, type, fn ) {
    if ( obj.attachEvent ) {
      obj['e'+type+fn] = fn;
      obj[type+fn] = function(){obj['e'+type+fn]( window.event );}
      obj.attachEvent( 'on'+type, obj[type+fn] );
    } else
      obj.addEventListener( type, fn, false );
    }
  function removeEvent( obj, type, fn ) {
    if ( obj.detachEvent ) {
    obj.detachEvent( 'on'+type, obj[type+fn] );
    obj[type+fn] = null;
    } else
      obj.removeEventListener( type, fn, false );
    }

  addEvent(window, 'load', function () { menuHover('menu'); });

  // --&gt; &lt;/script&gt;

&lt;/head&gt;

&lt;body&gt;

  &lt;ul id="menu"&gt;
    &lt;li&gt;&lt;a href="#"&gt;Styles&lt;/a&gt;

    &lt;ul&gt;
      &lt;li&gt;&lt;a href="#"&gt;Red/White&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href="#"&gt;Ros?©/Blush&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href="#"&gt;Sparkling&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href="#"&gt;Dessert&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href="#"&gt;Fortified&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href="#"&gt;Fruit&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href="#"&gt;Ice Wine&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;

    &lt;/li&gt;
    &lt;li&gt;&lt;a href="#"&gt;Whites&lt;/a&gt;

    &lt;ul&gt;
      &lt;li&gt;&lt;a href="#"&gt;Albari?±o&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href="#"&gt;Chardonnay&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href="#"&gt;Chenin blanc&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href="#"&gt;Muscat&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href="#"&gt;Pinot blanc&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href="#"&gt;Pinot gris&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href="#"&gt;Riesling&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href="#"&gt;Sauvignon blanc &lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href="#"&gt;S?©millon&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;

    &lt;/li&gt;
    &lt;li&gt;&lt;a href="#"&gt;Reds&lt;/a&gt;

    &lt;ul&gt;
      &lt;li&gt;&lt;a href="#"&gt;Cabernet Sauvignon &lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href="#"&gt;Malbec &lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href="#"&gt;Merlot &lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href="#"&gt;Pinot noir &lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href="#"&gt;Syrah/Shiraz &lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href="#"&gt;Zinfandel&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;

    &lt;/li&gt;
    &lt;li&gt;&lt;a href="#"&gt;Noted Regionals&lt;/a&gt;

    &lt;ul&gt;
      &lt;li&gt;&lt;a href="#"&gt;Amarone&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href="#"&gt;Beaujolais&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href="#"&gt;Burgundy &lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href="#"&gt;Chianti &lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href="#"&gt;Madeira &lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href="#"&gt;Port &lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href="#"&gt;Sancerre &lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href="#"&gt;Tokaji &lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href="#"&gt;Vinho Verde &lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;

    &lt;/li&gt;
    &lt;li&gt;&lt;a href="#"&gt;Key Countries&lt;/a&gt;

    &lt;ul&gt;
      &lt;li&gt;&lt;a href="#"&gt;France&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href="#"&gt;Italy&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href="#"&gt;Spain&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href="#"&gt;United States&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href="#"&gt;Argentina&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href="#"&gt;Australia&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href="#"&gt;South Africa&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;

    &lt;/li&gt;
  &lt;/ul&gt;

&lt;/body&gt;
&lt;/html&gt;
</code></pre>
<div class="update">
<p><strong>Update: 6 September 2009</strong></p>
<p>I recently got an email from a reader noting how IE6 caused a flicker when mousing over the top level navigation and inquiring if there was a way to fix this. Fortunately, the fix is quite easy. Simply add &#8220;<code>position: relative</code>&#8221; to the CSS declaration &#8220;<code>#menu li a</code>&#8220;.</p>
<p>Basically, what is happening is IE6 hovers don&#8217;t work very well when hovering over padding on an element. So the hover state disappears when moving from the actual text to the padding area and then to the menu below. The &#8220;position: relative&#8221; trick is useful for solving this in many other situations.</p>
<p>I&#8217;ve updated the code above to reflect this change.</p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.darrenkrape.com/categories/design-and-development/drop-down-menus-persistent-top-level-menu-styling/feed/</wfw:commentRss>
		<slash:comments>21</slash:comments>
		</item>
	</channel>
</rss>
