Ardzii Ardzii -4 years ago 134
CSS Question

bootstrap nav-bar-right floating left

I tried to look out for an answer for my problem but I can't seem to find one. If by any chance there actually is a solution out there, then, I'm sorry, my bad!

I seem to be encountering a problem with Bootstrap's default nav bar: The dropdown menu:

.navbar-right
is floating left for some reason. You can check it out here.
I'm working on rails 5 and the code for my nav currently looks like:

<nav class="navbar navbar-default">
<div class="container-fluid">
<!-- Brand and toggle get grouped for better mobile display -->
<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>
<%= link_to "Prodemex", root_path, class: "navbar-brand", id: "logo" %>
</div>

<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li><%= link_to "Articulos", articles_path %></li>
<li><%= link_to "Usuarios", users_path %></li>
<% if logged_in? %>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Actions <span class="caret"></span></a>
<ul class="dropdown-menu">
<li><%= link_to "Nuevo articulo", new_article_path %></li>
<li role="separator" class="divider"></li>
<li><a href="#">One more separated link</a></li>
</ul>
</li>
</ul>
<% end %>
<form class="navbar-form navbar-left">
<div class="form-group">
<input type="text" class="form-control" placeholder="Search">
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
<% if logged_in? %>
<ul class="nav navbar-nav navbar-right">
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="true">Mi cuenta<%= " - Admin " if current_user.admin? %><span class="caret"></span></a>
<ul class="dropdown-menu">
<li><%= link_to "Modificar mis datos", edit_user_path(current_user) %></li>
<li><%= link_to "Ver mi perfil", user_path(current_user) %></li>
<li><a href="#">Something else here</a></li>
<li role="separator" class="divider"></li>
<li><%= link_to 'Cerrar sesión', logout_path, method: :delete %></li>
</ul>
</li>
</ul>
<% else %>
<ul class="navbar-right nav navbar-nav">
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="true">Mi cuenta <span class="caret"></span></a>
<ul class="dropdown-menu">
<li><%= link_to 'Iniciar sesión', login_path %></li>
<li><%= link_to 'Registrarse', signup_path %></li>
</ul>
</li>
</ul>
<% end %>
</div><!-- /.navbar-collapse -->
</div><!-- /.container-fluid -->
</nav>


You can retrieve more of the code on github
I have a custom CSS with all that and already tried to delete all portions related with navbar but it did nothing...

$navbar-default-bg: #3498DB;

@import "bootstrap-sprockets";
@import "bootstrap";

.gen {
padding: 0 2%;
}

.col-xs-8 {
float: none;
}

a {
text-decoration: none;
}

body {
font-weight: 200;
}

#logo {
float: left;
font-size: 1.7em;
color: #fff;
text-transform: uppercase;
font-weight: 200;
}

#logo:hover {
text-decoration: none;
color: #5e82a6;
}

.navbar {
border-radius: 0;
margin: 0;
}

.center {
text-align: center;
}

#home {
background: linear-gradient(rgba(0, 0, 0, 0.35), rgba(0, 0, 0, 0.35)), image-url('cover_page.jpg');
background-size: cover;
background-attachment: fixed;
height: 100vh;
}

.jumbotron {
padding: 15.5% 20%;
background-color: transparent;

}

.jumbotron h1,h3 {
color: #fff;
font-weight: 200;
margin-bottom: 10px;
}

.btn-xlg {
font-size: 1.2em;
font-weight: 200;
background-color: #3498DB;
border: 1px solid #3498DB;
margin: 0;
display: inline-block;
letter-spacing: 1px;
}

.btn-xlge {
font-size: 1.2em;
font-weight: 200;
color: #000;
background-color: none;
border: 1px solid #000;
margin-left: 10px;
display: inline-block;
}

.btn-xlge:hover,
active {
background-color: #337ab7;
color: #fff;
border: 1px solid #337ab7;
}

.footer {
background-color: #3498DB;
color: #fff;
letter-spacing: 1px;
padding: 10px;
}

footer ul {
list-style:none;
margin-top: 10px;
}

.footer a {
color: #fff;
}

.footer a:hover,
.footer a:active {
color: #ccc;
text-decoration: none;
}

.article-actions {
border-top: 1px solid #eaeaea;
padding-top: 15px;
margin-top: 10px;
}

.descrition {
margin-top: 0;
}

.article-title {
font-size: 1.5em;
}

.article-title a {
text-decoration: none;
}

.article-body {
border-top: 1px solid #eaeaea;
padding-top: 15px;
padding-bottom: 15px;
}

.article-meta-details {
margin-top: 5px;
margin-bottom: 0;
font-size: 1em;
}

.article-meta-details a {
text-decoration:none;
}

.well-lg {
padding-bottom: 10px;
}

.listing {
list-style: none;
padding-left: 0;
}

.insideuser {
padding-left: 50px;
float: left;
}

.author {
font-weight: 400;
color: #3498DB;
}

.authorpage {
margin-top: 10px;
padding: 10px 0;
}

.pop {
margin-top: 15px;
-webkit-box-shadow: 4px 1px 10px 0px rgba(50, 50, 50, 0.27);
-moz-box-shadow: 4px 1px 10px 0px rgba(50, 50, 50, 0.27);
box-shadow: 4px 1px 10px 0px rgba(50, 50, 50, 0.27);
}


And the weird thing is that when I log in, the dropdown goes back to the right where it is supposed to be...
When inspecting the code, I almost found a fix, deleting the left float to this portion:

@media (min-width: 768px)
custom.self-839…ss?body=1:5864
.navbar-nav {
float: left;
margin: 0;
}


But obviously when I do that it messes up with the rest of the navbar.
Any body can help me? Thanks in advance!!

Answer Source

First off! Thanks guys for your help! @Gal, your answer actually put me in the right direction and I finally found where my mistake was! (Yes, the errors is always located between the keyboard and the chair! hahhahaha!)

OK so by implementing Gal's code into my nav. I found out that the problem was coming from the fact that the problematic dropdown was considered as included in the:

<ul class="nav navbar-nav">

And that's when I figure that the UL wasn't closing properly! This portion was the problematic one:

<ul class="nav navbar-nav">
        <li><%= link_to "Articulos", articles_path %></li>
        <li><%= link_to "Usuarios", users_path %></li>
        <% if logged_in? %>
          <li class="dropdown">
            <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Actions <span class="caret"></span></a>
            <ul class="dropdown-menu">
              <li><%= link_to "Nuevo articulo", new_article_path %></li>
              <li role="separator" class="divider"></li>
              <li><a href="#">One more separated link</a></li>
            </ul>
          </li>
        </ul>
      <% end %>

I just switch the </ul> tag with the <% end %> one like this:

<ul class="nav navbar-nav">
        <li><%= link_to "Articulos", articles_path %></li>
        <li><%= link_to "Usuarios", users_path %></li>
        <% if logged_in? %>
          <li class="dropdown">
            <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Actions <span class="caret"></span></a>
            <ul class="dropdown-menu">
              <li><%= link_to "Nuevo articulo", new_article_path %></li>
              <li role="separator" class="divider"></li>
              <li><a href="#">One more separated link</a></li>
            </ul>
          </li>
        </ul>
      <% end %>

and it works like a charm!

Thanks again everyone!!!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download