user13317 user13317 - 1 year ago 140
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">

<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<link rel="stylesheet" href="" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

<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>
<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>
<a href="#"><span class="glyphicon glyphicon-info-sign"></span> About</a>
<a href="#"><span class="glyphicon glyphicon-envelope"></span> Contact</a>
<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 class="form-group">
<input type="password" class="form-control input-sm" name="password" placeholder="Password">
<button type="submit" class="btn btn-primary btn-sm">Sign In</button>
<script src="" integrity="sha256-cCueBR6CsyA4/9szpPfrX3s49M9vUU5BgtiJj06wt/s=" crossorigin="anonymous"></script>
<script src="" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>


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

Answer Source

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;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download