Reputation: 13
today i ran into a problem with bootstrap while using a horizontal dropdown menu, it won't scroll with my navbar, obviously caused by this css-class:
.dropdown-menu {
width: 100%;
position: fixed;
top:210px;
z-index: 1000;
display: none;
float: left;
text-align:center;
}
But if i delete or change the position:fixed
tag my dropdown-menu is vertical again...
HTML
<div class="logo"><a href="#"><img src="img/logo.jpg" alt="logo"></a></div>
<nav class="navbar navbar-inverse" role="navigation">
<div class="container-fluid">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
</div>
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li><a href="#">mmenu1</a></li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">mmenu2</a>
<ul class="dropdown-menu" role="menu">
<li><a href="#">smenu1</a></li>
<li><a href="#">smenu2</a></li>
<li><a href="#">smenu3</a></li>
<li><a href="#">smenu4</a></li>
<li><a href="#">smenu5</a></li>
<li><a href="#">smenu6</a></li>
</ul>
</li>
<li><a href="#">mmenu3</a></li>
</ul>
</div><!-- /.navbar-collapse -->
</div><!-- /.container-fluid -->
</nav>
CSS
.logo{
text-align:center;
}
.navbar .navbar-nav {
font-size:1.9em;
font-family:verdana;
display: inline-block;
float: none;
}
.navbar-inverse .nav > li{
display:block;
}
ul.nav li.dropdown:hover ul.dropdown-menu{
display: block;
}
.navbar .navbar-collapse {
text-align: center;
}
.navbar-inverse .nav > li{
width:170px;
display:block;
}
.dropdown-menu {
width: 100%;
position: fixed;
top:210px;
z-index: 1000;
display: none;
float: left;
text-align:center;
}
.dropdown-menu li {
display:inline-block;
}
Here is the visualized code in bootply: http://www.bootply.com/cmjcUyHRTw
Is there any way to go around this problem?
Upvotes: 1
Views: 1888
Reputation: 1363
The problem is the width:100% on the .dropdown-menu is only expanding to 100% of an li element not the page.
Remove position fixed. Then add
.dropdown-menu {
width: 600px;
}
and
.dropdown-menu li {
display:block;
float: left;
}
Upvotes: 0
Reputation: 2982
So the problem is that width: 100%
means different things when position is fixed and when it is absolute. When it's fixed, it fills 100% of the window. When it's absolute, it fills 100% of its closest parent with relative positioning.
In this case, li.dropdown
is relatively positioned, so the dropdown fills that width. To fix that, add this line of CSS:
ul.nav li.dropdown {position: static;}
Now fix your dropdown menu CSS with this:
.dropdown-menu {
position: absolute;
top:50px;
}
Here is the fixed bootply: http://www.bootply.com/EuiK4NsMn5
Upvotes: 1