Align the form to the center in Bootstrap 4 [duplicate] - html

This question already has answers here:
Bootstrap Center Vertical and Horizontal Alignment
(17 answers)
Closed 2 years ago.
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 Bootstrap v4. I am not sure if that helps.
HTML:
<section id="cover">
<div id="cover-caption">
<div id="container">
<div class="col-sm-10 col-sm offset-1">
<h1 class="display-3">Welcome to Bootstrap 4</h1>
<div class="info-form">
<form action="" class="form-inline">
<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>
↓
</div>
</div>
</div>
</section>
CSS:
html,
body{
height: 100%;
}
#cover {
background: #222 url('../img/stars.jpg') center center no-repeat;
background-size: cover;
color: white;
height: 100%;
text-align: center;
display: flex;
align-items: center;
}
#cover-caption {
width: 100%;
}

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>
↓
</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
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.

Related

Bootstrap 4 - can't horizontally align a vertical form

I have the following form code:
<div id="site" class="container-fluid">
<div class="row">
<div class="my-4 offset-3 col-6 justify-content-center align-items-center">
<form action="/some/path" method="post">
<div class="form-group row">
<label for="username" class="col-2 col-form-label">Username</label>
<div class="col-4">
<input type="text" class="form-control" id="username" name="_username" required="required" autocomplete="username" />
</div>
</div>
<div class="form-group row">
<label for="password" class="col-2 col-form-label">Password</label>
<div class="col-4">
<input type="password" class="form-control" id="password" name="_password" required="required" autocomplete="current-password" />
</div>
</div>
<div class="form-group row">
<div class="col-2"></div>
<div class="col-4">
<div class="form-check">
<input class="form-check-input" type="checkbox" id="remember_me" name="_remember_me" value="on" />
<label for="remember_me" class="form-check-label">Remember me</label>
</div>
</div>
</div>
<div class="form-group row">
<div class="col-6">
<button class="btn btn-burnt-orange" type="submit" id="_submit" name="_submit">Log in</button>
</div>
</div>
</form>
</div>
</div>
</div>
I want the form to be centered on the page, but instead, it's still further to the left than center as shown by this screenshot:
I thought my math was correct: Bootstrap uses a 12-column grid, so by using a 3-column offset, and a 6-column content area, I'd have essentially two 3-column gutters on each side. And by specifying the form rows to add up to 6-columns (2-column labels and 4-column inputs), the form would fill the 6-column content area, thereby automatically centering itself. But I'm obviously wrong.
So, what can I do to actually center this thing? And to make it 6 columns wide (because, judging by the screenshot, it isn't)?
The form is horizontally centered, but it's only half the width of it's parent. The issue is...
"And by specifying the form rows to add up to 6-columns (2-column
labels and 4-column inputs), the form would fill the 6-column content
area, thereby automatically centering itself"
Since 6 is half of 12, the form col-6 (2+4) is only going to fill half the parent col-6. If you want a narrower form in the middle use col-4 (1/3 width), and then something that adds to 12 for the labels and form input (eg. 3+9). OFC you could also use 4/8, 6/6, etc...
<div class="container-fluid">
<div class="row">
<div class="my-4 offset-4 col-4 justify-content-center align-items-center">
<form action="/some/path" method="post">
<div class="form-group row">
<label for="username" class="col-3 col-form-label">Username</label>
<div class="col-9">
<input type="text" class="form-control" id="username" name="_username" required="required" autocomplete="username">
</div>
</div>
<div class="form-group row">
<label for="password" class="col-3 col-form-label">Password</label>
<div class="col-9">
<input type="password" class="form-control" id="password" name="_password" required="required" autocomplete="current-password">
</div>
</div>
<div class="form-group row">
<div class="col-3"></div>
<div class="col-9">
<div class="form-check">
<input class="form-check-input" type="checkbox" id="remember_me" name="_remember_me" value="on">
<label for="remember_me" class="form-check-label">Remember me</label>
</div>
</div>
</div>
<div class="form-group row">
<div class="col-12">
<button class="btn btn-burnt-orange" type="submit" id="_submit" name="_submit">Log in</button>
</div>
</div>
</form>
</div>
</div>
</div>
https://www.codeply.com/go/b4FE5qflxS
Related: Center the content inside a column in Bootstrap 4
You can accomplish this by using col-6 and justify-content-center. All you have to do is setup the elements, rows and columns with bootstrap.
Here is an example of centering the form group:
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="site" class="container-fluid">
<div class="row justify-content-center">
<div class="col-6" style="background: #eee;">
<!-- formgroup start -->
<form>
<div action="/some/path" class="form-group">
<label for="username">Username</label>
<input type="text" class="form-control" id="username" placeholder="Enter username" name="_username" required="required" autocomplete="username" />
</div>
<div class="form-group">
<label for="password">Password</label>
<input type="password" class="form-control" id="password" placeholder="Password" name="_password" required="required" autocomplete="current-password" />
</div>
<div class="form-group form-check">
<input type="checkbox" class="form-check-input" id="remember_me" name="_remember_me" value="on" />
<label class="form-check-label" for="remember_me">Remember me</label>
</div>
<button type="submit" class="btn btn-primary" id="_submit" name="_submit">Submit</button>
</form>
<!-- formgroup end -->
</div>
</div>
</div>
Reference these pages on the Bootstrap 4 documentation
https://getbootstrap.com/docs/4.1/layout/grid/
https://getbootstrap.com/docs/4.1/components/forms/

Align text and input so that they are neatly inline vertically

A bit of a beginner's question, I am trying to make my form to look like the following image where the text on the left are vertically aligned and the input field has the same widths.
twitter form
<div class="container">
<div class="row">
<div class="col-md-6">
<label class="d-inline">Full Name: </label>
<input type="text" placeholder="First Name" formControlName="firstName">
</div>
<div class="col-md-6">
<h6>Some text</h6>
</div>
</div>
<br>
<div class="row">
<div class="col-md-6">
<label class="d-inline">Email: </label>
<input type="email" placeholder="Email" formControlName="email">
</div>
<div class="col-md-6">
<h6>Some text</h6>
</div>
</div>
</div>
Which gives me the following result. I also want the input to stretch to the remaining parent container so that it stops before the "some text".
my attempt
I have tried to set the width css property of the input field to 100% hoping that it will do it. However, that just stretch it all the way which forces it to go to the next line.
I am using bootstrap 4 with Angular2.
Just give your p tag a min-width. I gave the input a max-width so it's better when your screen size is smaller. Hope this answers your question
p.d-inline {
min-width: 75px;
display: inline-block;
text-align: right;
margin-left: 30px;
}
h6 {
margin-left: 30px;
}
input {
max-width: 100px;
}
You said you are using Bootstrap.
Why not Use Bootstrap Form then?
You can find how-to HERE
Open the following snippet in Full Page to see how it works
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<form class="form-horizontal">
<div class="form-group">
<label for="inputEmail3" class="col-lg-1 control-label">Email</label>
<div class="col-lg-2">
<input type="email" class="form-control" id="inputEmail3" placeholder="Email">
</div>
</div>
<div class="form-group">
<label for="inputPassword3" class="col-lg-1 control-label">Password</label>
<div class="col-lg-2">
<input type="password" class="form-control" id="inputPassword3" placeholder="Password">
</div>
</div>
<div class="form-group">
<div class="col-lg-offset-1 col-lg-2">
<div class="checkbox">
<label>
<input type="checkbox"> Remember me
</label>
</div>
</div>
</div>
<div class="form-group">
<div class="col-lg-offset-1 col-lg-3">
<button type="submit" class="btn btn-default">Sign in</button>
</div>
</div>
</form>
As form-horizontal is not working as expected in bootstrap 4 so, you can try like this.
<div class="container">
<div class="row">
<div class="col-md-6 form-group row">
<label class="d-inline control-label col-md-3 text-right">Full Name: </label>
<div class="col-md-9">
<input class="form-control" type="text" placeholder="First Name" formControlName="firstName">
</div>
</div>
<div class="col-md-6">
<h6>Some text</h6>
</div>
</div>
<br>
<div class="row">
<div class="col-md-6 form-group row">
<label class="d-inline control-label col-md-3 text-right">Email: </label>
<div class="col-md-9">
<input class="form-control" type="email" placeholder="Email" formControlName="email">
</div>
</div>
<div class="col-md-6">
<h6>Some text</h6>
</div>
</div>
</div>

How to add spacing between the words in a <p> tag and align them with text boxes in the next div

The following is the code for the form which I wrote:
<div class="container-fluid" id="unlabelled">
<p class="bg-primary">Items<span>Quantity</span><span>In Kit</span></p>
<form>
<div class="form-group col-lg-5">
<input type="text" class="form-control" id="unlabelled-items" placeholder="Enter code or description">
</div>
<div class="form-group col-md-2">
<input type="text" class="form-control" id="quantity" placeholder="Enter Quantity">
</div>
<div class="form-group">
<input type="checkbox" id="in-kit">
<button class="btn-primary" id="add-btn">Add</button>
</div>
</form>
</div>
I am using bootstrap here. And I have the top paragraph heading with a class="bg-primary" which gives a nice background for the heading. I have two text input fields, a checkbox and a button. I want to align the text in the above paragraph with these fields.
https://imgur.com/XinA1kT
I want the Items text to be aligned in center with the first text field, Quantity with the second text field and the In kit with the check box.
As you can see in my code, I added span tags around the text. I experimented with it by adding margin properties to these span tags, but it didn't work properly. I tried a bunch of it works okay but I atleast need 15 to 20 of them which even didn't solve my problem.
Is there any alternative for this? I could even try other alternatives for the p tag too.
Thank you.
This can at least get you started, it's just rows and columns and some minor CSS. The problem you'll have is on a mobile device, if you want this to remain completely horizontal it'll start to break down.
.well-md.well-head {
background: #3973a6;
border: none;
color: #fff;
font-size: 18px;
height: 60px;
text-align: center;
}
.checkbox {
text-align: center;
}
<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.5/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" />
<div class="container">
<form>
<div class="row well well-md well-head">
<div class="col-xs-4">
<p>Items</p>
</div>
<div class="col-xs-3">
<p>Quantity</p>
</div>
<div class="col-xs-3">
<p>In Kit</p>
</div>
</div>
<div class="row">
<div class="form-group col-xs-4">
<input type="text" class="form-control" id="unlabelled-items" name="unlabelled-items" placeholder="Enter code or description">
</div>
<div class="form-group col-xs-3">
<input type="text" class="form-control" id="quantity" name="quantity" placeholder="Enter Quantity">
</div>
<div class="form-group">
<div class="form-group col-xs-3">
<div class="form-group">
<div class="checkbox">
<input type="checkbox" id="in-kit" name="in-kit">
</div>
</div>
</div>
<div class="form-group col-xs-2">
<button class="btn btn-primary" id="add-btn">Add</button>
</div>
</div>
</div>
</form>
</div>
You need to put your text descriptors into columns that match with the columns for you inputs. Like this:
<div class="container-fluid" id="unlabelled">
<p class="bg-primary">
<div class="row">
<div class="form-group col-md-5"> Items</div>
<div class="form-group col-md-2">Quantity</div>
<div class="form-group col-md-2">In Kit</div>
</div>
</p>
<form>
<div class="form-group col-md-5">
<input type="text" class="form-control" id="unlabelled-items" placeholder="Enter code or description">
</div>
<div class="form-group col-md-2">
<input type="text" class="form-control" id="quantity" placeholder="Enter Quantity">
</div>
<div class="form-group">
<div class="form-group col-md-2">
<input type="checkbox" id="in-kit">
</div>
<div class="form-group col-md-2">
<button class="btn-primary" id="add-btn">Add</button>
</div>
</div>
</form>
</div>

How to align the form horizontally in the middle of the div in bootstrap

<div class="container">
<div class="row clearfix">
<div class="col-md-12 column">
<form role="form">
<div class="form-group">
<label for="exampleInputEmail1">Email address</label><input type="email" class="form-control" id="exampleInputEmail1" />
</div>
<div class="form-group">
<label for="exampleInputPassword1">Password</label><input type="password" class="form-control" id="exampleInputPassword1" />
</div>
<div class="form-group">
<label for="exampleInputFile">File input</label><input type="file" id="exampleInputFile" />
<p class="help-block">
Example block-level help text here.
</p>
</div>
<div class="checkbox">
<label><input type="checkbox" /> Check me out</label>
</div> <button type="submit" class="btn btn-default">Submit</button>
</form>
</div>
</div>
</div>
I am using bootstrap.
And I want to align the form in the middle of this div col-md-12 column.
Any solutions for this?
Demo http://bootply.com/103569
Could use an offset like:
<div class="container">
<div class="row clearfix">
<div class="col-md-6 col-md-offset-3 column">
<form role="form">
<div class="form-group">
<label for="exampleInputEmail1">Email address</label><input type="email" class="form-control" id="exampleInputEmail1" />
</div>
<div class="form-group">
<label for="exampleInputPassword1">Password</label><input type="password" class="form-control" id="exampleInputPassword1" />
</div>
<div class="form-group">
<label for="exampleInputFile">File input</label><input type="file" id="exampleInputFile" />
<p class="help-block">
Example block-level help text here.
</p>
</div>
<div class="checkbox">
<label><input type="checkbox" /> Check me out</label>
</div> <button type="submit" class="btn btn-default">Submit</button>
</form>
</div>
</div>
<div class="row clearfix">
<div class="col-md-12 column">
</div>
</div>
</div>
or
<div class="container">
<div class="row clearfix">
<div class="col-md-12 column">
<div class="col-md-6 col-md-offset-3 column">
<form role="form">
<div class="form-group">
<label for="exampleInputEmail1">Email address</label><input type="email" class="form-control" id="exampleInputEmail1" />
</div>
<div class="form-group">
<label for="exampleInputPassword1">Password</label><input type="password" class="form-control" id="exampleInputPassword1" />
</div>
<div class="form-group">
<label for="exampleInputFile">File input</label><input type="file" id="exampleInputFile" />
<p class="help-block">
Example block-level help text here.
</p>
</div>
<div class="checkbox">
<label><input type="checkbox" /> Check me out</label>
</div> <button type="submit" class="btn btn-default">Submit</button>
</form>
</div>
</div>
</div>
</div>
http://getbootstrap.com/css/#grid-offsetting
You can have two solutions here with the property display.
One with inline-block:
.col-md-12 column {
text-align:center;
}
.col-md-12 column form {
display:inline-block;
}
Two with table :
.col-md-12 column form {
display:table;
margin:auto;
}
If you have a fixed width for the form you only need to add the margin:auto
Give your form a class
<form role="form" class="myForm">
and add the following style to it
.myForm
{
width: 480px;
margin-left: auto;
margin-right: auto;
}
http://bootply.com/103575
I would try adding
class="center"
to
<form role="form">
ending up with
<form role="form" class="center">
and creating the style declaration:
.center{margin:0px auto;}
keeping in mind that .center must have a set width that is less than the width of it's container.
Hope that helps.

Cannot move check box to align with my bootstrap textbox

JSFiddle for code, then screenshot of what I see on my screen, since JSFiddle is displaying it wrong :
(entire code in link, brief view below)
http://jsfiddle.net/WeDLR/1/
<div class="col-lg-3">
<form method =post action="/UnitInfo/TSMServlet" NAME="form01">
<div class="form-group">
<label for="terminalname" class="col-lg-3 control-label">Terminal Name</label>
<div class="col-lg-3">
<input type="checkbox" class="checkbox" display="inline-block" name="checkbox_tname" onclick="checkbox_tname_click();">
<input type="text" class="form-control" name="tname" placeholder="Terminal Name">
</div>
</div>
</div>
Currently, my check box is partially under my textbox. I want to make it so the center of the check box is on the same line as the center of the text box. How can I do this?
PS. If I don't use a bootstrap textbox, it seems like my alignment is just fine.
Try following css...
.checkbox {
width: 100%;
}
.form-group {
white-space: nowrap;
display:inline
}
.form-control {
display: inline-block;
}
Demo Fiddle
Try this, if its ok I will explain. I did not change any css. See Fiddle.
<nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
<ul class="nav navbar-nav">
<li class="active">Home</li>
</ul>
</nav>
<br>
<br>
<br>
<br>
<font class="header">
<center>
<b>Unit Information Filter </b>
</center>
</font><br>
<br>
<br>
<div class="container">
<div class="col-lg-3">
<form method="post" action="/UnitInfo/TSMServlet" NAME="form01">
<div class="form-group">
<label for="terminalname" class="col-xs-12 col-sm-12 col-lg-12 control-label">Terminal Name</label>
<div class="col-xs-2 col-sm-1 col-lg-2">
<input type="checkbox" class="checkbox" name="checkbox_tname" onclick="checkbox_tname_click();">
</div>
<div class="col-xs-10 col-sm-11 col-lg-10">
<input type="text" class="form-control" name="tname" placeholder="Terminal Name">
</div>
</div>
<br>
<div class="form-group">
<label for="tbid" class="col-lg-2 control-label">TBID</label>
<div class="col-lg-10">
<input type="text" class="form-control" name="tbid" placeholder="TBID">
</div>
</div>
<br>
<div class="form-group">
<label for="version" class="col-lg-2 control-label">Version</label>
<div class="col-lg-10">
<input type="text" class="form-control" name="version" placeholder="Version">
</div>
</div>
<br>
<div class="form-group">
<label for="lastconnect" class="col-lg-2 control-label">Last Connect Time</label>
</div>
<div class="form-group">
<label for="from" class="col-lg-2 control-label">From </label>
<div class="col-lg-10">
<input type="date" class="form-control" name="timefrom" placeholder="FROMdate">
<label for="to" class="control-label">To</label>
<input type="date" class="form-control" name="timeto" placeholder="TOdate">
</div>
</div>
<div class="form-group">
<div class="col-lg-offset-2 col-lg-10">
<button type="submit" class="btn btn-default" ONCLICK="checkValue();" value="search" CLASS="BTN_MENU" >
Submit
</button>
</div>
</div>
</div>
FIDDLE