Bootstrap 4 vertical center text inside horizontal form row [duplicate] - css

This question already has answers here:
Flexbox: center horizontally and vertically
(14 answers)
Vertical Align Center in Bootstrap 4 [duplicate]
(20 answers)
Closed 3 years ago.
In a Bootstrap 4 Horizontal form, how can a div with text be centered vertically?
Here's an example taken from https://getbootstrap.com/docs/4.3/components/forms/#horizontal-form
but instead of a form input have a plain text div, eg "Password goes here" text:
<form>
<div class="form-group row">
<label for="inputEmail3" class="col-sm-2 col-form-label">Email</label>
<div class="col-sm-10">
<input type="email" class="form-control" id="inputEmail3" placeholder="Email">
</div>
</div>
<div class="form-group row">
<label for="inputPassword3" class="col-sm-2 col-form-label">Password</label>
<div class="col-sm-10 align-middle">
Password goes here
</div>
</div>
</form>
I tried classes align-middle, d-inline-block, but no effect.

use align-self-center to align vertically
<form>
<div class="form-group row">
<label for="inputEmail3" class="col-sm-2 col-form-label">Email</label>
<div class="col-sm-10">
<input type="email" class="form-control" id="inputEmail3" placeholder="Email">
</div>
</div>
<div class="form-group row">
<label for="inputPassword3" class="col-sm-2 col-form-label">Password</label>
<div class="col-sm-10 align-self-center ">
Password goes here
</div>
</div>
</form>
Ref:https://getbootstrap.com/docs/4.3/utilities/flex/#align-self
Demo:
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<form>
<div class="form-group row">
<label for="inputEmail3" class="col-sm-2 col-form-label">Email</label>
<div class="col-sm-10">
<input type="email" class="form-control" id="inputEmail3" placeholder="Email">
</div>
</div>
<div class="form-group row">
<label for="inputPassword3" class="col-sm-2 col-form-label">Password</label>
<div class="col-sm-10 align-self-center ">
Password goes here
</div>
</div>
</form>

If you will try to vertically center align text inside a div using the CSS rule vertical-align: middle; you won't succeed. Suppose you have a div element with the height of 50px and you have placed some link inside the div that you want to align vertically center. The simplest way to do it is — just apply the line-height property with value equal to the height of div which is 50px.
.menu{
height: 20px;
line-height: 20px;
}

Related

How can i make elements to be inline in col class in bootstrap

I have this layout
<div class="container-fluid header">
<div class="row">
<div class="col-md-4">
<label>Date From</label>
<input type="text" class="form-control" bsDatepicker [(bsValue)]="dateFrom" [minDate]="currentDate">
</div>
</div>
</div>
after that the label is on top of the input. I can't find a way so in this same col-md-4, the label to be
next to the input - inline. So they show together in the same line.
How can i make this and to preserve the responsivness of the col bootstrap class ?
Add form tag with inline class.
<div class="container-fluid">
<div class="row">
<form class="form-inline">
<div class="col-md-4 form-group ">
<label>Date From</label>
<input type="text" class="form-control" bsDatepicker [(bsValue)]="dateFrom" [minDate]="currentDate">
</div>
</form>
</div>

Multiple horizontal nested form-rows

I'm using Bootstrap 4.0 beta 2 and try to align label and form control horizontal in one line by using form-row and col-* classes.
Now I'm trying to align two of these form-row structs in one line, see fiddle (use HD resolution when running). But I don't know why the first input in first row is not vertically aligned with input field of second row. I've struggled for some hours now and using different classes form-inline, row, and so one and tried to play with margin and padding classes, but no luck.
The DOM cannot be changed, only the classes applied. Who can help me?
The problem is that your first column additionally has form-row on it, creating a row inside another row. There should never be a row inside a row; only use columns inside of rows. These rows add additional padding (15px for row, and 5px for form-row), which interferes with the percentage-based widths and offsets when you nest them.
Instead of nesting your second label + div inside of a new form-row, you need to take it out of its form-row, and place it inside of the form-row of the first label + div. After this, you'll need to adjust the col-md counts of both label+ div combinations to suit.
I've gone with 2 and 3 respectively, as that comes to a grand total of 10 ((2 + 3) + (2 + 3)), which equals the total of your second row (2 + 8).
I've created a fiddle showcasing this here, and you can also see this by maximising the snippet below:
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css" rel="stylesheet"/>
<form>
<fieldset>
<div class="form-row">
<label class="col-form-label col-md-2 col-sm-12">LABEL</label>
<div class="col-md-3 col-sm-12">
<input type="text" class="form-control">
</div>
<label class="col-form-label col-md-2 col-sm-12">LABEL</label>
<div class="col-md-3 col-sm-12">
<input type="text" class="form-control">
</div>
</div>
<div class="form-row mt-2">
<label class="col-form-label col-md-2 col-sm-12">LABEL</label>
<div class="col-md-8 col-sm-12">
<input type="text" class="form-control">
</div>
</div>
</fieldset>
</form>
Hope this helps! :)
CSS Only Solution:
Assuming you can't modify the DOM, you can always fall back to CSS:
.form-row.mt-2 {
width: 100%;
}
#media screen and (min-width: 768px) {
.form-row.mt-2>div {
padding-left: 4px;
padding-right: 13px;
}
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css" rel="stylesheet" />
<form>
<fieldset>
<div class="form-row">
<div class="form-row col-md-6 col-sm-12">
<label class="col-form-label col-md-4 col-sm-12">LABEL</label>
<div class="col-md-4 col-sm-12">
<input type="text" class="form-control">
</div>
</div>
<div class="form-row col-md-6 col-sm-12">
<label class="col-form-label col-md-4 col-sm-12">LABEL</label>
<div class="col-md-4 col-sm-12">
<input type="text" class="form-control">
</div>
</div>
</div>
<div class="form-row mt-2">
<label class="col-form-label col-md-2 col-sm-12">LABEL</label>
<div class="col-md-8 col-sm-12">
<input type="text" class="form-control">
</div>
</div>
</fieldset>
</form>
Though again, the DOM modification would be the more preferable solution :)
Check the snippet.
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css" />
<form class="col-12">
<div class="form-row">
<div class="col-md-6 col-12">
<div class="form-row">
<label class="col-form-label col-md-4 col-12">LABEL</label>
<div class="col-md-8 col-12">
<input type="text" class="form-control">
</div>
</div>
</div>
<div class="col-md-6 col-12">
<div class="form-row">
<label class="col-form-label col-md-4 col-12">LABEL</label>
<div class="col-md-8 col-12">
<input type="text" class="form-control">
</div>
</div>
</div>
</div>
<div class="form-row align-items-center mt-2">
<label class="col-form-label col-md-2 col-12">LABEL</label>
<div class="col-md-10 col-12">
<input type="text" class="form-control">
</div>
</div>
</form>

Bootstrap text fields not selectable on mobile browser

On my website http://createabundance.ca, inside the payment form that is shown when any of the orange buttons are clicked, the text fields are not selectable on mobile safari or chrome, but work without issue on desktop safari, and chrome.
Screenshot: http://d.pr/i/nrBu
Any ideas as to what the issue might be?
You have form fields like this
<div class="form-row">
<div class="col-xs-12 form-group required">
<input name="email" class="form-control" size="4" placeholder="邮件地址" type="email">
</div>
</div>
remove col-xs-12 from <div class="col-xs-12 form-group required"> and put it in separate div <div class="col-xs-12"> like below
<div class="form-row">
<div class="col-xs-12">
<div class="form-group required">
<input name="name" class="form-control" size="4" placeholder="姓名" type="text">
</div>
</div>
</div>
and it will fix the problem when form opens in mobile
sidenote: class="form-row" has no css property so totally remove this <div class="form-row"> because it severing no purpose
<div class="col-xs-12">
<div class="form-group required">
<input name="name" class="form-control" size="4" placeholder="姓名" type="text">
</div>
</div>
but if you want to use <div class="form-row"> then
<div class="col-xs-12">
<div class="form-row">
<div class="form-group required">
<input name="name" class="form-control" size="4" placeholder="姓名" type="text">
</div>
</div>
</div>

Nesting Bootstrap input boxes side by side

I have two input boxes that I am trying to get side by side on the row but I'd like there still to be a gap between the boxes but flush to the sides of the container.
Is there a way using just Bootstrap to do this? The container is a replication of a mobile device.
<div class="group col-sm-12">
<div class="row">
<div class="col-sm-6">
<div class="row">
<input type="text" name="first-name" ng-model="user.firstname" />
</div>
</div>
<div class="col-sm-6">
<div class="row">
<input type="text" name="last-name" ng-model="user.surname" />
</div>
</div>
</div>
</div>
plunkr example
Bootstrap has a few different grid classes. col-<size>-<num> where <size> can be xs, sm, md, or lg, and <num> can be an integer between (and including) 1-12.
For mobile devices, you'll want to use xs. If you don't specify any of the larger sizes, then they'll use the same widths as the smaller one. If you change col-sm-12 to col-xs-12 and col-sm-6 to col-xs-6 I think you'll get the result you're looking for.
It also helps your inputs look more consistent and more easily themed if you give them the form-control class.
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet"/>
<div class="group col-xs-12">
<div class="row">
<div class="col-xs-6">
<div class="row">
<input class="form-control" type="text" name="first-name" />
</div>
</div>
<div class="col-xs-6">
<div class="row">
<input class="form-control" type="text" name="last-name" />
</div>
</div>
</div>
</div>
I'm not familiar with Bootstrap, but one way to achieve it would be to add this to your CSS:
.col-sm-6, .col-sm-6 .row, .col-sm-6 .row input {
display: inline-block;
}
I am not sure if I understand your question correctly. You need a space between the text box side by side right? if so try this code. You could also use col-xs-offset-1 for space between grid. This will allow you to push 1 grid away. example http://www.bootply.com/U6vlrIktq2
<div class="col-md-4">
<input type="text" class="form-control">
</div>
<div class="col-md-4 ">
<input type="text" class="form-control"><br>
</div>
<!-- for more spacing between boxes-->
<div class="col-xs-5">
<input type="text" class="form-control">
</div>
<div class="col-xs-5 col-xs-offset-1">
<input type="text" class="form-control">
</div>

Bootstrap 3 - Division of columns

Assume we have two rows:
1 Row with col-md-12
1 Row with 2 divs, each of col-md-6
In the first row, I have Label/Textbox in a form-horizontal way. An example is shown below.
My question is, does the div with class form-group give me a row of width 12 columns? How to align content when I have rows including columns of different width? For instance, I need to align the sub button to be aligned with the Textbox, not the Label.
I am realizing that when I use a div.form-group, then I get the chance to have new 12 columns, even though the div.form-group is inside a div.col-md-6. Is this correct?
<!-- Latest compiled and minified CSS -->
<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="row">
<div class="col-xs-11 col-md-6">
<div class="form-group">
<label for="txtFirstName" class="control-label col-md-3 col-xs-3">First Name</label>
<div class="col-md-7 col-xs-8">
<input type="text" class="form-control" id="txtFirstName">
</div>
</div>
</div>
<div class="col-xs-11 col-md-6">
<div class="form-group">
<label for="txtLastName" class="control-label col-md-3 col-xs-3">Last Name</label>
<div class="col-md-7 col-xs-8">
<input type="text" class="form-control" id="txtLastName">
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-xs-11 col-md-6">
<div class="form-group">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</div>
you will have to use class 'row' to make it into col-6 div.
<div class="col-xs-11 col-md-6">
<div class="form-group">
<div class="row">
<div class="col-md-3 col-xs-3">
<label for="txtFirstName" class="control-label">First Name</label>
</div>
<div class="col-md-7 col-xs-8">
<input type="text" class="form-control" id="txtFirstName">
</div>
</div>
</div>
</div>
Check http://getbootstrap.com/css/ , Nesting columns section

Resources