twitter-bootstrap 如何在引导程序中以表单为中心输入?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43939000/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 00:50:33  来源:igfitidea点击:

How center input in form in bootstrap?

htmlcsstwitter-bootstrapinputcenter

提问by wenus

I want to have two inputs ( login and password ). One under one, each about col-sm-6. I can't center this inputs without col-sm-offset-3. With offset It really works, but I want to center this inputs on every screens. Maybe I shoul add somewhere .center > Or margin auto? But where...

我想要两个输入(登录名和密码)。一低于一,每个约 col-sm-6。如果没有 col-sm-offset-3,我无法将这些输入居中。使用偏移它确实有效,但我想将此输入集中在每个屏幕上。也许我应该在某处添加 .center > 或 margin auto?但是哪里...

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

<div class="container">

  <div class="row">
    <div class="box">
      <div class="col-lg-12">
        <hr>
        <h4 class="text-center">Logowanie</h4>
        <hr>
        <form method="post" role="form" class="form-horizontal">
          {{ csrf_field() }}
          <div class="row">
            <div class="form-group">
              <div class="col-sm-6">
                <input type="email" class="form-control" name="email" placeholder="email">
              </div>
            </div>
            <div class="form-group">
              <div class="col-sm-6">
                <input type="email" class="form-control" name="email" placeholder="email">
              </div>
            </div>
          </div>
        </form>
      </div>
    </div>
  </div>
</div>

采纳答案by Zim

The use the offset on all screens by using the xstier..

通过使用xs层在所有屏幕上使用偏移量..

http://www.bootply.com/P31H4uF1QG

http://www.bootply.com/P31H4uF1QG

         <form method="post" role="form" class="form-horizontal">
                    {{ csrf_field() }}
                  <div class="form-group">
                    <div class="col-xs-6 col-xs-offset-3">
                      <input type="email" class="form-control" name="email">
                    </div>
                  </div>
                  <div class="form-group">
                    <div class="col-xs-6 col-xs-offset-3">
                      <input type="email" class="form-control" name="email" placeholder="email">
                    </div>
                  </div>
         </form>

Also, don't use both rowand form-group, since the form-groupworks like a rowto contain col-*

另外,不要同时使用rowand form-group,因为它的form-group作用类似于row包含col-*

回答by Dalin Huang

If you don't want to use offsetyou can use a centered class I call it .centeredand this as a wrapper to the content you wanted to center.

如果您不想使用,offset您可以使用我称之为居中的类,.centered并将其作为您想要居中的内容的包装器。

See the example below:

请参阅以下示例:

.centered {
  display: flex;
  align-items: center;
  justify-content: center;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<div class="container">

  <div class="row">
    <div class="box">
      <div class="col-lg-12">
        <hr>
        <h4 class="text-center">Logowanie</h4>
        <hr>
        <form method="post" role="form" class="form-horizontal">
          {{ csrf_field() }}
            <div class="form-group centered">
              <div class="col-sm-6">
                <input type="email" class="form-control" name="email" placeholder="email">
              </div>
            </div>
            <div class="form-group centered">
              <div class="col-sm-6">
                <input type="email" class="form-control" name="email" placeholder="email">
              </div>
            </div>
        </form>
      </div>
    </div>
  </div>
</div>

回答by Manish Patel

Use the offset with small for all screen and wrap form in this div

对所有屏幕使用小偏移量并在此 div 中包装表单

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
    <div class="row">
        <div class="box">
            <div class="col-lg-12">
                <hr>
                <h4 class="text-center">Logowanie</h4>
                <hr>
                <div class="row">
                  <div class="col-sm-offset-3 col-sm-6">
                    <form method="post" role="form" class="form-horizontal">
                        {{ csrf_field() }}
                      <div class="form-group">
                        <div class="col-sm-12">
                          <input type="email" class="form-control" name="email" placeholder="email">
                        </div>
                      </div>
                      <div class="form-group">
                        <div class="col-sm-12">
                          <input type="email" class="form-control" name="email" placeholder="email">
                        </div>
                      </div>                        
                    </form>
                  </div>
                </div>
            </div>
        </div>
    </div>
</div> 

回答by Andriy Sushchyk

Inputs have width: 100%by default in Bootstrap. So, you need to override this style property(for example set width:50%) and add center or text-center class to parent element, and it is always will be centered.

width: 100%默认情况下,Bootstrap 中有输入。因此,您需要覆盖此样式属性(例如 set width:50%)并将 center 或 text-center 类添加到父元素,并且它始终会居中。

But I highly recommend you to use column:

但我强烈建议您使用列:

 <div class="col-xs-offset-3 col-xs-6 col-md-offset-4 col-md-4">
 <input type="email" class="form-control" name="email" placeholder="email">
 </div>