如何在Bootstrap 3中居中一个内联表单

6

我正在尝试制作一个具有多个选项的搜索栏,并希望将其居中显示。

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>

<div id="search_panel">
  <div class='container'>
    <form class="form-inline" role="form">
      <!-- SEARCH PLACE -->
      <div class="form-group">
        <label class="sr-only" for="inputEmail">search</label>
        <input id="where" name="where" type="search" placeholder="where you want to visit ...." class="form-control input-md" required="">
      </div>
      <!-- CHECK-IN DATE -->
      <div class="form-group" id="start">
        <i class="fa fa-calendar fa-2x"></i>
        <input type='text' placeholder="Check-In Date" class="form-control" id='datepicker-start' />
      </div>
      <div class="form-group" id="end">
        <i class="fa fa-calendar fa-2x"></i>
        <input type='text' placeholder="Check-Out Date" class="form-control" id='datepicker-end' />
      </div>
      <!-- Button Drop Down -->
      <div class="form-group">
        <label class="sr-only" for="inputEmail">Select Basic</label>
        <select id="selectbasic" name="selectbasic" class="form-control">
          <option value="1">Option one</option>
          <option value="2">Option two</option>
        </select>
      </div>
    </form>
  </div>
</div>


很难确定你的问题是什么。你能否更详细地解释一下你想要实现什么? - idmean
5个回答

14
你也可以结合类别。
如果你改变定义从

开始。
<form class="form-inline" role="form">

要让 <form class="form-inline text-center" role="form"> 正常工作,

我希望在添加额外样式之前尽可能保留纯 Bootstrap 样式。

Bootstrap 自带本地对齐类,可以在此处找到:http://getbootstrap.com/css/#type-alignment

希望对你有所帮助。


尽可能保持原始的Bootstrap样式,然后再添加自定义样式,这正是框架/库的目的。对于简单性给予+1。 - cezar

7

我建议在表单中添加 text-align: center。请参考我的示例(请注意,要查看效果,您必须选择“完整页面”。否则,Bootstrap默认为移动视图)。

编辑:

哎呀!我想您不需要 inline-block。无论如何,我已将其设置为 inline-bock。:P

#search_panel {
    text-align: center; 
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>

<div id="search_panel">
  <div class='container'>
    <form class="form-inline" role="form">
      <!-- SEARCH PLACE -->
      <div class="form-group">
        <label class="sr-only" for="inputEmail">search</label>
        <input id="where" name="where" type="search" placeholder="where you want to visit ...." class="form-control input-md" required="">
      </div>
      <!-- CHECK-IN DATE -->
      <div class="form-group" id="start">
        <i class="fa fa-calendar fa-2x"></i>
        <input type='text' placeholder="Check-In Date" class="form-control" id='datepicker-start' />
      </div>
      <div class="form-group" id="end">
        <i class="fa fa-calendar fa-2x"></i>
        <input type='text' placeholder="Check-Out Date" class="form-control" id='datepicker-end' />
      </div>
      <!-- Button Drop Down -->
      <div class="form-group">
        <label class="sr-only" for="inputEmail">Select Basic</label>
        <select id="selectbasic" name="selectbasic" class="form-control">
          <option value="1">Option one</option>
          <option value="2">Option two</option>
        </select>
      </div>
    </form>
  </div>
</div>


它能够工作,但是当我给#search_panel添加position:absolute;时,它会浮动到左边。我的目的是将这个#social面板覆盖在另一个div上面。 - Muhammad Asim Khan

3

只需将.text-center类添加到包含该表单的行中即可。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
  <head>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<meta charset="utf-8">
<!-- meta http-equiv="X-UA-Compatible" content="IE=edge" -->
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">

<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap-theme.min.css" integrity="sha384-fLW2N01lMqjakBkx3l/M9EahuwpSfeNvV63J5ezn3uZzapT0u7EYsXMjQV+0En5r" crossorigin="anonymous">

<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
  <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
  <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
  </head>
  <body>
<div class="container-fluid" style="background-color: #EEEEEE;">
  <div class="row text-center">
    <div class="col-sm-12" id="formContainer">
      <form class="form-inline" id="SignToNewsletterForm" onsubmit="...">
        <input type="text" class="form-control input-lg" id="firstname" placeholder="Your first name...">
        <input type="email" class="form-control input-lg" id="email" placeholder="Your Email Address...">
        <button type="submit" class="btn btn-success btn-lg">I Want To Subscribe</button>
      </form>
    </div>
  </div>
</div>
  </body>
</html>


1

我不确定我是否理解了你的问题,但以下是一个表单居中的示例:

http://jsfiddle.net/Lfw3v0p6/

我只是添加了一些 CSS:

.form-inline {
    text-align: center;
}

如果这不是您所寻找的内容,请更新您的问题以提供更多细节或您已尝试过的示例。


0
#search_panel {
    text-align: center; 
    z-index:10;
    position:absolute;
    padding-left:5%;
    top:40%;
}

我通过添加左填充来解决问题,因为当位置是绝对时,所有填充都会消失


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接