Developers Stuffs . Lot of Programming tutorials on different lanuages like PHP, JAVA, webdesign, jquery

How To Create a CSS Dropdown Menu

How To Create a CSS Dropdown Menu

CSS Dropdown Menu

Each page have a navigation area, which links to all the other pages within that section of the website. This is great, but in order to go to, say, the FAQs page from the home page, you'd first have to go to the about page, then on to the FAQs from there. This is by not acceptable.With the help of some advanced selectors a dropdown menu can be easily created with CSS. Follow the step by step process of creating your own CSS Dropdown Menu.

The menu we’ll be creating have two sub categories that appear once the parent link is activated by a hover. The first series of sub-links appear underneath main nav bar, then the second series of links fly out horizontally from the first dropdown.

Download Drop Down Menu

The HTML Structure

<nav>
 <ul>
  <li><a href="#">Home</a></li>
  <li><a href="#">Tutorials</a></li>
  <li><a href="#">Blogs</a></li>
  <li><a href="#">Social</a></li>
 </ul>
</nav>
Firstly we need to create a HTML structure for our CSS menu. We’ll use HTML5 to make the navigation menu in a <nav> element, then add the primary navigation links in a simple unordered list.
<nav>
 <ul>
  <li><a href="#">Home</a></li>
  <li><a href="#">Tutorials</a>
   <ul>
    <li><a href="#">HTML</a></li><li><a href="#">CSS</a></li>
    <li><a href="#">JQuery</a>
<ul>
      <li><a href="#">HTML</a></li>
      <li><a href="#">CSS</a></li>
     </ul>
    </li>
   </ul>
  </li>
  <li><a href="#">Blog</a>
   <ul>
    <li><a href="#">rtechiecode</a></li><li><a href="#">devstuffs</a></li>
</ul>
  </li>
  <li><a href="#">Social</a></li>
 </ul>
</nav>

We will add the first sets of sub-menu under the “Tutorials” and “Blog” links, each one being a complete unordered list inserted within the <li> of its parent menu option.
The secondary sub-menu is nested under the “JQuery” option of the first sub-menu. These links are placed into another unordered list and inserted into the “JQuery” <li>.

The CSS Styling

nav ul ul 
{
 display: none;
}

 nav ul li:hover > ul 
 {
  display: block;
 }

Let’s begin the CSS by getting the basic dropdown functionality working. First hide the sub menus by targeting any UL’s within a UL with the display:none; declaration. In order to make these menus reappear they need to be converted back to block elements on hover of the LI. The > child selector makes sure only the child UL of the LI being hovered is targeted, rather than all sub menus appearing at once.

nav ul 
{
 background:#333; 
 padding: 0 20px;
 border-radius: 10px;  
 list-style: none;
 position: relative;
 display: inline-table;
}

 nav ul:after 
{
  content: "";
   clear: both; 
  display: block;
 }

Adding position:relative; will allow us to absolutely position the sub menus according to this main nav bar, then display:inline-table will condense the width of the menu to fit. The clearfix style rule will clear the floats used on the subsequent list items without the use of overflow:hidden, which would hide the sub menus and prevent them from appearing.

nav ul li 
{
 float: left;
}
 
nav ul li a 
{
  display: block; 
  padding: 25px 40px;
  color:#fff; 
  text-decoration: none;
 }
nav ul li:hover 
 {
  background: #4b545f;
 }
  nav ul li:hover a 
 {
   color:#f5f5f5;
  }
 
 

The individual menu items are then styled up with CSS rules added to the <li> and the nested anchor. In the browser this will make the link change to a blue gradient background and white text.

nav ul ul 
 {
 padding: 0;
 position: absolute;
 background:#666; 
 border-radius: 0px; 
 top: 100%;
 }
 nav ul ul li 
  {
  float: none; 
  position: relative;
border-top: 1px solid #6b727c;
  border-bottom: 1px solid #575f6a;
  }
  nav ul ul li a 
  {
   color: #fff;
   padding: 15px 40px;
  } 
   nav ul ul li a:hover 
   {
    background:#999;
   }

The main navigation bar is now all styled up, but the sub menus still need some work. To make sure they fly out under the main menu they are positioned absolutely 100% from the top of the UL (ie, the bottom). The LI’s of each UL in the sub menu don’t need floating side by side, instead they’re listed vertically with thin borders separating each one. A quick hover effect then darkens the background.

nav ul ul ul {
 position: absolute; 
left: 100%; 
top:0;
}

The final step is to position the sub-sub-menus accordingly. These menus will be inheriting all the sub-menu styling already, so all they need is to be positioned absolutely to the right (left:100%) of the relative position of the parent <li>.

Download Tutorial.

0 comments:

Post a Comment

ABOUT US

Dev Stuffs is an IT focussed webpage. We strive hard to bring useful content for the budding developers and future programmers. You will find short Programming tutorials and some useful tools for developers.
Happy Programming. :)