Home >Web Front-end >HTML Tutorial >CSS: Collapse menu under responsive style (striped style)_html/css_WEB-ITnose
Original text: CSS: Responsive Navigation Menu
Translation: CSS: Responsive Navigation Menu
Translator: dwqs
Written before, about how to make a responsive drop-down menu: Responsive drop-down menu
Previously, I wrote a tutorial on how to make responsive mobile navigation. Now, I've discovered a new trick to implement responsive menus without using JavaScript, but using HTML 5's clear semantic markup.
This menu can be aligned left, center and right, unlike the menu mentioned in the previous article that requires clicking to switch between showing and hiding. It also has a marker showing the active/current menu item, and it works on all mobile platforms and all desktop browsers including IE.
First, take a look at the effect of the Demo: Demo
1. Purpose
The purpose of this tutorial is to show how to turn a regular list menu into A smaller display dropdown menu.
This trick is very useful on navigations composed of many links, such as the screenshot below, to condense all the buttons into an elegant drop-down.
2. Nav HTML tags
<nav class="nav"> <ul> <li class="current"><a href="#">Portfolio</a></li> <li><a href="#">Illustration</a></li> <li><a href="#">Web Design</a></li> <li><a href="#">Print Media</a></li> <li><a href="#">Graphic Design</a></li> </ul></nav>
3. CSS
/* nav */.nav { position: relative; margin: 20px 0;}.nav ul { margin: 0; padding: 0;}.nav li { margin: 0 5px 10px 0; padding: 0; list-style: none; display: inline-block;}.nav a { padding: 3px 12px; text-decoration: none; color: #999; line-height: 100%;}.nav a:hover { color: #000;}.nav .current a { background: #999; color: #fff; border-radius: 5px;}
4. Center and right alignment
As mentioned earlier, it can be changed through the text-align attribute Alignment.
/* right nav */.nav.right ul { text-align: right;}/* center nav */.nav.center ul { text-align: center;}
5. IE support
In IE 8 and earlier versions, HTML5 nav tags and media are not supported. Query. Introduce css3-mediaqueries.js (or respond.js) and html5shim.js to provide fallback compatibility processing. If you don’t want to use html5shim.js, you can use div instead of nav
<!--[if lt IE 9]> <script src="http://css3-mediaqueries-js.googlecode.com/files/css3-mediaqueries.js"></script> <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script><![endif]?>
6. Responsive
Now comes the interesting part ??Responsiveness of media query menu. If you are not familiar with responsive design, you can read my previous articles about responsive design (subsequent translation) and media query.
At the 600 px breakpoint, I set the nav element to relative positioning, so that I can absolutely position the ul element. With display:none, I hide all li elements but keep the current li element displayed as a block element. Then, when the mouse is hovering over the nav element, add display:block to all li elements. To mark the current li element, I will add an icon. For centering and right alignment, use the left and right attributes on the positioned ul element. Take a look at the final demo effect:
@media screen and (max-width: 600px) { .nav { position: relative; min-height: 40px; } .nav ul { width: 180px; padding: 5px 0; position: absolute; top: 0; left: 0; border: solid 1px #aaa; background: #fff url(images/icon-menu.png) no-repeat 10px 11px; border-radius: 5px; box-shadow: 0 1px 2px rgba(0,0,0,.3); } .nav li { display: none; /* hide all <li> items */ margin: 0; } .nav .current { display: block; /* show only current <li> item */ } .nav a { display: block; padding: 5px 5px 5px 32px; text-align: left; } .nav .current a { background: none; color: #666; } /* on nav hover */ .nav ul:hover { background-image: none; } .nav ul:hover li { display: block; margin: 0 0 5px; } .nav ul:hover .current { background: url(images/icon-check.png) no-repeat 10px 7px; } /* right nav */ .nav.right ul { left: auto; right: 0; } /* center nav */ .nav.center ul { left: 50%; margin-left: -90px; } }
Recommended related blog posts:
Demo: http://red-team- design.com/wp-content/uploads/2013/02/animenu-demo.html
Original post: