Align the form to the center in Bootstrap 4

后端 未结 3 1309
走了就别回头了
走了就别回头了 2020-11-29 04:16

I am trying to align the form to the center and keep it responsive. I have tried several ways but no success. I am trying to center all the text and the form. I am using Boo

相关标签:
3条回答
  • 2020-11-29 04:26

    All above answers perfectly gives the solution to center the form using Bootstrap 4. However, if someone wants to use out of the box Bootstrap 4 css classes without help of any additional styles and also not wanting to use flex, we can do like this.

    A sample form

    HTML

    <div class="container-fluid h-100 bg-light text-dark">
      <div class="row justify-content-center align-items-center">
        <h1>Form</h1>    
      </div>
      <hr/>
      <div class="row justify-content-center align-items-center h-100">
        <div class="col col-sm-6 col-md-6 col-lg-4 col-xl-3">
          <form action="">
            <div class="form-group">
              <select class="form-control">
                        <option>Option 1</option>
                        <option>Option 2</option>
                      </select>
            </div>
            <div class="form-group">
              <input type="text" class="form-control" />
            </div>
            <div class="form-group text-center">
              <div class="form-check-inline">
                <label class="form-check-label">
        <input type="radio" class="form-check-input" name="optradio">Option 1
      </label>
              </div>
              <div class="form-check-inline">
                <label class="form-check-label">
        <input type="radio" class="form-check-input" name="optradio">Option 2
      </label>
              </div>
              <div class="form-check-inline">
                <label class="form-check-label">
        <input type="radio" class="form-check-input" name="optradio" disabled>Option 3
      </label>
              </div>
            </div>
            <div class="form-group">
              <div class="container">
                <div class="row">
                  <div class="col"><button class="col-6 btn btn-secondary btn-sm float-left">Reset</button></div>
                  <div class="col"><button class="col-6 btn btn-primary btn-sm float-right">Submit</button></div>
                </div>
              </div>
            </div>
    
          </form>
        </div>
      </div>
    </div>
    

    Link to CodePen

    https://codepen.io/anjanasilva/pen/WgLaGZ

    I hope this helps someone. Thank you.

    0 讨论(0)
  • 2020-11-29 04:27

    You need to use the various Bootstrap 4 centering methods...

    • Use text-center for inline elements.
    • Use justify-content-center for flexbox elements (ie; form-inline)

    https://www.codeply.com/go/Am5LvvjTxC

    Also, to offset the column, the col-sm-* must be contained within a .row, and the .row must be in a container...

    <section id="cover">
        <div id="cover-caption">
            <div id="container" class="container">
                <div class="row">
                    <div class="col-sm-10 offset-sm-1 text-center">
                        <h1 class="display-3">Welcome to Bootstrap 4</h1>
                        <div class="info-form">
                            <form action="" class="form-inline justify-content-center">
                                <div class="form-group">
                                    <label class="sr-only">Name</label>
                                    <input type="text" class="form-control" placeholder="Jane Doe">
                                </div>
                                <div class="form-group">
                                    <label class="sr-only">Email</label>
                                    <input type="text" class="form-control" placeholder="jane.doe@example.com">
                                </div>
                                <button type="submit" class="btn btn-success ">okay, go!</button>
                            </form>
                        </div>
                        <br>
    
                        <a href="#nav-main" class="btn btn-secondary-outline btn-sm" role="button">↓</a>
                    </div>
                </div>
            </div>
        </div>
    </section>
    
    0 讨论(0)
  • 2020-11-29 04:45
    <div class="d-flex justify-content-center align-items-center container ">
    
        <div class="row ">
    
    
            <form action="">
                <div class="form-group">
                    <label for="inputUserName" class="control-label">Enter UserName</label>
                    <input type="email" class="form-control" id="inputUserName" aria-labelledby="emailnotification">
                    <small id="emailnotification" class="form-text text-muted">Enter Valid Email Id</small>
                </div>
                <div class="form-group">
                    <label for="inputPassword" class="control-label">Enter Password</label>
                    <input type="password" class="form-control" id="inputPassword" aria-labelledby="passwordnotification">
    
                </div>
            </form>
    
        </div>
    
    </div>
    
    0 讨论(0)
提交回复
热议问题