quirky purple
quirky purple

Reputation: 2219

Horizontal navbar menu submenu alignment

In the following code, I am creating a navigation bar. When you hover on an item in the navbar, it displays a drop down menu. Certain items on the drop down menu display a submenu when hovered. All this is working, but when the submenu displays, I am having issues with the positioning of its top property. I have tried making its' parent div position:relative, adjusting it's top: position, adjusting the display property, etc., but can't seem to locate the problem.

#nav {
  width: 886px;
  height: 32px;
  margin: 1px auto 0 auto;
  border-top: 1px solid #B1B6B9;
  border-bottom: 1px solid #646E73;
  background-color: #A9AFB2;
  /* 50% gray */
  background: -moz-linear-gradient(top, #bbbfc1 0%, #7f878b 100%);
  /* FF3.6+ */
  background: -webkit-gradient(linear, left top, left bottom, color-stop(0%, #bbbfc1), color-stop(100%, #7f878b));
  /* Chrome,Safari4+ */
  background: -webkit-linear-gradient(top, #bbbfc1 0%, #7f878b 100%);
  /* Chrome10+,Safari5.1+ */
  background: -o-linear-gradient(top, #bbbfc1 0%, #7f878b 100%);
  /* Opera 11.10+ */
  background: -ms-linear-gradient(top, #bbbfc1 0%, #7f878b 100%);
  /* IE10+ */
  background: linear-gradient(to bottom, #bbbfc1 0%, #7f878b 100%);
  /* W3C */
  filter: progid: DXImageTransform.Microsoft.Gradient (GradientType=0, StartColorStr='#ffBBBFC1', EndColorStr='#ff7F878B');
  padding: 0 40px;
}

.navone {
  position: relative;
  top: 0;
  left: 0;
  height: 32px;
  float: left;
  margin-bottom: -3px;
  z-index: 150;
  width: auto;
}

.navone a {
  text-decoration: none;
  float: left;
  height: 16px;
  color: #FFFFFF;
  font-family: Jabas, Arial, Helvetica, sans-serif;
  font-size: 19px;
  padding: 5px 14px 11px 14px;
  display: block;
  border-left: 1px solid #B1B6B9;
  border-right: 1px solid #737C81;
  text-shadow: -1px -1px 0px rgba(84, 95, 101, .35);
}

.navone a:hover {
  text-decoration: none;
  background-color: #567697;
  /* 75% dark blue */
  background: -moz-linear-gradient(top, #567697 0%, #1e4975 100%);
  /* FF3.6+ */
  background: -webkit-gradient(linear, left top, left bottom, color-stop(0%, #567697), color-stop(100%, #1e4975));
  /* Chrome,Safari4+ */
  background: -webkit-linear-gradient(top, #567697 0%, #1e4975 100%);
  /* Chrome10+,Safari5.1+ */
  background: -o-linear-gradient(top, #567697 0%, #1e4975 100%);
  /* Opera 11.10+ */
  background: -ms-linear-gradient(top, #567697 0%, #1e4975 100%);
  /* IE10+ */
  background: linear-gradient(to bottom, #567697 0%, #1e4975 100%);
  /* W3C */
  filter: progid: DXImageTransform.Microsoft.Gradient (GradientType=0, StartColorStr='#ff567697', EndColorStr='#ff1E4975');
}

.navone a.active {
  background-color: #89A8C7;
  /* 55% blue */
  background: -moz-linear-gradient(top, #89a8c7 0%, #5480ad 100%);
  /* FF3.6+ */
  background: -webkit-gradient(linear, left top, left bottom, color-stop(0%, #89a8c7), color-stop(100%, #5480ad));
  /* Chrome,Safari4+ */
  background: -webkit-linear-gradient(top, #89a8c7 0%, #5480ad 100%);
  /* Chrome10+,Safari5.1+ */
  background: -o-linear-gradient(top, #89a8c7 0%, #5480ad 100%);
  /* Opera 11.10+ */
  background: -ms-linear-gradient(top, #89a8c7 0%, #5480ad 100%);
  /* IE10+ */
  background: linear-gradient(to bottom, #89a8c7 0%, #5480ad 100%);
  /* W3C */
  filter: progid: DXImageTransform.Microsoft.Gradient (GradientType=0, StartColorStr='#ff89A8C7', EndColorStr='#ff5480AD');
}

.navone:hover .navtwo {
  display: block;
}

.navtwo {
  border-top: 1px solid #65809B;
  display: none;
  position: absolute;
  top: 32px;
  left: 0px;
  z-index: 200;
  width: 100%;
  -webkit-box-shadow: 4px 4px 3px rgba(84, 95, 101, .3);
  -moz-box-shadow: 4px 4px 3px rgba(84, 95, 101, .3);
  box-shadow: 4px 4px 3px rgba(84, 95, 101, .3);
}

.navtwo a {
  color: #3E6082;
  background-color: #D8E2EC;
  font-family: Arial, Helvetica, sans-serif;
  font-size: 13px;
  font-weight: normal;
  width: 90%;
  height: auto;
  display: block;
  padding: 4px 5%;
  border: 1px solid #B1C5DA;
  box-sizing: box-border;
  border-top: 1px solid #FFFFFF;
  text-shadow: none;
}

.navtwo a:hover {
  color: #FFFFFF;
  background-color: #3E6082;
  text-decoration: none;
}

.dropdown {
  position: relative;
}

.dropdown > a {
  background: #C1C5C7 !important;
  color: #32393D !important;
  font-weight: bold;
  border-top: 1px solid #E0E2E3;
  border-bottom: 1px solid #A2A8AB !important;
  z-index: 50;
}

.dropdown > a:after {
  float: right;
  content: "\25B6"
}

.dropdown:hover > a {
  background: #92999D !important;
}

.dropdown-content {
  display: none;
  z-index: 100;
}

.dropdown-content a {
  padding: 4px 8px;
  width: 150px;
}

.dropdown:hover .dropdown-content {
  display: block;
  position: absolute;
  top: 0;
  left: 100%;
}
<div id="nav">
  <!--begin of option-->
  <div class="navone">
    <a href="">Option</a>
    <div class="navtwo">
      <!--begin of dropdown menu-->
      <div class="dropdown">
        <a href="">Test 1</a>
        <!--begin of submenu-->
        <div class="dropdown-content">
          <a href="#">Test Submenu</a>
          <a href="#">Test Long Submenu</a>
          <a href="#">Test Longest Submenu</a>
        </div>
      </div>
      <!--begin of dropdown menu-->
      <div class="dropdown">
        <a href="">Test 2</a>
        <!--begin of submenu-->
        <div class="dropdown-content">
          <a href="#">Test</a>
          <a href="#">Long Test</a>
          <a href="#">The Longest Test</a>
        </div>
      </div>
      <a href="">Test 3</a>
    </div>
  </div>
  <!--end of option-->


  <div class="navone">
    <a href="#">Longer Option Name</a>
    <div class="navtwo">
      <div class="dropdown">
        <a href="">Test 1</a>
        <div class="dropdown-content">
          <a href="#">Test Submenu</a>
          <a href="#">Test Long Submenu</a>
          <a href="#">Test Longest Submenu</a>
        </div>
      </div>
      <a href="">Test 2</a>
      <a href="">Test 3</a>
    </div>
  </div>

</div>

or JSFiddle: https://jsfiddle.net/cshanno/1fsk6fm2/5/

 WANTED                                     MINE
 -----------                               -----------
| NavOption |                             | NavOption |
 -----------                               ----------- ---------
| Dropdown  |                             | Dropdown  | Submenu |
 ----------- ----------                    ----------- ---------
| Dropdown >| Submenu  |                  | Dropdown >|           < The submenu is shifting
 ----------- ----------                    -----------              to the top 

Where the > shows the drop down item that contains sub menu options.

Upvotes: 2

Views: 114

Answers (2)

user3186555
user3186555

Reputation:

The issue has to do with your display property of your .dropdowns. if you set the display property to inline-block on those <div>s they should render more correctly. You may have to add width: 100% to your .dropdown CSS and reduce the white space in between each menu item by adding font-size: 0 to .navtwo css, but it works:

Also, Ignacio's float solution, does pretty much the same thing.

Display: block for those <div>s is the problem. floating or displaying as inline is your best solution

#nav {
  width: 886px;
  height: 32px;
  margin: 1px auto 0 auto;
  border-top: 1px solid #B1B6B9;
  border-bottom: 1px solid #646E73;
  background-color: #A9AFB2;
  /* 50% gray */
  background: -moz-linear-gradient(top, #bbbfc1 0%, #7f878b 100%);
  /* FF3.6+ */
  background: -webkit-gradient(linear, left top, left bottom, color-stop(0%, #bbbfc1), color-stop(100%, #7f878b));
  /* Chrome,Safari4+ */
  background: -webkit-linear-gradient(top, #bbbfc1 0%, #7f878b 100%);
  /* Chrome10+,Safari5.1+ */
  background: -o-linear-gradient(top, #bbbfc1 0%, #7f878b 100%);
  /* Opera 11.10+ */
  background: -ms-linear-gradient(top, #bbbfc1 0%, #7f878b 100%);
  /* IE10+ */
  background: linear-gradient(to bottom, #bbbfc1 0%, #7f878b 100%);
  /* W3C */
  filter: progid: DXImageTransform.Microsoft.Gradient (GradientType=0, StartColorStr='#ffBBBFC1', EndColorStr='#ff7F878B');
  padding: 0 40px;
}

.navone {
  position: relative;
  top: 0;
  left: 0;
  height: 32px;
  float: left;
  margin-bottom: -3px;
  z-index: 150;
  width: auto;
}

.navone a {
  text-decoration: none;
  float: left;
  height: 16px;
  color: #FFFFFF;
  font-family: Jabas, Arial, Helvetica, sans-serif;
  font-size: 19px;
  padding: 5px 14px 11px 14px;
  display: block;
  border-left: 1px solid #B1B6B9;
  border-right: 1px solid #737C81;
  text-shadow: -1px -1px 0px rgba(84, 95, 101, .35);
}

.navone a:hover {
  text-decoration: none;
  background-color: #567697;
  /* 75% dark blue */
  background: -moz-linear-gradient(top, #567697 0%, #1e4975 100%);
  /* FF3.6+ */
  background: -webkit-gradient(linear, left top, left bottom, color-stop(0%, #567697), color-stop(100%, #1e4975));
  /* Chrome,Safari4+ */
  background: -webkit-linear-gradient(top, #567697 0%, #1e4975 100%);
  /* Chrome10+,Safari5.1+ */
  background: -o-linear-gradient(top, #567697 0%, #1e4975 100%);
  /* Opera 11.10+ */
  background: -ms-linear-gradient(top, #567697 0%, #1e4975 100%);
  /* IE10+ */
  background: linear-gradient(to bottom, #567697 0%, #1e4975 100%);
  /* W3C */
  filter: progid: DXImageTransform.Microsoft.Gradient (GradientType=0, StartColorStr='#ff567697', EndColorStr='#ff1E4975');
}

.navone a.active {
  background-color: #89A8C7;
  /* 55% blue */
  background: -moz-linear-gradient(top, #89a8c7 0%, #5480ad 100%);
  /* FF3.6+ */
  background: -webkit-gradient(linear, left top, left bottom, color-stop(0%, #89a8c7), color-stop(100%, #5480ad));
  /* Chrome,Safari4+ */
  background: -webkit-linear-gradient(top, #89a8c7 0%, #5480ad 100%);
  /* Chrome10+,Safari5.1+ */
  background: -o-linear-gradient(top, #89a8c7 0%, #5480ad 100%);
  /* Opera 11.10+ */
  background: -ms-linear-gradient(top, #89a8c7 0%, #5480ad 100%);
  /* IE10+ */
  background: linear-gradient(to bottom, #89a8c7 0%, #5480ad 100%);
  /* W3C */
  filter: progid: DXImageTransform.Microsoft.Gradient (GradientType=0, StartColorStr='#ff89A8C7', EndColorStr='#ff5480AD');
}

.navone:hover .navtwo {
  display: block;
}

.navtwo {
  border-top: 1px solid #65809B;
  display: none;
  position: absolute;
  top: 32px;
  left: 0px;
  z-index: 200;
  width: 100%;
  font-size: 0;
  -webkit-box-shadow: 4px 4px 3px rgba(84, 95, 101, .3);
  -moz-box-shadow: 4px 4px 3px rgba(84, 95, 101, .3);
  box-shadow: 4px 4px 3px rgba(84, 95, 101, .3);
}

.navtwo a {
  color: #3E6082;
  background-color: #D8E2EC;
  font-family: Arial, Helvetica, sans-serif;
  font-size: 13px;
  font-weight: normal;
  width: 90%;
  height: auto;
  display: block;
  padding: 4px 5%;
  border: 1px solid #B1C5DA;
  box-sizing: box-border;
  border-top: 1px solid #FFFFFF;
  text-shadow: none;
}

.navtwo a:hover {
  color: #FFFFFF;
  background-color: #3E6082;
  text-decoration: none;
}

.dropdown {
  position: relative;
  display: inline-block;
  width:100%;
}

.dropdown > a {
  background: #C1C5C7 !important;
  color: #32393D !important;
  font-weight: bold;
  border-top: 1px solid #E0E2E3;
  border-bottom: 1px solid #A2A8AB !important;
  z-index: 50;
}

.dropdown > a:after {
  float: right;
  content: "\25B6"
}

.dropdown:hover > a {
  background: #92999D !important;
}

.dropdown-content {
  display: none;
  z-index: 100;
}

.dropdown-content a {
  padding: 4px 8px;
  width: 150px;
}

.dropdown:hover .dropdown-content {
  display: block;
  position: absolute;
  top: 0;
  left: 100%;
}
<div id="nav">
  <!--begin of option-->
  <div class="navone">
    <a href="">Option</a>
    <div class="navtwo">
      <!--begin of dropdown menu-->
      <div class="dropdown">
        <a href="">Test 1</a>
        <!--begin of submenu-->
        <div class="dropdown-content">
          <a href="#">Test Submenu</a>
          <a href="#">Test Long Submenu</a>
          <a href="#">Test Longest Submenu</a>
        </div>
      </div>
      <!--begin of dropdown menu-->
      <div class="dropdown">
        <a href="">Test 2</a>
        <!--begin of submenu-->
        <div class="dropdown-content">
          <a href="#">Test</a>
          <a href="#">Long Test</a>
          <a href="#">The Longest Test</a>
        </div>
      </div>
      <a href="">Test 3</a>
    </div>
  </div>
  <!--end of option-->


  <div class="navone">
    <a href="#">Longer Option Name</a>
    <div class="navtwo">
      <div class="dropdown">
        <a href="">Test 1</a>
        <div class="dropdown-content">
          <a href="#">Test Submenu</a>
          <a href="#">Test Long Submenu</a>
          <a href="#">Test Longest Submenu</a>
        </div>
      </div>
      <a href="">Test 2</a>
      <a href="">Test 3</a>
    </div>
  </div>

</div>

or JSfiddle

Upvotes: 0

Inacio Schweller
Inacio Schweller

Reputation: 1986

I made a small change on the .dropdown selector and now it's working:

.dropdown {
  position: relative;
  float: left;
  clear: both;
  width: 100%; 
}

JSFiddle: https://jsfiddle.net/c71f6qzm/

Hope it helps!

Upvotes: 3

Related Questions