Horizontal submenu html. Simple cross-browser horizontal menu

It’s rare to see horizontal menus on a website that drop down when you hover the mouse, but they are very beautiful and functional. Or as they say - usable. Today we will create a usable horizontal drop-down menu using pure CSS code. I have often noticed that people use javascript to achieve this effect. In fact, everything can be done only with CSS. Let's look at two options for drop-down horizontal menus using CSS. It's good to have a choice, I think you agree.

If you need a simple horizontal menu. Reading the article about creation. Once you've mastered the basics, start creating a drop-down menu.

Option 1 - Drop-down horizontal menu

For clarity, see the screenshot below. You are probably familiar with this horizontal menu structure.

For even greater clarity, let's look at the HTML structure of the menu. Let’s make sure what exactly achieved the horizontal position of the menu and its validity. Menu css structure:

Showing and hiding menu children is done using CSS: ul > li:hover ul. That is quite simple. This switch allows you to define the behavior of all child menus. When creating a regular horizontal menu, in addition to display:block we add other styles, such as background color, etc. The only difference between a regular menu and a dropdown menu is that we don't style the nested ULs. You need to place it on the dependent LI element, which is hovered (UL > LI: hover).

Let's look at the CSS code:

#header ( height:120px; position:relative; background: transparent url(header_bkg.png) repeat-x scroll top center;) #nav ( margin:0px; padding:0px; position:absolute; top: 70px; display:block ;) #nav > li ( list-style-type:none; float:left; display:block; margin:0px 10px; position:relative; padding:10px; width:100px;) #nav > li:hover ul ( display :block; ) #nav > li:hover ( background-color:#808080; -moz-border-radius:10px; -webkit-border-radius:10px; ) #nav li ul ( margin:0px; padding:0px; display:none;) #nav li ul li ( list-style-type:none; margin:10px 0 0 0;) #nav li ul li a ( display:block; padding:5px 10px; color:#A2E200; text- decoration:none;) #nav li ul li:hover a ( background-color:#606060; -moz-border-radius:5px; -webkit-border-radius:5px;) #nav li span ( cursor:pointer; margin :0px 10px; font-weight:bold;

Here's how to do it using pure CSS code. I still can’t figure out why I should use JavaScript, except maybe for .

Option 2 - horizontal menu + solution for IE6

Please note that option 1 will not work in IE6. The solution to compatibility of the menu with IE6 will be a piece of js code. More to the point, let's create a menu. Below is a screenshot of the future menu:

The menu design is similar to the first version, so let's move on to the code itself.

#menu ( padding: 0; margin: 0; font-size: 100%; font-family: Georgia; ) #menu li ( list-style: none; float: left; height: 33px; padding: 0; margin: 0 ; width: 150px; text-align: background: #171717; position: relative padding-top: 12px; display: none; position: absolute; left: 0; top: 45px; ) #menu li ul li ( float: none; margin: 0; width: 150px; text-align: center; background: #7F7F7F; ) #menu li a ( display: block; width: 150px; height: 33px; color: #fff; text-decoration: none; ) #menu li:hover ul, #menu li.jshover ul ( display: block; ) # menu li:hover, #menu li.jshover ( background: #424242; )

JS - solution for IE6

jsHover = function() ( var hEls = document.getElementById("menu").getElementsByTagName("li"); for (var i=0, len=hEls.length; i

All tags after this (and other tags related to HTML5) will be perceived normally by older browsers.

Let's return directly to our markup. Next, we need to insert a bulleted list into our tag, for me it looks like this:

  • home
  • About Us
  • Portfolio
  • Blog
  • Contacts

So, we seem to have finished with the markup, it’s time to start writing styles, since now our menu doesn’t look very good, to put it mildly:

Writing styles for our horizontal menu

And so, the first thing we need to do when laying out the menu is to remove the list markers, we obviously don’t need them, we do it like this:

Ul( list-style:none; )

After this our menu will look like this:

I don't really like how our menu is stuck to the edges of the browser, let's fix that:

With this code, we set the width of our menu, gave it 50px top and bottom margins, and positioned it in the center. Who knows, if a block element has a width, then in order to position this element strictly in the center we just need to give it an external margin (margin) on the right and left with the value auto.

The next step is to finally make our menu horizontal; this is done by setting the elements

  • float: left

    Menu li( float:left; )

    Our entire menu has now become horizontal.

    If you do not understand what exactly happened and what the float property does, I recommend that you google information about this property and study it thoroughly, since
    Not a single page of layout can do without it, I can tell you that for sure. Well, okay, let's continue!

    Menu li a( display:block;/* Make the link a block element*/ padding:12px 20px;/* Set the padding */ text-decoration: none; /* remove the underline */ color:#fff;/* set the color links white */ background:#444;/* make the background color dark */ font:14px Verdana, sans-serif;/* set the font size and name */ )

    One of the most important rules here is display:block;. The fact is that by default links are inline elements, and indentations are applied differently to inline elements in different browsers, so it is advisable to make the link a block element and only then apply to it the properties associated with external or internal indents. Now I don’t want to burden you with unnecessary information; over time, using real examples, you will understand why such an emphasis is placed here.

    Let's see what we got, refresh the browser page and there you go!:

    As you can see, it doesn’t look bad, we can say that, in principle, our menu is ready. The only thing that still needs to be done is to set the light of the links when hovering, and it seems to me that the menu will look better with separators between the items.

    Let's start with the delimiters:

    Menu li( border-left:1px solid #666; ) .menu li:first-child( border-left:none; )

    What have we done here? Yes, everything is very simple, we set for our points (

  • ) left border of 1px size and color #666;. As for the .menu li:first-child selector, here we use a special pseudo-class that allows us to select the first child element of the list. I also recommend reading about pseudo-classes in more detail on the Internet, you will learn a lot of useful things.

    Let's see what we got again:

    In my opinion, it's much better with delimiters.

    Menu li a:hover( background:#888; )

    Again using a special pseudo-class, this time hover, we set the color of the link when hovering over it, look:

    I think it’s cool :) I hope you end up with the same menu as mine.

    This is where I will end this lesson, I really hope that it was useful to you and now you know how to layout a simple horizontal menu using pure html and css. Of course, we created a one-level menu; it will be a little more complicated with a two-level menu (with a nested list), but this is a topic for another lesson, that’s all for me. Come again, I will be glad!!!