How to align vertically input field text and checkbox? - css

I have input field and checkbox in the same row. I would like to vertically align checkbox in the middle. Here is example of what I have:
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
<div class="form-row">
<div class="form-group col-6">
<label for="Addr">Address:</label>
<input class="form-control" type="text" name="Addr" id="Addr" value="" placeholder="Enter Physical Address" maxlength="40">
</div>
<div class="form-group col-6">
<div class="custom-control custom-checkbox">
<input class="custom-control-input" type="checkbox" name="sameAddress" id="sameAddress" value="Y">
<label class="custom-control-label" for="sameAddress">check this box if mailing address is the same as physical address</label>
</div>
</div>
</div>
As you can see in my example checkbox is on the top of the div container. How to align check box vertically in the middle?

As Bootstrap 4 documentation. Seen here https://getbootstrap.com/docs/4.0/utilities/flex/
you can use class="d-flex align-items-center".
I added style="border: 1px solid black; height:200px;" to demonstrate.
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
<div class="form-row d-flex align-items-center" style="border: 1px solid black; height:200px;">
<div class="form-group col-6">
<label for="Addr">Address:</label>
<input class="form-control" type="text" name="Addr" id="Addr" value="" placeholder="Enter Physical Address" maxlength="40">
</div>
<div class="form-group col-6 ">
<div class="custom-control custom-checkbox ">
<input class="custom-control-input" type="checkbox" name="sameAddress" id="sameAddress" value="Y">
<label class="custom-control-label" for="sameAddress">check this box if mailing address is the same as physical address</label>
</div>
</div>
</div>

display: flex is often an easy way to align elements.
In your CSS it would be like this :
div.form-row {
display: flex;
align-items: center;
}
/* I overwrote the bottom margins that were messing with my alignment */
div.form-group.col-6 {
margin-bottom: 0;
}

You could wrap it in a div class and just set the margin how you want it to align it perfectly with your input box.
You would just add the below to your css and then wrap your custom-control element in a div with css class centered
Here's a live example https://jsfiddle.net/qpwsmoh8/
and the code
css
.centered {
margin-top:38px;
}
html
<div class="centered">
<div class="custom-control custom-checkbox">
...
</div>
</div>

Change this
<div class="custom-control custom-checkbox">
<input class="custom-control-input" type="checkbox" name="sameAddress" id="sameAddress" value="Y">
<label class="custom-control-label" for="sameAddress">check this box if mailing address is the same as physical address</label>
</div>
To this snippet -
<div class="custom-control custom-checkbox" style="
display: flex;
justify-items: center;
align-items: center;
">
<label class="custom-control-label" style="
float: left;
"></label>
<input class="custom-control-input" type="checkbox" name="sameAddress" id="sameAddress" value="Y">
<label class="" for="sameAddress">check this box if mailing address is the same as physical address</label>
</div>

try adding this in the css for your input tag or the corresponding classname
input {
vertical-align: bottom;
position: relative;
}

Related

How can i decrease boostrap's form-control width without using the responsivness

I have this html
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous">
<div class="container-fluid">
<div class="row">
<div class="col-md-12">
<div class="form-group">
<label class="form-control-label" for="salary" name="salary">Specify the salary for <span
class="user-detail">{{user?.first_name}}</span></label>
<input id="salary" class="form-control form-control-alternative" type="text">
</div>
</div>
</div>
</div>
when I apply form-control then the input goes till the end of the parent container.
Here I will need to write numbers on the input so it will be too big.
How can I decrease its size ?
If I try to apply custom style
.form-control {
width:200px !important;
}
then it starts to be inline-block instead like block element.
You can use Bootstrap's column sizing on form controls. Here I'm using col-* for various sizes.
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous">
<div class="container-fluid">
<div class="row">
<div class="col-12">
<div class="form-group">
<label class="form-control-label" for="salary" name="salary">Specify the salary for <span
class="user-detail">{{user?.first_name}}</span></label>
<input id="salary" class="form-control form-control-alternative col-4" type="text">
</div>
<div class="form-group">
<label class="form-control-label" for="salary" name="salary">Specify the salary for <span
class="user-detail">{{user?.first_name}}</span></label>
<input id="salary" class="form-control form-control-alternative col-6" type="text">
</div>
<div class="form-group">
<label class="form-control-label" for="salary" name="salary">Specify the salary for <span
class="user-detail">{{user?.first_name}}</span></label>
<input id="salary" class="form-control form-control-alternative col-8" type="text">
</div>
</div>
</div>
</div>
See Bootstrap's column sizing docs.

Cannot set input on the same line with small dimension

I'm trying to add two input field on the same line with the minimum dimension, I tried this:
<div class="row">
<input type="number" class="form-control" class="slider-start" data-id="1">
<input type="number" class="form-control" class="slider-end" data-id="1">
</div>
this is a fiddle
Try adding inline-flex inside your style.
<div class="row" style="display: inline-flex;">
<input type="number" class="form-control" class="slider-start" data-id="1">
<input type="number" class="form-control" class="slider-end" data-id="1">
</div>
Flexbox should help you.
.form-control{
width: 35%; /*size of input box*/
flex-flow: row nowrap; /*sets flex-box elements order*/
}
.row {
display: flex;
}
I hope that I helped.
Do you mean something like this?
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
<div class="row">
<div class="col-auto">
<input type="number" class="form-control" class="slider-start" data-id="1">
</div>
<div class="col-auto">
<input type="number" class="form-control" class="slider-end" data-id="1">
</div>
</div>

Bootstrap left align right-text class on mobile

Using the text-right class on my form labels, when going responsive, I want the label to left align so it's top-left instead of top-right, however the change to text-align:left does not appear to take effect.
#media (max-width: 767px) {
.form-group > label.text-right {
text-align: left;
color: red;
}
}
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
<div class="form-group row">
<label class="col-sm-2 col-form-label text-right">Label</label>
<div class="col-sm-10">
<input type="text" class="form-control">
</div>
</div>
Bootstrap's text alignment classes are responsive. You don't need any extra CSS.Use text-sm-right text-left...
<div class="form-group row">
<label class="col-sm-2 col-form-label text-sm-right text-left">Label</label>
<div class="col-sm-10">
<input type="text" class="form-control">
</div>
</div>
Demo: https://www.codeply.com/go/djMd492DHA
Note: The accepted answer is best for Bootstrap 4, but the following will work for Bootstrap 3
That's become !important is set on .text-right
You can also set !important. I might suggest not using the .text-right class and maybe make your own. Using !important to override another !important makes me cringe. I wouldn't recommend it, and I would recommend using !important as infrequently as possible. I can understand why Bootstrap uses it here, though.
I'll give you two options.
Here's what you could do:
#media (max-width: 767px) {
.form-group > label.text-right {
text-align: left !important;
color: red;
}
}
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
<div class="form-group row">
<label class="col-sm-2 col-form-label text-right">Label</label>
<div class="col-sm-10">
<input type="text" class="form-control">
</div>
</div>
Here's what I suggest doing:
#media (max-width: 767px) {
.form-group > label.align-label {
text-align: left;
color: red;
}
}
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
<div class="form-group row">
<label class="col-sm-2 col-form-label align-label">Label</label>
<div class="col-sm-10">
<input type="text" class="form-control">
</div>
</div>

Bootstrap Inline form - Horizontal stacked inputs and button

I have a design, where there are 4 input fields ending with a Search Button i.e.
INPUT1 INPUT2 INPUT3 INPUT4 BUTTON
I need all these to be stacked horizontally inside a form tag and should be inline with out any padding or spaces, showing at the center of the page on top of a banner. Need to accomplish this using bootstrap.
Regards,
Sijo Jose
Try this one.
<form>
<div class="input-container">
<div class="inline-content">
<input type="text" value="input 1">
<input type="text" value="input 2">
<input type="text" value="input 3">
<input type="text" value="input 4">
<button type="button">button</button>
</div>
</div>
</form>
I have also created a Bootply. Here is the link. http://www.bootply.com/XlBQ3QRLGN
Below code might help
HTML Code :-
<div class="header">
<div class="inner">
<input type="text"/>
<input type="text"/>
<input type="text"/>
<input type="text"/>
<input type="submit"/>
</div>
</div>
CSS Code:-
div {
min-height:75px;
line-height:75px;
text-align:center;
color:white;
font-weight:bold;
}
.inner {
width:100%;
max-width:960px;
margin:0 auto;
}
.header {
background:red;
}
Using form-inline using bootstrap :-
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<style>
form {
margin: 0 auto;
}
</style>
</head>
<body>
<form class="form-inline">
<label class="sr-only" for="inlineFormInput">Name</label>
<input type="text" class="form-control mb-2 mr-sm-2 mb-sm-0" id="inlineFormInput" placeholder="inputone">
<label class="sr-only" for="inlineFormInput">Name</label>
<input type="text" class="form-control mb-2 mr-sm-2 mb-sm-0" id="inlineFormInput" placeholder="inputtwo">
<label class="sr-only" for="inlineFormInput">Name</label>
<input type="text" class="form-control mb-2 mr-sm-2 mb-sm-0" id="inlineFormInput" placeholder="inputthree">
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</body>
</html>
Using bootstrap
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<form>
<label class="text-inline">
<input type="text">
</label>
<label class="text-inline">
<input type="text">
</label>
<label class="text-inline">
<input type="text">
</label>
<label class="btn-inline">
<input type="submit" name="submit">
</label>
</form>
</div>
</body>
</html>

Aligning the Bootstrap Form Labels

I have this Bootstrap horizontal form but the problem is label text is not aligned . How can I align the label text.
<!DOCTYPE HTML>
<html>
<head>
<link rel="stylesheet" href="bootstrap/css/bootstrap.min.css" />
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-12">
<h3>Signup Form</h3><hr/>
<form class="form-horizontal" role="form">
<div class="form-group">
<label for="username" class="col-sm-2 control-label">Username</label>
<div class="col-sm-4">
<input type="text" placeholder="Your Username ..." class="form-control"/>
</div>
</div>
<div class="form-group">
<label for="email" class="col-sm-2 control-label">Email</label>
<div class="col-sm-4">
<input type="email" placeholder="Your Email ..." class="form-control"/>
</div>
</div>
</form>
</div>
</div>
</div>
<script src="bootstrap/js/jquery-1.11.3.js"></script>
<script src="bootstrap/js/bootstrap.min.js"></script>
</body>
</html>
I want Email label to be aligned with Username label
It is aligned right by default in Bootstrap stylesheet, you can give your form and id and add something like this to your stylesheet:
#signup-form label {
text-align: left;
}
This will align all the labels in your form to left, you may modify the selector to your needs to limit the scope of this style.
Working sample at JSBin.
.form-horizontal .control-label {
text-align: left;
}
simply use this.
CSS:
.control-label {
text-align:left!important;
}

Resources