Centering Bootstrap input fields

后端 未结 9 1230
执笔经年
执笔经年 2021-01-29 22:12

I am new with this, especially with Bootstrap. I have this code:

相关标签:
9条回答
  • 2021-01-29 22:51

    Try applying this style to your div class="input-group":

    text-align:center;
    

    View it: Fiddle.

    0 讨论(0)
  • 2021-01-29 22:54

    Ok, this is best solution for me. Bootstrap includes mobile-first fluid grid system that appropriately scales up to 12 columns as the device or viewport size increases. So this worked perfectly on every browser and device:

            <div class="row">
                <div class="col-lg-4"></div>
                <div class="col-lg-4">
                    <div class="input-group">
                        <input type="text" class="form-control" /> 
                        <span class="input-group-btn">
                            <button class="btn btn-default" type="button">Go!</button>
                        </span>
                    </div><!-- /input-group -->
                </div><!-- /.col-lg-4 -->
                <div class="col-lg-4"></div>
            </div><!-- /.row -->
    

    It means 4 + 4 + 4 =12... so second div will be in the middle that way.

    Example: http://jsfiddle.net/jpGwm/embedded/result/

    0 讨论(0)
  • 2021-01-29 22:55

    The best way for centering your element it is using .center-block helper class. But must your bootstrap version not less than 3.1.1

    <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha/css/bootstrap.css" rel="stylesheet" />
    <div class="row">
      <div class="col-lg-3 center-block">
        <div class="input-group">
          <input type="text" class="form-control">
          <span class="input-group-btn">
                 <button class="btn btn-default" type="button">Go!</button>
             </span>
        </div>
        <!-- /input-group -->
      </div>
      <!-- /.col-lg-6 -->
    </div>
    <!-- /.row -->

    0 讨论(0)
  • 2021-01-29 22:55

    Try to use this code:

    .col-lg-3 {
      width: 100%;
    }
    .input-group {
       width: 200px; // for exemple
       margin: 0 auto;
    }
    

    if it didn't work use !important

    0 讨论(0)
  • 2021-01-29 22:56

    For bootstrap 4 use offset-4, see below.

    <div class="mx-auto">        
            <form class="mx-auto" action="/campgrounds" method="POST">
                <div class="form-group row">                
                    <div class="col-sm-4 offset-4">
                        <input class="form-control" type="text" name="name" placeholder="name">
                    </div>
                </div>
                <div class="form-group row">               
                    <div class="col-sm-4 offset-4">
                        <input class="form-control" type="text" name="picture" placeholder="image url">
                    </div>
                </div>
                <div class="form-group row">
                        <div class="col-sm-4 offset-4">
                            <input class="form-control" type="text" name="description" placeholder="description">
                        </div>
                    </div>
                <button class="btn btn-primary" type="submit">Submit!</button>
                <a class="btn btn-secondary"  href="/campgrounds">Go Back</a>
            </form>      
        </div>
    
    0 讨论(0)
  • 2021-01-29 23:00

    Well in my case the following work fine

    <div class="card-body p-2">
      <div class="d-flex flex-row justify-content-center">
         <div style="margin: auto;">
            <input type="text" autocomplete="off"
              style="max-width:150px!important; "
              class="form-control form-control-sm font-weight-bold align-self-center w-25" id="dtTechState">
         </div>
      </div>
    </div>
    
    0 讨论(0)
提交回复
热议问题