Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I am adding the final answer here, for the sake of ensuring that every one sees the answer I chose (i.e, for the greater good).</p> <p>This answer is inspired by GraphicO, with modifications:</p> <p>The HTML:</p> <pre><code>&lt;nav&gt; &lt;ul class="main-menu" &gt; &lt;li id="item1"&gt; &lt;a href="#"&gt;item 1&lt;/a&gt; &lt;div&gt; &lt;ul class="sub-menu"&gt; &lt;li id="item11"&gt; &lt;a href="#"&gt;item 1.1&lt;/a&gt; &lt;/li&gt; &lt;li id="item12"&gt; &lt;a href="#"&gt;item 1.2&lt;/a&gt; &lt;/li&gt; &lt;li id="item13"&gt; &lt;a href="#"&gt;item 1.3&lt;/a&gt; &lt;/li&gt; &lt;/ul&gt; &lt;/div&gt; &lt;/li&gt; &lt;li id="item2"&gt; &lt;a href="#"&gt;item 2&lt;/a&gt; &lt;div&gt; &lt;ul class="sub-menu"&gt; &lt;li id="item21"&gt; &lt;a href="#"&gt;item 2.1&lt;/a&gt; &lt;/li&gt; &lt;li id="item22"&gt; &lt;a href="#"&gt;item 2.2&lt;/a&gt; &lt;/li&gt; &lt;/ul&gt; &lt;/div&gt; &lt;/li&gt; &lt;li id="item3"&gt; &lt;a href="#"&gt;item 3&lt;/a&gt; &lt;/li&gt; &lt;/ul&gt; &lt;/nav&gt; </code></pre> <p>Then, the CSS:</p> <pre><code>a { color: black; text-decoration: none; } nav { position: relative; width: 100%; background-color: red; } .main-menu { margin: 0 auto; width: 1024px; list-style: none; } .main-menu &gt; li { display: inline-block; margin-right: 2em; } .main-menu &gt; li.selected { background-color: yellow; } .main-menu &gt; li &gt; div { /* nested div (containing the sub nav) will be hidden by default */ width: 100%; position: absolute; left: 0; background-color: yellow; display:none; } .main-menu &gt; li.selected &gt; div { display: inline; } .sub-menu { list-style: none; margin: 0 auto; width: 1024px; } .sub-menu &gt; li { display: inline-block; margin-right: 2em; } </code></pre> <p>And finally the jQuery:</p> <pre><code>// Make the first class selected $(".main-menu &gt; li:first").addClass("selected"); // Switch the selected class $(".main-menu &gt; li").click(function() { $(".selected").removeClass("selected"); $(this).addClass("selected"); }); // Disable menu links $(".main-menu &gt; li &gt; a").click(function(e) { e.preventDefault(); }); </code></pre> <p>Thanks.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload