user13317 user13317 - 2 months ago 10
CSS Question

css - Bootstrap - Vertically align form with form-control input-sm in navbar

I can't seem to be able to vertically align a form with "input-sm" that is placed within the navbar. If the inputs to the form are not the same height as the navbar, it appears that they get top aligned. I was hoping there might be a way to do this without manually adjusting the padding or margin-top. Any ideas?



<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="utf-8">
<title>Example</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
</head>

<body>
<header>
<div class="navbar navbar-default navbar-fixed-top" role="navigation">
<div class="container">
<div class="navbar-header">
<a class="navbar-brand" href="#">Brand</a>
<button class="navbar-toggle" type="button" data-toggle="collapse" data-target="#navbar-main">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
</div>
<div class="navbar-collapse collapse" id="navbar-main">
<ul class="nav navbar-nav">
<li class="active">
<a href="#"><span class="glyphicon glyphicon-home"></span> Home</a>
</li>
<li>
<a href="#"><span class="glyphicon glyphicon-info-sign"></span> About</a>
</li>
<li>
<a href="#"><span class="glyphicon glyphicon-envelope"></span> Contact</a>
</li>
</ul>
<form class="navbar-form navbar-right" role="search">
<div class="form-group">
<input type="email" class="form-control input-sm" name="email" placeholder="Email">
</div>
<div class="form-group">
<input type="password" class="form-control input-sm" name="password" placeholder="Password">
</div>
<button type="submit" class="btn btn-primary btn-sm">Sign In</button>
</form>
</div>
</div>
</div>
</header>
<script src="https://code.jquery.com/jquery-3.1.0.min.js" integrity="sha256-cCueBR6CsyA4/9szpPfrX3s49M9vUU5BgtiJj06wt/s=" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
</body>


</html>





enter image description here
It doesn't look too bad as is, I just want to figure out how to do it!

Answer

The simplest thing to do is adjust the margins as you mentioned. That's how Bootstrap does it.

.navbar-form {
    margin-top: 10px;
    margin-bottom: 10px;
}

The second simplest would be to use flexbox if you're able to.

.navbar-form {
    display: flex;
    align-items: center;
}
/* Items will butt up next to each other using flexbox so the inline
spacing that existed before will be gone. So we replicate it. */
.navbar-form > .form-group {
    margin-right: 0.25rem;
}
Comments