How can I fix this Bootstrap layout issue? - html

I have a form that uses Bootstrap 3. My layout should follow the below:
On small screens, Display 1 input per row with the label stacked above the input
On medium devices, Display 2 input per row with the label to the left of each input
On large devices, Display 3 inputs per rom with the label to the left of each input
Rows that contain a textarea, should always be displayed with one input per row
I have this almost working with one small quirk. Everything works well with all of the elements lining up like the below on all bootstrap breakpoints up to md like this:
However, when I hit the lg breakpoint, the text area and it's label no longer line up with the other element like this:
Looking at the grid I've set up I can see why this happens but I cant seem to find a pattern that fits my needs without causing this issue. I know I can start hacking away with CSS like negative margins to force the layout but I feel like there is a better way.
How can I fix this?
Here is a jsFiddle showing the issue
And the relevant HTML:
<form class="form-horizontal" role="form">
<div class="row">
<div class="col-sm-12 input-row clone-icon text-right">
<div class="form-group">
<div class="col-md-2 col-lg-2 left-on-mobile">
<label for="" class="control-label ">Textarea</label>
</div>
<div class="col-md-10 col-lg-10">
<textarea class="form-control form-input"></textarea>
</div>
</div>
</div>
<div class="col-sm-6 col-lg-4 input-row clone-icon text-right">
<div class="form-group">
<div class="col-md-4 col-lg-5 left-on-mobile">
<label for="" class="control-label ">Input Field</label>
</div>
<div class="col-md-8 col-lg-7">
<input type="text" class="form-control">
</div>
</div>
</div>
<div class="col-sm-6 col-lg-4 input-row clone-icon text-right">
<div class="form-group">
<div class="col-md-4 col-lg-5 left-on-mobile">
<label for="" class="control-label ">Input Field</label>
</div>
<div class="col-md-8 col-lg-7">
<input type="text" class="form-control">
</div>
</div>
</div>
<div class="col-sm-6 col-lg-4 input-row clone-icon text-right">
<div class="form-group">
<div class="col-md-4 col-lg-5 left-on-mobile">
<label for="" class="control-label ">Input Field</label>
</div>
<div class="col-md-8 col-lg-7">
<input type="text" class="form-control">
</div>
</div>
</div>
</div>
</form>

For the first Input field change the col-lg-* classes from 5 and 7 to 6 and 6. That will make its <label> tag to align with the one in the previous row.
<div class="col-sm-6 col-lg-4 input-row clone-icon text-right">
<div class="form-group">
<div class="col-md-4 col-lg-6 left-on-mobile">
<label for="" class="control-label ">Input Field</label>
</div>
<div class="col-md-8 col-lg-6">
<input type="text" class="form-control">
</div>
</div>
</div>
Demo link here

Related

How to ignore "order" class on mobile in Bootstrap

Is there a way to ignore the order class on larger screens?
I have two inputs with a label that goes like this;
<div class="row">
<div class="col-12 col-md-5 order-first order-md-5"><label class="form-label">Label for 1st input <small>(Explanation for first input)</small></label></div>
<div class="col-12 col-md-5 offset-md-2 order-last order-md-10"><label class="form-label">Label for 2n input<small>( Explanation for second input )</small></label></div>
<div class="col-12 col-md-5 order-first order-md-5">
<div class="form-group mt-2">
<input autocomplete="off" type="text" class="form-control" placeholder="">
</div>
</div>
<div class="col-12 col-md-2 align-self-center">
<p class="form-group text-center mt-3">and/or</p>
</div>
<div class="col-12 col-md-5 order-last order-md-10">
<div class="form-group mt-2">
<input autocomplete="off" type="text" class="form-control" placeholder="">
</div>
</div>
</div>
They work just as I want them to on mobile, however, I need them to look like they were as before I have added the order classes which look like
<div class="row">
<div class="col-12 col-md-5"><label class="form-label">Label for 1st input <small>(Explanation for first input)</small></label></div>
<div class="col-12 col-md-5 offset-md-2"><label class="form-label">Label for 2n input<small>( Explanation for second input )</small></label></div>
<div class="col-12 col-md-5">
<div class="form-group mt-2">
<input autocomplete="off" type="text" class="form-control" placeholder="">
</div>
</div>
<div class="col-12 col-md-2 align-self-center">
<p class="form-group text-center mt-3">and/or</p>
</div>
<div class="col-12 col-md-5">
<div class="form-group mt-2">
<input autocomplete="off" type="text" class="form-control" placeholder="">
</div>
</div>
</div>
Ordering is correct on mobile and just as I want, but it breaks the desktop order. Ordering without the order classes is perfect on the desktop for me, but it breaks the mobile order. Is there a way to activate the order classes on mobile only?
Try to add class like .order-1 insted of order-first and all related classes hope that work for you.

Bootstrap - vertical align form group label on top, input at bottom

I have a form that uses Bootstrap 4.4. The form groups contain a label and an input and sometimes also a select and when the select is present it breaks the alignment of the labels and inputs on the same row.
How to keep the labels always on top and the input at the bottom? I want to do this without custom Css and additional html elements if possible.
<div class="row col-sm-12 col-md-12 col-lg-12">
<div class="col-sm-12 col-md-3 col-lg-3 form-group">
<label for="value">Value:</label>
<select>
<option>something</option>
</select>
<div class="input-group">
<input type="text" class="form-control">
</div>
</div>
<div class="col-sm-12 col-md-3 col-lg-3 form-group">
<label for="name">Name:</label>
<div class="input-group">
<input type="text" class="form-control">
</div>
</div>
</div>
Using justify on one of the containers I got all the inputs to allign at the bottom but I want to keep the labels at the top.
Edit: I need to keep the layout like this, with the two (or more) form groups on one row:
If you add label -> class="w-100" it will take width:100% so it won't take any element to the same row with itself. To keep inputs always at the end of the form group you can add something on top of them. Also if you give the same design for selects and other form elements it will look better.
<div class="row col-sm-12 col-md-12 col-lg-12">
<div class="col-sm-12 col-md-3 col-lg-3 form-group">
<label class="w-100" for="value">Value:</label>
<select>
<option>something</option>
</select>
<div class="input-group">
<input type="text" class="form-control">
</div>
</div>
<div class="col-sm-12 col-md-3 col-lg-3 form-group">
<label class="w-100" for="name">Name:</label>
<div class="input-group">
<input type="text" class="form-control">
</div>
</div>
</div>

bootstrap columns on same row on mobile

I have this row using horizontal form to show the label on the left side. On Mobile i want the label to occupy one row, and I want the 3 textboxes to remain on the same row, how to acheive this? They will stack on top of each other still
<div class="row">
<div class="col-sm-12">
<div class="form-group row">
<label for="dobDay" class="col-sm-12 col-md-3 col-form-label">Date Of Birth</label>
<div class="col-sm-4 col-md-3">
<input type="number" class="form-control" id="dobDay" placeholder="DD">
</div>
<div class="col-sm-4 col-md-3">
<input type="number" class="form-control" id="dobMonth" placeholder="MM">
</div>
<div class="col-sm-4 col-md-3">
<input type="number" class="form-control" id="dobYear" placeholder="AAAA">
</div>
</div>
</div>
</div>
You can utilize the equal-width class .col:
<div class="form-group row">
<label for="dobDay" class="col-12 col-md col-form-label" />
<div class="col">
<input />
</div>
<div class="col">
<input />
</div>
<div class="col">
<input />
</div>
</div>
You set col-12 on Date of Birth label so that it would occupy a row for extra small devices (less than 576px). Since other 3 columns are set with col, they will take up equal width on their own row. That would give you the col-sm-4 effect.
On medium devices and up, the Date of Birth label, with col-md, will join the equal-width group as well. That would give you the col-md-3 effect.
demo: https://jsfiddle.net/davidliang2008/yr5gp6kq/8/
That stacking was because you didn't mention breakpoints for xs devices.
<div class="row">
<div class="col-sm-12">
<div class="form-group row">
<label for="dobDay" class="col-xs-12 col-sm-12 col-md-3 col-form-label">Date Of Birth</label>
<div class="col-xs-4 col-sm-4 col-md-3">
<input type="number" class="form-control" id="dobDay" placeholder="DD">
</div>
<div class="col-xs-4 col-sm-4 col-md-3">
<input type="number" class="form-control" id="dobMonth" placeholder="MM">
</div>
<div class="col-xs-4 col-sm-4 col-md-3">
<input type="number" class="form-control" id="dobYear" placeholder="AAAA">
</div>
</div>
</div>
</div>

Bootstrap : <h4> title not on a new line

I have the following HTML code using Bootstrap 3 CSS classes :
<h4>Profile</h4>
<div class="row form-group col-sm-8">
<label class="col-sm-2 control-label">Name :</label>
<div class="col-sm-6">
myName
</div>
</div>
<div class="row form-group col-sm-8">
<label class="col-sm-2 control-label">Email :</label>
<div class="col-sm-8">
me#internet.org
</div>
</div>
<h4>Credits</h4>
<div class="form-group col-sm-8">
<label class="col-sm-2 control-label">Credits :</label>
<div class="col-sm-7">
42
</div>
</div>
I'd like the Credits <h4> title to be on a new line, under the previous fields. But it's displayed on the top right.
Can someone please explain me why and help me display it to the right position ?
CSS Fiddle : https://jsfiddle.net/DTcHh/
The problem is that the col- classes make the elements they are applied to floats. You will need to wrap them in a container with clearfix. You probably want this (note that row applies clearfix too):
<h4>Profile</h4>
<div class="row">
<div class="form-group col-sm-8">
<label class="col-sm-2 control-label">Name :</label>
<div class="col-sm-6">
myName
</div>
</div>
<div class="form-group col-sm-8">
<label class="col-sm-2 control-label">Email :</label>
<div class="col-sm-8">
me#internet.org
</div>
</div>
</div>
<h4>Credits</h4>
<div class="form-group col-sm-8">
<label class="col-sm-2 control-label">Credits :</label>
<div class="col-sm-7">
42
</div>
</div>
You can create a new div with the class 'row' for the credits:
<div class="row">
<h4>Credits</h4>
</div>

Variable size label issue in Bootstrap

I am designing a form using Bootstrap but my labels are of variable size which are causing my form to get distorted as marked in the screenshot.
I have used clearfix class & vertical-align style as well.
I don't want fixed height for my labels as they can of varying size. (e.g. 2, 3, 4 or even of more lines).
This is my code.
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-6 col-lg-4">
<div class="form-group">
<label> Label 1 </label>
<input class="form-control" type="text">
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-6 col-lg-4">
<div class="form-group">
<label> Label 2 In fact the bigger label than all </label>
<input class="form-control" type="text">
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-6 col-lg-4">
<div class="form-group">
<label> Label 3 </label>
<input class="form-control" type="text">
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-6 col-lg-4">
<div class="form-group">
<label> Label 4 </label>
<input class="form-control" type="text">
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-6 col-lg-4">
<div class="form-group">
<label> Label 5 </label>
<input class="form-control" type="text">
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-6 col-lg-4">
<div class="form-group">
<label> Label 6 </label>
<input class="form-control" type="text">
</div>
</div>
</div>
This is how I do it when forced to use bootstrap
Within your input 'row' you have an 'input-label' and 'input-element' which are col-6. This ensures that they take up 50% of the available space. The col-xs-12 ensures that they then stack at smaller browser widths (mobile).
<div class="row">
<div class="col-md-12 col-sm-12 col-xs-12">
<div class="col-md-6 col-sm-6 col-xs-12 input-label">
<label for="xxx">Input label</label>
</div>
<div class="col-md-6 col-sm-6 col-xs-12 input-element">
<input type="text" name="xxx" id="xxx" class="col-md-12 col-sm-12 col-xs-12" />
</div>
</div>
</div>