水平导航栏问题

时间:2015-10-27 10:45:36

标签: jquery html css navigation

我正在投资组合网站,需要一些帮助让我的导航栏水平工作。我正在调整我最近为另一个网站创建的垂直导航栏,但是我很难让它保持水平,尤其是努力让下拉菜单垂直扩展并实际下拉到导航栏下方而不是扩展到左边,因为它是目前。

我会在下面附上我的代码和小提琴。

HTML:

<div id='cssmenu'>
    <ul>
        <li><a href='#'>About</a></li>
        <li class='has-sub'><a href='#'>Work</a>
            <ul>
                <li><a href='#'>Commercial</a></li>
                <li><a href='#'>Residential</a></li>
            </ul>
        </li>
        <li><a href='#'>Contact</a></li>
    </ul>
</div>

CSS:

#cssmenu,
#cssmenu ul,
#cssmenu ul li,
#cssmenu ul li a {  
  margin: 0;
  padding: 0;
  border: 0;
  list-style: none;
  line-height: 1;
  display: inline-block;
  float: left;
  position: relative;
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
}

#cssmenu {
  width: 100%;
  font-family: Open Sans, sans-serif;
  color: #000;
}

#cssmenu ul ul {
  display: none;
}

#cssmenu > ul > li.active > ul {
  display: inline-block;
}

.align-right {
  float: left;
}

#cssmenu > ul > li > a {
  padding: 16px 22px;
  cursor: pointer;
  z-index: 2;
  font-size: 12px;
  text-decoration: none;
  color: #000;
  background: #fff;
  -webkit-transition: color .2s ease;
  -o-transition: color .2s ease;
  transition: color .2s ease;
}

#cssmenu > ul > li > a:hover {
  color: #ccc;
}

#cssmenu ul > li.has-sub > a:after {
  position: absolute;
  right: 26px;
  top: 19px;
  z-index: 5;
  display: block;
  height: 10px;
  width: 2px;
  background: #000;
  content: "";
  -webkit-transition: all 0.1s ease-out;
  -moz-transition: all 0.1s ease-out;
  -ms-transition: all 0.1s ease-out;
  -o-transition: all 0.1s ease-out;
  transition: all 0.1s ease-out;
}

#cssmenu ul > li.has-sub > a:before {
  position: absolute;
  right: 22px;
  top: 23px;
  display: block;
  width: 10px;
  height: 2px;
  background: #000;
  content: "";
  -webkit-transition: all 0.1s ease-out;
  -moz-transition: all 0.1s ease-out;
  -ms-transition: all 0.1s ease-out;
  -o-transition: all 0.1s ease-out;
  transition: all 0.1s ease-out;
}

#cssmenu ul > li.has-sub.open > a:after,
#cssmenu ul > li.has-sub.open > a:before {
  -webkit-transform: rotate(45deg);
  -moz-transform: rotate(45deg);
  -ms-transform: rotate(45deg);
  -o-transform: rotate(45deg);
  transform: rotate(45deg);
}

#cssmenu ul ul li a {
  padding: 14px 22px;
  cursor: pointer;
  z-index: 2;
  font-size: 12px;
  text-decoration: none;
  color: #000;
  background: #fff;
  -webkit-transition: color .2s ease;
  -o-transition: color .2s ease;
  transition: color .2s ease;
}

#cssmenu ul ul ul li a {
  padding-left: 32px;
}

#cssmenu ul ul li a:hover {
  color: #ccc;
}

#cssmenu ul ul > li.has-sub > a:after {
  top: 16px;
  right: 26px;
  background: #000;
}

#cssmenu ul ul > li.has-sub > a:before {
  top: 20px;
  background: #000;
}

这里可以看到小提琴 - http://jsfiddle.net/yw9j1dow/1/

任何帮助都会很棒!

1 个答案:

答案 0 :(得分:0)

你需要添加一些定位:这里是一个更新的小提琴:http://jsfiddle.net/yw9j1dow/2/

我已添加此css:

#cssmenu ul li ul li {display: block; float: left;}
#cssmenu ul.sub-menu {position: absolute; top: 100%;}

此类也是html中的子导航:

 <ul class="sub-menu">

问题在于您的子导航是继承您之前在样式中设置的display: inline-block。您还需要absolute定位才能使列表正确向下流动。