Align The Form To The Center In Bootstrap 4


Answer :

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://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> 

<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> 

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

enter image description here

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.


Comments

Popular posts from this blog

Converting A String To Int In Groovy

"Cannot Create Cache Directory /home//.composer/cache/repo/https---packagist.org/, Or Directory Is Not Writable. Proceeding Without Cache"

Android How Can I Convert A String To A Editable