<?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>Learning jQuery &#187; Horizontal Subnav</title>
	<atom:link href="http://www.learningjquery.org/index.php/tag/horizontal-subnav/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.learningjquery.org</link>
	<description>Learning jQuery: Tips, techniques, and tutorials for the jQuery JavaScript library</description>
	<lastBuildDate>Tue, 24 Aug 2010 10:10:35 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Horizontal Subnav with CSS</title>
		<link>http://www.learningjquery.org/index.php/horizontal-subnav-with-css/</link>
		<comments>http://www.learningjquery.org/index.php/horizontal-subnav-with-css/#comments</comments>
		<pubDate>Thu, 16 Jul 2009 09:13:08 +0000</pubDate>
		<dc:creator>Ben</dc:creator>
				<category><![CDATA[jQuery]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[Horizontal Subnav]]></category>

		<guid isPermaLink="false">http://www.learningjquery.org/?p=55</guid>
		<description><![CDATA[Not too long ago I wrote a tutorial on how to create a drop down menu with CSS &#38;
jQuery, today I would like to go over how to create a simple navigation with a horizontal subnav.
In most cases we can achieve this effect purely with CSS, but since we have to attend to our red [...]]]></description>
			<content:encoded><![CDATA[<p>Not too long ago I wrote a tutorial on how to create a drop down menu with CSS &amp;<br />
jQuery, today I would like to go over how to create a simple navigation with a horizontal subnav.</p>
<p>In most cases we can achieve this effect purely with CSS, but since we have to attend to our red headed step child aka IE6, we will use a few lines of jQuery to cover all grounds.</p>
<p><a href="http://www.sohtanaka.com/web-design/examples/horizontal-subnav/" target="_blank"><img class="center" src="http://www.sohtanaka.com/web-design/examples/horizontal-subnav/preview.gif" alt="Horizontal Subnavigation - CSS jQuery Tutorial" /></a></p>
<div class="highlight">
<p><a class="view" href="http://www.sohtanaka.com/web-design/examples/horizontal-subnav/" target="_blank">View Demo</a></div>
<h4>Wireframe &#8211; HTML</h4>
<p>Nest a set of links wrapped within the &lt;span&gt; tag, this is how the sub navigation will be positioned.</p>
<pre>&lt;ul id="topnav"&gt;
    &lt;li&gt;&lt;a href="#"&gt;Link&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;
        &lt;a href="#"&gt;Link&lt;/a&gt;
        <span style="color: #999999;">&lt;!--Subnav Starts Here--&gt;</span>
        &lt;span&gt;
            &lt;a href="#"&gt;Subnav Link&lt;/a&gt; |
            &lt;a href="#"&gt;Subnav Link&lt;/a&gt; |
            &lt;a href="#"&gt;Subnav Link&lt;/a&gt;
        &lt;/span&gt;
       <span style="color: #999999;"> &lt;!--Subnav Ends Here--&gt;</span>
    &lt;/li&gt;
    &lt;li&gt;&lt;a href="#"&gt;Link&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</pre>
<h4>Styling &#8211; CSS</h4>
<p><img class="center" src="http://www.sohtanaka.com/web-design/examples/horizontal-subnav/wireframe.gif" alt="Horizontal Subnavigation - CSS jQuery Tutorial" /><br />
Unlike a regular drop down menu where the subnav appears directly underneath the hovered/clicked list item, in this case all the subnav sets will start at the same location (left aligned &#8211; underneath the navigation).</p>
<pre>ul#topnav {
	margin: 0; padding: 0;
	float: left;
	width: 970px;
	list-style: none;
	position: relative; <span style="color: #999999;">/*--Set relative positioning on the unordered list itself - not on the list item--*/</span>
	font-size: 1.2em;
	background: url(topnav_stretch.gif) repeat-x;
}
ul#topnav li {
	float: left;
	margin: 0; padding: 0;
	border-right: 1px solid #555; <span style="color: #999999;">/*--Divider for each parent level links--*/</span>
}
ul#topnav li a {
	padding: 10px 15px;
	display: block;
	color: #f0f0f0;
	text-decoration: none;
}
ul#topnav li:hover { background: #1376c9 url(topnav_active.gif) repeat-x; }
<span style="color: #999999;">/*--Notice the hover color is on the list item itself, not on the link. This is so it can stay highlighted even when hovering over the subnav--*/</span></pre>
<p>Now set the absolute positioning on the &lt;span&gt; tag 35px from the top. I added some rounded corners on the bottom for style (this will not work in IE).</p>
<pre>ul#topnav li span {
	float: left;
	padding: 15px 0;
	position: absolute;
	left: 0; top:35px;
	display: none; <span style="color: #999999;">/*--Hide by default--*/</span>
	width: 970px;
	background: #1376c9;
	color: #fff;
	<span style="color: #999999;">/*--Bottom right rounded corner--*/</span>
	-moz-border-radius-bottomright: 5px;
	-khtml-border-radius-bottomright: 5px;
	-webkit-border-bottom-right-radius: 5px;
	<span style="color: #999999;">/*--Bottom left rounded corner--*/</span>
	-moz-border-radius-bottomleft: 5px;
	-khtml-border-radius-bottomleft: 5px;
	-webkit-border-bottom-left-radius: 5px;
}
ul#topnav li:hover span { display: block; } <span style="color: #999999;">/*--Show subnav on hover--*/</span>
ul#topnav li span a { display: inline; } <span style="color: #999999;">/*--Since we declared a link style on the parent list link, we will correct it back to its original state--*/</span>
ul#topnav li span a:hover {text-decoration: underline;}</pre>
<p>For those who are not very familiar with how positioning works, check out the following tutorials:</p>
<ul>
<li><a href="http://www.w3schools.com/Css/css_positioning.asp" target="_blank">w3schools &#8211; CSS Positioning</a></li>
<li><a href="http://css-tricks.com/absolute-relative-fixed-positioining-how-do-they-differ/" target="_blank">Absolute, Relative, Fixed Positioning: How Do They Differ? </a></li>
<li><a href="http://thecssblog.com/tutorials/stopping-the-css-positioning-panic-part-1/" target="_blank">Stopping the CSS positioning panic</a></li>
</ul>
<h4>IE6 Fix &#8211; jQuery</h4>
<p>Since IE6 does not understand <strong>li:hover</strong> (basically it only understands a hover event on a &lt;a&gt; tag), use jQuery to go around the issue.</p>
<p><small>For those who are not familiar with <a href="http://www.jquery.com/" target="_blank">jQuery</a>, do check out their site first and get an overview of how it works. I’ve shared a <a href="http://www.sohtanaka.com/web-design/tag/jquery/" target="_blank">few tricks</a> that I have picked up along the way, you can check those out as well.</small></p>
<p><strong>Initial Step &#8211; Call the jQuery file</strong><br />
You can choose to <a href="http://code.google.com/p/jqueryjs/downloads/detail?name=jquery-1.3.2.min.js&amp;downloadBtn=" target="_blank">download</a> the file from the jQuery site, or you can use this one hosted on Google.</p>
<pre>&lt;script type="text/javascript"
src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"&gt;&lt;/script&gt;</pre>
<p>The following script contains comments explaining which jQuery actions are being performed.</p>
<pre><span style="color: #999999;">&lt;script type="text/javascript"&gt;
$(document).ready(function() {</span>

	$("ul#topnav li").hover(function() { <span style="color: #999999;">//Hover over event on list item</span>
		$(this).css({ 'background' : '#1376c9 url(topnav_active.gif) repeat-x'}); <span style="color: #999999;">//Add background color and image on hovered list item</span>
		$(this).find("span").show(); <span style="color: #999999;">//Show the subnav</span>
	} , function() { <span style="color: #999999;">//on hover out...</span>
		$(this).css({ 'background' : 'none'}); <span style="color: #999999;">//Ditch the background</span>
		$(this).find("span").hide(); <span style="color: #999999;">//Hide the subnav</span>
	});

<span style="color: #999999;">});
&lt;/script&gt;</span></pre>
<p><a href="http://www.sohtanaka.com/web-design/examples/horizontal-subnav/" target="_blank"><img class="center" src="http://www.sohtanaka.com/web-design/examples/horizontal-subnav/preview.gif" alt="Horizontal Subnavigation - CSS jQuery Tutorial" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.learningjquery.org/index.php/horizontal-subnav-with-css/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
