Styling inlined form inputs with Bootstrap 3 - html

I'm trying to create a simple form using Bootstrap 3 where people can sign up for a small event.
On this form, they should also be able to indicate if they want one or more T-shirts, and which size(s) they want to order.
I'm not new to HTML, CSS and Javascript, but I am quite new to Bootstrap. Also while I'm a programmer by day, I'm mainly writing REST services so my design skills are fairly limited.
Using w3schools and other tutorial sites I have arrived at this:
<div class="form-group form-group-sm">
<p class="help-block">Please select the number of T-shirts per size you would like to order.</p>
<label class="col-xs-1" for="shirt-s">S</label>
<div class="col-xs-1">
<input class="form-control" type="text" id="shirt-s">
</div>
<label class="col-xs-1" for="shirt-m">M</label>
<div class="col-xs-1">
<input class="form-control" type="text" id="shirt-m">
</div>
<label class="col-xs-1" for="shirt-l">L</label>
<div class="col-xs-1">
<input class="form-control" type="text" id="shirt-l">
</div>
<label class="col-xs-1" for="shirt-xl">XL</label>
<div class="col-xs-1">
<input class="form-control" type="text" id="shirt-xl">
</div>
<label class="col-xs-1" for="shirt-xxl">XXL</label>
<div class="col-xs-1">
<input class="form-control" type="text" id="shirt-xxl">
</div>
<div class="col-xs-2"> </div>
</div>
<div class="form-group">
<button type="submit" class="btn btn-default">Submit</button>
</div>
See jsfiddle:
https://jsfiddle.net/tonvanbart/83nbny8h/5/
Not the prettiest in the world, but I can live with it. My main question is: why is the 'submit' button not in it's own row but inlined after the input fields? At first I thought I had to get to a total of 12 columns and I only had 10. But adding a 2-column div with a non-breaking space didn't help, and adding a <br> tag had no effect either.
Also, while I can live with this (it's for an informal, small group of people who know each other well) if anybody could give me a pointer on how to get the T-shirt size indicators closer to their respective input fields, that would be great. Thank you in advance.
Feel free to let me know if you need more information.

Here's the fixed code:
https://jsfiddle.net/ccx9x7om/1/
I added the following CSS
.row .form-group label, .row .form-group input {
display: inline-block;
}
.row .form-group input {
width: 50%;
}
button {
margin: 25px;
}
In order to use bootstrap's columns properly, you need to wrap them in a row, and wrap all rows inside either a .container or a .container-fluid. I moved the labels inside their respective .col-xs-2 then wrapped everything in the needed aforementioned divs.

Another way you could do this is by nesting your columns as to split the form in half which will greatly reduce the screen space it's using and bring your inputs closer while remaining responsive across viewports.
This is simply following the container/row/column layout that Bootstrap generally follows. See Nesting Columns.
*Note: The form-group class isn't necessary, it simply adds padding around your inputs.
See working Snippet at FullPage.
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
<div class="container">
<form>
<h2>Order a Shirt</h2>
<div class="row">
<div class="col-sm-6">
<div class="form-group">
<label for="name">Name</label>
<input type="password" class="form-control" id="name" placeholder="Your Name">
</div>
<div class="form-group">
<label for="email">Email address</label>
<input type="email" class="form-control" id="email" placeholder="Email address (never shown)">
</div>
</div>
<div class="col-sm-6">
<div class="row">
<div class="col-xs-4 col-sm-4">
<div class="form-group">
<label for="shirt-s">S</label>
<input class="form-control" type="text" id="shirt-s">
</div>
</div>
<div class="col-xs-4 col-sm-4">
<div class="form-group">
<label for="shirt-m">M</label>
<input class="form-control" type="text" id="shirt-m">
</div>
</div>
<div class="col-xs-4 col-sm-4">
<div class="form-group">
<label for="shirt-l">L</label>
<input class="form-control" type="text" id="shirt-l">
</div>
</div>
</div>
<div class="row">
<div class="col-xs-4 col-sm-4">
<div class="form-group">
<label for="shirt-xl">XL</label>
<input class="form-control" type="text" id="shirt-xl">
</div>
</div>
<div class="col-xs-4 col-sm-4">
<div class="form-group">
<label for="shirt-xxl">XXL</label>
<input class="form-control" type="text" id="shirt-xxl">
</div>
</div>
<div class="col-xs-4 col-sm-4">
<div class="form-group">
<label>Order Now</label>
<button type="submit" class="btn btn-success btn-block btn-submit">Submit</button>
</div>
</div>
<div class="col-xs-12">
<p class="help-block">Please select the number of T-shirts per size you would like to order.</p>
</div>
</div>
</div>
</div>
</form>
</div>

Related

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>

Aligning a textarea besides several vertically aligned input elements

As shown in the image below, I would like to align a text area besides the vertically aligned several input elements. I am using bootstrap grid to achieve the required effect.
Currently, when I use "form-horizontal", all the items stack up in a single column, hence, the textarea lies under the phone input element.
I want it to be responsive. If I use margin property on the text area, then the inputs overlap each other and so, makes the UI look messy. It would be great if I could get your help.
Desired layout:
Current layout:
<div class="container">
<form class="form-horizontal" role="form">
<div class="form-group">
<div class="col-sm-10 col-md-2 col-lg-2 col-lg-offset-0">
<input type="text" class="form-control" id="name" placeholder="NAME">
</div>
</div>
<div class="form-group">
<div class="col-sm-10 col-md-2 col-lg-2 col-lg-offset-0">
<input type="email" class="form-control" id="email" placeholder="E-MAIL">
</div>
</div>
<div class="form-group">
<div class="col-sm-10 col-md-2 col-lg-2 col-lg-offset-0">
<input type="tel" class="form-control" id="tel" placeholder="PHONE">
</div>
</div>
<div class="form-group">
<div class="col-sm-10 col-md-6 col-lg-6 col-lg-offset-0">
<textarea class="form-control message" rows="6" id="comment" placeholder="MESSAGE"></textarea>
</div>
</div>
</form>
</div>
You can do this by nesting columns. Create two columns (equal to 12 columns), then place another row inside each along with your columns that contain the form inputs. I imagine you want your input to use the entire space available so set the nested columns to col-*-12 so they utilize the entire parent columns width.
Working Example: Open at FullPage
form {
background: #f2f2f2;
padding: 20px 20px 5px;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<div class="container">
<form>
<div class="row">
<div class="col-sm-6">
<div class="row">
<div class="col-sm-12">
<div class="form-group">
<input type="text" class="form-control" id="name" placeholder="NAME">
</div>
</div>
<div class="col-sm-12">
<div class="form-group">
<input type="email" class="form-control" id="email" placeholder="E-MAIL">
</div>
</div>
<div class="col-sm-12">
<div class="form-group">
<input type="tel" class="form-control" id="tel" placeholder="PHONE">
</div>
</div>
</div>
</div>
<div class="col-sm-6">
<div class="row">
<div class="col-sm-12">
<div class="form-group">
<textarea class="form-control message" rows="6" id="comment" placeholder="MESSAGE"></textarea>
</div>
</div>
</div>
</div>
</div>
</form>
</div>

bootstrap input forms alignment

I am trying to create a data entry form using bootstrap and forms input.
It seems to work well until on some lines it puts one entry on the line instead of 3.
I have tried various routes and always see this behavior.
Any help at understanding why it is doing this wound be greatly appreciated.
I have a plunker example here
<fieldset class="scheduler-border well">
<legend class="scheduler-border">Customer</legend>
<form class="form-group" role="form">
<div class="form-group left">
<label for="input16" class="col-md-2 control-label">Label A</label>
<div class="col-md-2">
<input type="number" class="form-control" id="Label A" placeholder="Label A">
</div>
<label for="input17" class="col-md-2 control-label">Customer Label ALpha Beta</label>
<div class="col-md-2">
<input type="number" class="form-control" id="Customer Label ALpha Beta" placeholder="Customer Label ALpha Beta">
</div>
<label for="input18" class="col-md-2 control-label">Label C</label>
<div class="col-md-2">
<input type="number" class="form-control" id="Label C" placeholder="Label C">
</div>
<label for="input15" class="col-md-2 control-label">Label D</label>
<div class="col-md-2">
<input type="number" class="form-control" id="Label AD" placeholder="Label D">
</div>
</div>
</form>
</fieldset>
If you the launch the preview window and increase the width you should be able to see the behavior.
Not entirely sure if this is what you want, but I guess what you mean is that for some screen sizes Label D ends up on the fourth column while it should be on the first. The problem here is that your label Customer Label Alpha Beta takes up too much height and since bootstrap's column are just a clever usage of float:left, Label D will be to the right of it.
The easiest way to adjust your code is to wrap groups of 3 labels/inputs in a .row to clear the floats once a row is full.
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
<fieldset class="scheduler-border well">
<legend class="scheduler-border">Customer</legend>
<form class="form-group" role="form">
<div class="form-group left container">
<div class="row">
<label for="input16" class="col-md-2 control-label">Label A</label>
<div class="col-md-2">
<input type="number" class="form-control" id="Label A" placeholder="Label A">
</div>
<label for="input17" class="col-md-2 control-label">Customer Label ALpha Beta</label>
<div class="col-md-2">
<input type="number" class="form-control" id="Customer Label ALpha Beta" placeholder="Customer Label ALpha Beta">
</div>
<label for="input18" class="col-md-2 control-label">Label C</label>
<div class="col-md-2">
<input type="number" class="form-control" id="Label C" placeholder="Label C">
</div>
</div>
<div class="row">
<label for="input15" class="col-md-2 control-label">Label D</label>
<div class="col-md-2">
<input type="number" class="form-control" id="Label AD" placeholder="Label D">
</div>
</div>
</div>
</form>
</fieldset>
Of course this solution isn't prefect, for example your rows might end up with different vertical distances. Maybe getting rid of the -col-* and using the classes .form-inline or `.form-horizontal will work better for you.

Centring two input boxes using center-block and span

Hi I am trying to center 2 input boxes in a row by adapting an example I saw here that was used for 1. I am trying to use span to get the 2 boxes together but they remain on top of each other and anchored left. Can someone help?
<div class="row">
<div class="center-block col-md-12" style="float: left; display: inline">
<button type="button" class="btn btn-primary register">
<span>
<div class="input-group">
<input type="Email" class="form-control" placeholder="Email" aria-describedby="basic-addon1">
</div>
<div class="input-group">
<input type="Password" class="form-control" placeholder="Password" aria-describedby="basic-addon1">
</div>
</span>
</button>
</div>
This is what I think you are trying to do:
<div class=".container-fluid">
<div class="row panel panel-default panel-heading">
<div class="col-md-offset-3 col-md-4">
<div class="input-group">
<input type="Email" class="form-control" placeholder="Email" aria-describedby="basic-addon1">
</div>
</div>
<div class="col-md-4">
<div class="input-group">
<input type="Password" class="form-control" placeholder="Password" aria-describedby="basic-addon1">
</div>
</div>
</div>
</div>
Here is the JSFiddle demo
Note that you are setting two textboxes within a button, which is wrong
Also, the size of your screen matters as the controls would stack on top of each other in case the screen size decreases.

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>