Smoothly open menu using CSS. Vertical drop-down menu on CSS: how to make it
From the author: I welcome everyone. For some reason, the drop-down menu of webmasters is associated with scripts, but for a long time to make such navigation you can absolutely calmly on pure CSS. Moreover, such a menu will be no worse. Today I will show you how to create a drop-down menu in CSS. I will share it, so to speak, recipe.
Lesson Plan and Marking Our Menu
In general, first we will define how we create the menu itself. In HTML5, the standard way is considered to create it in the NAV container using marked list. Let's do this. Markers, we will later remove with CSS, in the menu they do not need us completely.
Actually, I immediately write in HTML marking with nested lists. That is, our list will be two-level - it will have other lists. Namely, for each item 1 list, which will form a drop-down menu.
There is here important momentwhich is that you may not need every item to make falling, but only some. Without problems, then in points without loss simply do not create invested lists.
Actually, here it is all marking:
< nav id = "nav" > < ul > < li > < a href = "#" > Paragraph 1< / a > < ul class = "second" > < li > < a href = "#" > Subparagraph< / a > < / li > < li > < a href = "#" > Subparagraph< / a > < / li > < li > < a href = "#" > Subparagraph< / a > < / li > < / ul > < / li > < li > < a href = "#" > Point 2< / a > < ul class = "second" > < li > < a href = "#" > Subparagraph< / a > < / li > < li > < a href = "#" > Subparagraph< / a > < / li > < li > < a href = "#" > Subparagraph< / a > < / li > < / ul > < / li > < li > < a href = "#" > Point 3< / a > < ul class = "second" > < li > < a href = "#" > Subparagraph< / a > < / li > < li > < a href = "#" > Subparagraph< / a > < / li > < li > < a href = "#" > Subparagraph< / a > < / li > < / ul > < / li > < li > < a href = "#" > Paragraph< / a > < ul class = "second" > < li > < a href = "#" > Subparagraph< / a > < / li > < li > < a href = "#" > Subparagraph< / a > < / li > < li > < a href = "#" > Subparagraph< / a > < / li > < / ul > < / li > < / ul > < / nav > |
I understand that she is huge. This is the problem of nested lists, which is required to write a lot of code. But consider the fact that we make 4 main items and for each will be a drop-down menu.
Actually, I set all navigation NAV identifier, and all invested lists are the SECOND style class to understand that they are invested.
Excellent, we are ready for the markup, you can look at the result:
Yes, it looks terrible. But now we will be included in the work of CSS and within a few minutes our navigation will be transformed. Let's work.
We write css-styles
The first thing I will do is freeze all default indents for all items. They will only interfere with us, better set the indents themselves, where it will be needed.
* (Margin: 0; Padding: 0;)
margin: 0; padding: 0; |
#NAV (Height: 70px;) #Nav UL (List-Style: None;)
#NAV ( height: 70px; #Nav UL ( list - Style: None; |
Now we need, in fact, to decide what our menu will be. Horizontal or vertical? I suggest first to make horizontal and see everything on his example. To do this, write such styles:
#NAV\u003e UL\u003e Li (Float: Left; width: 180px; position: relative;)
#NAV\u003e UL\u003e Li ( float: Left; width: 180px; position: relative; |
Note that with the help of the sign\u003e we appeal to the items of the list, which are directly invested in UL, and that is directly invested in NAV. This is very important, as it allows not to apply the style to the points of the attached lists. The fact is that by my idea in the main list items will be located horizontally, and in the nested - vertically.
But you, of course, can do differently, in harmony with your idea. You can make all the items in one line.
In any case, the above code, I hope you understand. He causes the items of the main list to pressed to the left edge, so that they all get together in one line, although they are block elements. I also asked them an obvious width and relative positioning.
What is positioning? It is necessary in order to then position the invested lists. If you studied positioning in CSS, you probably know that if you set the relative positioning unit, then all the items in it can be positioned absolutely inside this particular block, and not the entire browser window.
Meanwhile, that's what we already got:
By this screenshot you can already imagine an exemplary result. Of course, we still have to be beautiful to place items so that the eyes do not beg for mercy when viewing navigation.
#Nav Li A (Display: Block; Background: # 90DA93; Border: 1px Solid # 060a13; Color: # 060a13; padding: 8px; text-align: center; text-decoration: none;) #Nav Li A: Hover ( Background: # 2F718E;)
#NAV Li A ( display: Block; background: # 90DA93; border: 1px Solid # 060a13; color: # 060a13; padding: 8px; tEXT - Align: Center; text - Decoration: None; #NAV Li A: Hover ( background: # 2F718E; |
First, the links themselves need to be made block. This is necessary in order to work internal indents and all properties were used correctly. Next, I specify the colors of the background, text, frame parameters, internal indents and text alignment in the center. Finally, I cancel underscores.
All these parameters do not necessarily prescribe the way I did. Colors You can choose an arbitrarily, frame to make another or not to use, indents can be reduced or increased.
Note that in this case we did not use the sign\u003e, so the specified rules will apply to all links, including those that are in nested items. So, now we have the following:
Excellent, but you understand that subparagraphs should not be seen, they should be revealed when you hover on the desired item. Without this, our menu is similar to the table. Well, it's time to hide the nested items.
#Nav Li .second (Display: None; Position: absolute; top: 100%;)
#Nav Li .second ( display: None; position: Absolute; top: 100%; |
First, hide fully attached lists. Secondly, we ask them absolute positioning and coordinate Top: 100%. This means that the drop-down menu will be displayed clearly under the main item to which it belongs is clearly under 100% of the height of this item.
Now we see on the webpage only the main menu that we need.
We implement loss
We have left to do the most important thing - to realize the very loss. There is nothing complicated in this, look at this code:
#Nav Li: Hover .second (Display: Block;)
#NAV Li: Hover .second ( display: Block; |
This code will work immaculately. When you hover on the master menu, the attached list will become visible. And only the list that is invested in the item on which the cursor is guided.
There is only one minor problem - the width of the nested items does not correspond to the width of the main. But it is very easily solved by adding the corresponding styles:
#Nav Li Li (width: 180px;)
#NAV Li Li ( width: 180px; |
All, the problem is solved:
Everything works perfectly. When you hover on the main item appears the corresponding drop-down menu. If you remove the cursor to the side - it disappears. If you translate the cursor to the attached items themselves, you can click on them and go to the desired section of the site. Thus, we made with you a very simple and easy drop-down menu absolutely without scripts.
Remove the menu to vertical
Well, well, with horizontal navigation, we fully figured out, but in addition to her sites, it is very often encountered vertical and it can also be dropping. In fact, remake the menu from the horizontal to the vertical is very easy, we will have to change the literally a couple of lines of code.
First, you will need to remove Float: Left at the main points list. This property contributes to the fact that our items are displayed in one line, but why do we need it if navigation should be vertical?
Now it remains to change the rules for the #Nav Li .second selector, that is, for invested lists, namely, they need to position a little differently. Like this:
#Nav Li .second (Display: None; Position: absolute; left: 100%; top: 0;)
#Nav Li .second ( display: None; position: Absolute; left: 100%; top: 0; |
That is, it is necessary to register two coordinates instead of one. First, in the case of horizontal navigation, I said that, by the idea, the items of the menu should be displayed under the main points. In the case of the vertical menu, it is not suitable - items must be displayed on the side.
Therefore, we change the TOP: 100% on Left: 100%. In addition, the coordinate Top is also needed. We ask it 0 so that the attached menu is on the same level with the item to which correspond.
Everything, now everything works as it should. You can test. As you can see, I did not lie when I said that you have to rewrite only a few lines of code.
Horizontal menu with multiple levels
Using about the same approach, you can create more menu levels if it is strongly necessary. For example, let's create for the fourth item of the main menu list that will be invested in one of the sub-items.
Mikhail Sat, 09/20/2014 - 12:28
Something my comment is not visible - duplicate:
===========
Thank you, but I do not fall out the menu (Under int.expl-8). Those. String does not work "UL. nAV.li: Hover UL (Display: Block;) "
Tell me what's the matter?
There are quenching of the material:
It would be nice in this line (UL.aaaaa Li: Hover UL (Display: Block;)) to express what part of it is responsible for "Layout Menu", and what - for hiding items of a deeper nesting level
It would be nice in the examples each line supplied the comment that it does.
In particular, it is unclear why you need a string "