CSS Bootstrap多级导航

时间:2017-12-17 12:49:04

标签: css twitter-bootstrap-3

我正在尝试使用bootstrap进行多级导航。现在我有这样的导航。



.navbar-custom {
  background: #42596b !important;
  border: 1px solid #42596b;
}

.navbar-custom .navbar-brand {
  color: #ffffff;
}

.navbar-custom .navbar-brand:hover {
  color: #cecece;
}

.navbar-custom .navbar-nav li a {
  color: #ffffff;
}

.navbar-custom .navbar-nav .dropdown a {
  background: #3e3f49;
}

.navbar-custom .navbar-nav .dropdown-menu li a {
  color: #00a3b5;
}

.navbar-default .navbar-nav>li>a:hover,
.navbar-default .navbar-nav>li>a:focus {
  color: #ade3ff;
}

.navbar-custom .multi-level {
  background: #3e3f49;
  padding: 24px 0;
  letter-spacing: 2px;
}

.navbar-custom .navbar-nav .multi-level .dropdown-submenu {
  border-top: 1px solid transparent;
  border-bottom: 1px solid transparent;
  padding: 10px 21px;
}

.navbar-custom .navbar-nav .multi-level .dropdown-submenu:hover {
  border-top: 1px solid #35363f;
  border-bottom: 1px solid #35363f;
  padding: 10px 21px;
}

.navbar-custom .navbar-nav .multi-level .dropdown-submenu>a {
  text-decoration: none;
  position: relative;
  color: #fff !important;
  line-height: 24px;
  font-size: 14px;
  padding: 0;
  text-transform: none;
  line-height: 1.2;
}

.navbar-brand {
  padding-top: 6px;
}

.navbar-brand img {
  width: 52px;
  display: inline-block;
}

.navbar-brand span {
  display: inline-block;
}


/* Dropdown submenu */

.dropdown-submenu {
  position: relative;
}

.dropdown-submenu>.dropdown-menu {
  top: 59px;
  left: 100%;
  position: absolute;
  margin-top: -6px;
  margin-left: -1px;
  -webkit-border-radius: 0 6px 6px 6px;
  -moz-border-radius: 0 6px 6px;
  border-radius: 0 6px 6px 6px;
  padding: 24px 0;
  min-width: 490px;
  height: 410px;
}

.dropdown-submenu>.dropdown-menu ul {
  float: left;
  width: 50%;
  padding: 0 10px 0 21px;
  list-style: none;
  margin: 0px;
}

.dropdown-submenu>.dropdown-menu ul>.category-label,
.dropdown-submenu>.dropdown-menu ul>.feature-label {
  font-size: 14px;
  font-weight: 600;
  padding-left: 0px;
  margin-bottom: 10px;
  padding: 11px 0 7px;
  line-height: 1.2;
  color: #3e3f49;
}

.dropdown-submenu>.dropdown-menu>.feature-list>li {
  border-left: 1px solid #dbdbdb;
}

.dropdown-submenu>.dropdown-menu ul>.feature-label {
  color: #ad452d;
  border: none;
}

.dropdown-submenu>.dropdown-menu ul>.level1 {
  padding-left: 12px;
  padding-bottom: 19px;
}

.dropdown-submenu>.dropdown-menu li a {
  background: #ffffff !important;
  text-transform: none;
  color: #3e3f49 !important;
  font-weight: normal;
}

.dropdown-submenu:hover>.dropdown-menu {
  display: block;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script type="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

<nav class="navbar navbar-default navbar-fixed-top  navbar-custom">
  <div class="container">
    <div class="navbar-header">
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
      <a class="navbar-brand" href="#">
        <img src="img/logo.png">
        <span>Brand</span>
      </a>
    </div>

    <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
      <ul class="nav navbar-nav">
        <li class="dropdown">
          <a id="dLabel" role="button" data-toggle="dropdown" class="btn btn-primary" data-target="#" href="/page.html">
                  MENU <span class="caret"></span>
              </a>
          <ul class="dropdown-menu multi-level" role="menu" aria-labelledby="dropdownMenu">
            <li class="dropdown-submenu">
              <a tabindex="-1" href="#">Informacije</a>
              <nav class="dropdown-menu">
                <ul class="level0">
                  <li class="category-label">Najave Dogadjaja</li>
                  <li class="level1"><a href="#">Seminari</a></li>
                  <li class="level1"><a href="#">Savetovanja</a></li>
                  <li class="level1"><a href="#">Predavanja</a></li>
                  <li class="level1"><a href="#">Radionice</a></li>
                  <li class="level1"><a href="#">Manifestacije</a></li>
                  <li class="level1"><a href="#">Posete</a></li>
                </ul>

                <ul class="feature-list level0">
                  <li class="feature-label">Feature title</li>
                  <li class="level1"><a href="#">Second level</a></li>
                  <li class="level1"><a href="#">Second level</a></li>
                </ul>
              </nav>
            </li>
            <li class="dropdown-submenu">
              <a tabindex="-1" href="#">Strucni Saveti</a>
              <nav class="dropdown-menu">
                <ul class="level0">
                  <li class="category-label">Strucni Saveta</li>
                  <li class="level1"><a href="#">Forum - Potrazite Strucni Savet</a></li>
                  <li class="level1"><a href="#">Preporuke</a></li>
                  <li class="level1"><a href="#">SMSZnanje</a></li>
                </ul>

                <ul class="feature-list level0">
                  <li class="feature-label">Feature title</li>
                  <li class="level1"><a href="#">Second level</a></li>
                  <li class="level1"><a href="#">Second level</a></li>
                </ul>
              </nav>
            </li>
            <li class="dropdown-submenu">
              <a tabindex="-1" href="#">Savetodavstvo</a>
              <nav class="dropdown-menu">
                <ul class="level0">
                  <li class="category-label">Savetodavstvo</li>
                  <li class="level1"><a href="#">Pokrajinski Sekretarijat</a></li>
                  <li class="level1"><a href="#">Centar Za Usavrsavanje</a></li>
                  <li class="level1"><a href="#">Centar Za Ruralni Razvoj</a></li>
                  <li class="level1"><a href="#">Savetodavne Sluzbe</a></li>
                  <li class="level1"><a href="#">Savetodavci</a></li>
                </ul>

                <ul class="feature-list level0">
                  <li class="feature-label">Feature title</li>
                  <li class="level1"><a href="#">Second level</a></li>
                  <li class="level1"><a href="#">Second level</a></li>
                </ul>
              </nav>
            </li>
            <li class="dropdown-submenu">
              <a tabindex="-1" href="#">Publikacije</a>
              <nav class="dropdown-menu">
                <ul class="level0">
                  <li class="category-label">Publikacije</li>
                  <li class="level1"><a href="#">Prezentacije</a></li>
                  <li class="level1"><a href="#">Aktuelni Savetnik</a></li>
                  <li class="level1"><a href="#">Rezultati Ogleda</a></li>
                  <li class="level1"><a href="#">Bilteni PSS</a></li>
                  <li class="level1"><a href="#">Agroplaner</a></li>
                  <li class="level1"><a href="#">Ostala Dokumenta</a></li>
                  <li class="level1"><a href="#">Uputsva i Prirucnici</a></li>
                </ul>

                <ul class="feature-list level0">
                  <li class="feature-label">Feature title</li>
                  <li class="level1"><a href="#">Second level</a></li>
                  <li class="level1"><a href="#">Second level</a></li>
                </ul>
              </nav>
            </li>
            <li class="dropdown-submenu">
              <a tabindex="-1" href="#">Pravilnici, Konkursi, Uredbe</a>
              <nav class="dropdown-menu">
                <ul class="level0">
                  <li class="category-label">Pravilnici, Konkursi, Uredbe</li>
                  <li class="level1"><a href="#">Pravilnici, Konkursi, Uredbe</a></li>
                  <li class="level1"><a href="#">Konkursi APV</a></li>
                  <li class="level1"><a href="#">Ostali Konkursi</a></li>
                </ul>

                <ul class="feature-list level0">
                  <li class="feature-label">Feature title</li>
                  <li class="level1"><a href="#">Second level</a></li>
                  <li class="level1"><a href="#">Second level</a></li>
                </ul>
              </nav>
            </li>
            <li class="dropdown-submenu">
              <a tabindex="-1" href="#">Ostali Prilozi</a>
              <nav class="dropdown-menu">
                <ul class="level0">
                  <li class="category-label">Ostali Prilozi</li>
                  <li class="level1"><a href="#">Korisni Linkovi</a></li>
                  <li class="level1"><a href="#">Korisni Alati</a></li>
                  <li class="level1"><a href="#">Prikaz Aktuelnih Izdanja</a></li>
                  <li class="level1"><a href="#">Doktorske Disertacije</a></li>
                  <li class="level1"><a href="#">Pisma Administratoru</a></li>
                  <li class="level1"><a href="#">Objavljeno na Medijima</a></li>
                  <li class="level1"><a href="#">Uslovi Koriscenja</a></li>
                  <li class="level1"><a href="#">In Memoriam</a></li>
                </ul>

                <ul class="feature-list level0">
                  <li class="feature-label">Feature title</li>
                  <li class="level1"><a href="#">Second level</a></li>
                  <li class="level1"><a href="#">Second level</a></li>
                </ul>
              </nav>
            </li>
            <li class="dropdown-submenu">
              <a tabindex="-1" href="#">Galerija</a>
              <nav class="dropdown-menu">
                <ul class="level0">
                  <li class="category-label">Galerija</li>
                  <li class="level1"><a href="#">Video Zapisi</a></li>
                  <li class="level1"><a href="#">Fotografije</a></li>
                </ul>

                <ul class="feature-list level0">
                  <li class="feature-label">Feature title</li>
                  <li class="level1"><a href="#">Second level</a></li>
                  <li class="level1"><a href="#">Second level</a></li>
                </ul>
              </nav>
            </li>
          </ul>
        </li>
      </ul>
    </div>
  </div>
</nav>
&#13;
&#13;
&#13;

JSFiddle

如何让我的子菜单(白色)始终与我的主菜单(黑色)保持在同一行的顶部

1 个答案:

答案 0 :(得分:1)

只需为top替换.dropdown-submenu>.dropdown-menu的值,并将其设为0:

.dropdown-submenu>.dropdown-menu {
    top: 0;
    ...

}

.dropdown-submenu

中移除相对位置

完整代码:

.navbar-custom {
  background: #42596b !important;
  border: 1px solid #42596b;
}

.navbar-custom .navbar-brand {
  color: #ffffff;
}

.navbar-custom .navbar-brand:hover {
  color: #cecece;
}

.navbar-custom .navbar-nav li a {
  color: #ffffff;
}

.navbar-custom .navbar-nav .dropdown a {
  background: #3e3f49;
}

.navbar-custom .navbar-nav .dropdown-menu li a {
  color: #00a3b5;
}

.navbar-default .navbar-nav > li > a:hover, .navbar-default .navbar-nav > li > a:focus {
  color: #ade3ff;
}

.navbar-custom .multi-level {
  background: #3e3f49;
  padding: 24px 0;
  letter-spacing: 2px;
}

.navbar-custom .navbar-nav .multi-level .dropdown-submenu {
  border-top: 1px solid transparent;
  border-bottom: 1px solid transparent;
  padding: 10px 21px;
}

.navbar-custom .navbar-nav .multi-level .dropdown-submenu:hover {
  border-top: 1px solid #35363f;
  border-bottom: 1px solid #35363f;
  padding: 10px 21px;
}

.navbar-custom .navbar-nav .multi-level .dropdown-submenu>a {
  text-decoration: none;
  position: relative;
  color: #fff !important;
  line-height: 24px;
  font-size: 14px;
  padding: 0;
  text-transform: none;
  line-height: 1.2;
}

.navbar-brand {
  padding-top: 6px;
}

.navbar-brand img {
  width: 52px;
  display: inline-block;
}

.navbar-brand span {
  display: inline-block;
}
/* Dropdown submenu */


.dropdown-submenu>.dropdown-menu {
    top: 0;
    left: 100%;
    position: absolute;
    margin-top: -6px;
    margin-left: -1px;
    -webkit-border-radius: 0 6px 6px 6px;
    -moz-border-radius: 0 6px 6px;
    border-radius: 0 6px 6px 6px;
    padding: 24px 0;
    min-width: 490px;
    height: 410px;
}

.dropdown-submenu>.dropdown-menu ul {
  float: left;
  width: 50%;
  padding: 0 10px 0 21px;
  list-style: none;
  margin: 0px;
}

.dropdown-submenu>.dropdown-menu ul>.category-label, .dropdown-submenu>.dropdown-menu ul>.feature-label {
  font-size: 14px;
  font-weight: 600;
  padding-left: 0px;
  margin-bottom: 10px;
  padding: 11px 0 7px;
  line-height: 1.2;
  color: #3e3f49;
}

.dropdown-submenu>.dropdown-menu>.feature-list>li {
  border-left: 1px solid #dbdbdb;
}

.dropdown-submenu>.dropdown-menu ul>.feature-label {
  color: #ad452d;
  border: none;
}

.dropdown-submenu>.dropdown-menu ul>.level1 {
  padding-left: 12px;
  padding-bottom: 19px;
}

.dropdown-submenu>.dropdown-menu li a {
    background: #ffffff !important;
    text-transform: none;
    color: #3e3f49 !important;
    font-weight: normal;
}

.dropdown-submenu:hover>.dropdown-menu {
    display: block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

<nav class="navbar navbar-default navbar-fixed-top  navbar-custom">
      <div class="container">
        <div class="navbar-header">
          <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
          <a class="navbar-brand" href="#">
            <img src="img/logo.png" >
            <span>Brand</span>
          </a>
        </div>

        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
          <ul class="nav navbar-nav">
            <li class="dropdown">
              <a id="dLabel" role="button" data-toggle="dropdown" class="btn btn-primary" data-target="#" href="/page.html">
                  MENU <span class="caret"></span>
              </a>
              <ul class="dropdown-menu multi-level" role="menu" aria-labelledby="dropdownMenu">
                <li class="dropdown-submenu">
                  <a tabindex="-1" href="#">Informacije</a>
                  <nav class="dropdown-menu">
                    <ul class="level0">
                      <li class="category-label">Najave Dogadjaja</li>
                      <li class="level1"><a href="#">Seminari</a></li>
                      <li class="level1"><a href="#">Savetovanja</a></li>
                      <li class="level1"><a href="#">Predavanja</a></li>
                      <li class="level1"><a href="#">Radionice</a></li>
                      <li class="level1"><a href="#">Manifestacije</a></li>
                      <li class="level1"><a href="#">Posete</a></li>
                    </ul>

                    <ul class="feature-list level0">
                      <li class="feature-label">Feature title</li>
                      <li class="level1"><a href="#">Second level</a></li>
                      <li class="level1"><a href="#">Second level</a></li>
                    </ul>
                  </nav>
                </li>
                <li class="dropdown-submenu">
                  <a tabindex="-1" href="#">Strucni Saveti</a>
                  <nav class="dropdown-menu">
                    <ul class="level0">
                      <li class="category-label">Strucni Saveta</li>
                      <li class="level1"><a href="#">Forum - Potrazite Strucni Savet</a></li>
                      <li class="level1"><a href="#">Preporuke</a></li>
                      <li class="level1"><a href="#">SMSZnanje</a></li>
                    </ul>

                    <ul class="feature-list level0">
                      <li class="feature-label">Feature title</li>
                      <li class="level1"><a href="#">Second level</a></li>
                      <li class="level1"><a href="#">Second level</a></li>
                    </ul>
                  </nav>
                </li>
                <li class="dropdown-submenu">
                  <a tabindex="-1" href="#">Savetodavstvo</a>
                  <nav class="dropdown-menu">
                    <ul class="level0">
                      <li class="category-label">Savetodavstvo</li>
                      <li class="level1"><a href="#">Pokrajinski Sekretarijat</a></li>
                      <li class="level1"><a href="#">Centar Za Usavrsavanje</a></li>
                      <li class="level1"><a href="#">Centar Za Ruralni Razvoj</a></li>
                      <li class="level1"><a href="#">Savetodavne Sluzbe</a></li>
                      <li class="level1"><a href="#">Savetodavci</a></li>
                    </ul>

                    <ul class="feature-list level0">
                      <li class="feature-label">Feature title</li>
                      <li class="level1"><a href="#">Second level</a></li>
                      <li class="level1"><a href="#">Second level</a></li>
                    </ul>
                  </nav>
                </li>
                <li class="dropdown-submenu">
                  <a tabindex="-1" href="#">Publikacije</a>
                  <nav class="dropdown-menu">
                    <ul class="level0">
                      <li class="category-label">Publikacije</li>
                      <li class="level1"><a href="#">Prezentacije</a></li>
                      <li class="level1"><a href="#">Aktuelni Savetnik</a></li>
                      <li class="level1"><a href="#">Rezultati Ogleda</a></li>
                      <li class="level1"><a href="#">Bilteni PSS</a></li>
                      <li class="level1"><a href="#">Agroplaner</a></li>
                      <li class="level1"><a href="#">Ostala Dokumenta</a></li>
                      <li class="level1"><a href="#">Uputsva i Prirucnici</a></li>
                    </ul>

                    <ul class="feature-list level0">
                      <li class="feature-label">Feature title</li>
                      <li class="level1"><a href="#">Second level</a></li>
                      <li class="level1"><a href="#">Second level</a></li>
                    </ul>
                  </nav>
                </li>
                <li class="dropdown-submenu">
                  <a tabindex="-1" href="#">Pravilnici, Konkursi, Uredbe</a>
                  <nav class="dropdown-menu">
                    <ul class="level0">
                      <li class="category-label">Pravilnici, Konkursi, Uredbe</li>
                      <li class="level1"><a href="#">Pravilnici, Konkursi, Uredbe</a></li>
                      <li class="level1"><a href="#">Konkursi APV</a></li>
                      <li class="level1"><a href="#">Ostali Konkursi</a></li>
                    </ul>

                    <ul class="feature-list level0">
                      <li class="feature-label">Feature title</li>
                      <li class="level1"><a href="#">Second level</a></li>
                      <li class="level1"><a href="#">Second level</a></li>
                    </ul>
                  </nav>
                </li>
                <li class="dropdown-submenu">
                  <a tabindex="-1" href="#">Ostali Prilozi</a>
                  <nav class="dropdown-menu">
                    <ul class="level0">
                      <li class="category-label">Ostali Prilozi</li>
                      <li class="level1"><a href="#">Korisni Linkovi</a></li>
                      <li class="level1"><a href="#">Korisni Alati</a></li>
                      <li class="level1"><a href="#">Prikaz Aktuelnih Izdanja</a></li>
                      <li class="level1"><a href="#">Doktorske Disertacije</a></li>
                      <li class="level1"><a href="#">Pisma Administratoru</a></li>
                      <li class="level1"><a href="#">Objavljeno na Medijima</a></li>
                      <li class="level1"><a href="#">Uslovi Koriscenja</a></li>
                      <li class="level1"><a href="#">In Memoriam</a></li>
                    </ul>

                    <ul class="feature-list level0">
                      <li class="feature-label">Feature title</li>
                      <li class="level1"><a href="#">Second level</a></li>
                      <li class="level1"><a href="#">Second level</a></li>
                    </ul>
                  </nav>
                </li>
                <li class="dropdown-submenu">
                  <a tabindex="-1" href="#">Galerija</a>
                  <nav class="dropdown-menu">
                    <ul class="level0">
                      <li class="category-label">Galerija</li>
                      <li class="level1"><a href="#">Video Zapisi</a></li>
                      <li class="level1"><a href="#">Fotografije</a></li>
                    </ul>

                    <ul class="feature-list level0">
                      <li class="feature-label">Feature title</li>
                      <li class="level1"><a href="#">Second level</a></li>
                      <li class="level1"><a href="#">Second level</a></li>
                    </ul>
                  </nav>
                </li>
              </ul>
            </li>
          </ul>
        </div>
      </div>
    </nav>