How To Create a 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>
<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; }
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; }
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; }
<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; }
nav ul ul ul { position: absolute; left: 100%; top:0; }
<li>
.Download Tutorial.
0 comments:
Post a Comment