Morgan Klaif Morgan Klaif - 2 months ago 9
CSS Question

CSS Submenu Items - Not Positioned Correctly

I found a tutorial online for a responsive website, but I am wanting to change the menu it provides into a sub menu drop down for specific items. When I tried to add one, it shows the sub menu items correctly, but does not position them correctly.

Here is the code:



.mainheader nav {
background-color: #666;
height: 40px;
border-radius: 5px;
-moz-border-radius: 5px;
-webkit-border-radius: 5px;
}
.mainheader nav ul {
list-style: none;
margin: 0 auto;
}
.mainheader nav ul ul {
position: absolute;
visibility: hidden;
}
.mainheader nav ul li:hover ul {
background-color: #666;
visibility: visible;
}
.mainheader nav ul li {
float: left;
display: inline;
}
.mainheader nav a:link,
.mainheader nav a:visited {
color: #FFF;
display: inline-block;
padding: 10px 25px;
height: 20px;
}
.mainheader nav a:hover,
.mainheader nav a:active,
.mainheader nav .active a:link,
.mainheader nav .active a:visited {
background-color: #CF5C3F;
text-shadow: none;
}
.mainheader nav ul li a {
border-radius: 5px;
-moz-border-radius: 5px;
-webkit-border-radius: 5px;
}

<header class="mainheader">
<nav>
<ul>
<li class="active"><a href="#">Home</a>
</li>
<li><a href="#">About</a>
</li>
<li>
<a href="#">Services</a>
<ul>
<li><a href="#">Test</a>
</li>
<li><a href="#">Test</a>
</li>
</ul>
</li>
<li><a href="#">Contact</a>
</li>
</ul>
</nav>
</header>





Here is the JSFiddle.

How do I get it so the two Test sub menu items are positioned below services, centered as services is, and below each other?

woz woz
Answer

Add this to get the <li> elements to stack vertically:

.mainheader nav ul ul li {
  clear: both;
}

Also, by default, a <ul> will be indented. Add this to .mainheader nav ul ul to fix the alignment:

padding: 0;
list-style-type: none;

See this: https://jsfiddle.net/wynLsuwa/3/.