Reputation: 2563
Codepen -> https://codepen.io/SubSul/pen/LgWzVw.
I have modified the bootstrap 4 navbar component to include a fixed button dropdown for login on the top right corner. I'm now trying to align the nav-items to the stick against the login button, but justify-content: flex-end
does not seems be pushing this to the right.
I tried adding margin-left to the nav-items, to no avail.
On the desktop I'm trying to achieve this -
And on the mobile, I'm trying to get the logo center aligned and login button stick to the right. I'm able to use margin-right
to move the logo to the center, but the transition on click of hamburger shifts the logo a little to the left, how do I leave it fixed to the center -
<html>
<head>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css"
integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
</head>
<body>
<div class="main">
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarText" aria-controls="navbarText" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<a class="navbar-brand" href="#">Navbar Logo</a>
<div class="collapse navbar-collapse justify-content-end" id="navbarText">
<ul class="navbar-nav">
<li class="nav-item active">
<a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Features</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Pricing</a>
</li>
</ul>
</div>
</nav>
<div class="login-widget">
<button class="btn btn-secondary btn-lg dropdown-toggle" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Log In
</button>
<div class="dropdown-menu dropdown-menu-right">
<form class="px-4 py-3">
<div class="form-group">
<label for="exampleDropdownFormEmail1">Email address</label>
<input type="email" class="form-control" id="exampleDropdownFormEmail1" placeholder="[email protected]">
</div>
<div class="form-group">
<label for="exampleDropdownFormPassword1">Password</label>
<input type="password" class="form-control" id="exampleDropdownFormPassword1" placeholder="Password">
</div>
<div class="form-check">
<input type="checkbox" class="form-check-input" id="dropdownCheck">
<label class="form-check-label" for="dropdownCheck">
Remember me
</label>
</div>
<button type="submit" class="btn btn-primary">Sign in</button>
</form>
<div class="dropdown-divider"></div>
<a class="dropdown-item" href="#">New around here? Sign up</a>
<a class="dropdown-item" href="#">Forgot password?</a>
</div>
</div>
</div>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"
integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo"
crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"
integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49"
crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"
integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy"
crossorigin="anonymous"></script>
</body>
</html>
Styles
.main {
display: flex;
justify-content: space-between;
}
.dropdown-menu {
margin-top: 15px;
}
.navbar-collapse {
/* margin-left: 100%; */
}
.login-widget {
/* margin-left: 320px; */
}
Upvotes: 0
Views: 2453
Reputation: 797
I came up with a solution for this, it's not the cleanest I've made but maybe it will work for you, I didn't use flexbox to do it so you can delete the .main css, first off all you need to move the .login-widget div inside the nav tag, doing that it's gonna look as you want for desktop.
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarText" aria-controls="navbarText" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<a class="navbar-brand" href="#">Navbar Logo</a>
<div class="collapse navbar-collapse justify-content-end" id="navbarText">
<ul class="navbar-nav">
<li class="nav-item active">
<a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Features</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Pricing</a>
</li>
</ul>
</div>
<div class="login-widget">
<button class="btn btn-secondary btn-lg dropdown-toggle" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Log In
</button>
<div class="dropdown-menu dropdown-menu-right">
<form class="px-4 py-3">
<div class="form-group">
<label for="exampleDropdownFormEmail1">Email address</label>
<input type="email" class="form-control" id="exampleDropdownFormEmail1" placeholder="[email protected]">
</div>
<div class="form-group">
<label for="exampleDropdownFormPassword1">Password</label>
<input type="password" class="form-control" id="exampleDropdownFormPassword1" placeholder="Password">
</div>
<div class="form-check">
<input type="checkbox" class="form-check-input" id="dropdownCheck">
<label class="form-check-label" for="dropdownCheck">
Remember me
</label>
</div>
<button type="submit" class="btn btn-primary">Sign in</button>
</form>
<div class="dropdown-divider"></div>
<a class="dropdown-item" href="#">New around here? Sign up</a>
<a class="dropdown-item" href="#">Forgot password?</a>
</div>
</div>
</nav>
But we will have a problem on mobile devices, when you click on the hamburger menu the .login-widget div it's going to align with the other nav items of the menu, breaking your layout, so what I do to fix this, it's giving to .login-widget a position absolute
.login-widget{
position: absolute;
right: 16px;
top: 4px;
}
that will do the trick for .login-widget, but now we have another problem .navbar-brand isn't centered, so what I do to fix it's using position absolute again
.navbar-brand{
position: absolute;
top: 8px;
right: calc(109px + 16px);
left: calc(56px + 16px);
text-align: center;
}
and for the last, you have to wrap this classes in a media query to reset the values for desktop.
@media only screen and (min-width: 992px){
.login-widget{
position: static;
right: auto;
top: auto;
}
.navbar-brand{
position: static;
top: auto;
right: auto;
left: auto;
text-align: left;
}
}
Here you have a codepen if you wanna test it, let me know if that's the solution you are looking for!
Upvotes: 2