Morgan Klaif
Morgan Klaif

Reputation: 75

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?

Upvotes: 0

Views: 47

Answers (3)

Tom
Tom

Reputation: 2645

I assume the below is what you're trying to achieve?

Note:

.mainheader nav ul ul {
  left: 0;
  width: 100%;
}

And:

.mainheader nav ul li {
  position: relative;
}

.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;
  left: 0;
  width: 100%;
}
.mainheader nav ul li:hover ul {
  background-color: #666;
  visibility: visible;
}
.mainheader nav ul li {
  float: left;
  display: inline;
  position: relative;
}
.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>

Upvotes: 0

Johannes
Johannes

Reputation: 67798

Add these two rules:

.mainheader nav ul li ul {
  padding: 0;
  min-width: 110px;
}
.mainheader nav ul li ul li {
  float: none;
  display: block;
  text-align: left;
}

Fiddle: https://jsfiddle.net/c19hku1o/1/

Upvotes: 0

woz
woz

Reputation: 11004

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/.

Upvotes: 1

Related Questions