css - Bootstrap - 在导航栏中将表单与表单控件input-sm垂直对齐

时间:2016-09-09 13:38:48

标签: html css twitter-bootstrap-3

我似乎无法将表单与放置在导航栏中的“input-sm”垂直对齐。如果表单的输入与导航栏的高度不同,则看起来它们是顶部对齐的。我希望有一种方法可以做到这一点,而无需手动调整填充或边缘顶部。有什么想法吗?

<!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 它看起来并不太糟糕,我只想弄明白该怎么做!

1 个答案:

答案 0 :(得分:3)

最简单的方法是调整边距,如上所述。这就是Bootstrap如何做到的。

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

第二个最简单的方法是使用flexbox,如果你能够这样做的话。

.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;
}