I'm trying to find a way to stack an element within a row class.
See JS Fiddle here: https://jsfiddle.net/jLhvmq24/2/
I need small tag "Enter Business Email" to show-up below the input box
<div class="row">
<div class="input-group col-10">
<input type="text" class="form-control" placeholder="email address">
<small class="text-muted">Enter Business Email.</small>
</div>
<div class="col-2">
<button class="btn btn-light">Subscribe</button>
</div>
</div>
Use the outer most parent(row) to flex row(by default it displays row).
Since you need the text to be below the input so add flex-direction:column; to the .input-group class.
fiddle
.row {
display: flex;
}
.input-group {
display: flex;
flex-direction: column;
}
<div class="row">
<div class="input-group col-10 mb-4">
<input type="text" class="form-control" placeholder="email address">
<small class="text-muted">Enter Business Email.</small>
</div>
<div class="col-2">
<button class="btn btn-light" type="button">Subscribe</button>
</div>
</div>
You could also use grids to solve it.
CSS
.container {
display: grid;
grid-template-rows: 1fr 1fr;
grid-template-areas:
"input"
"small";
}
.input-1 {
grid-area: input;
}
.small-1 {
grid-area: small;
}
HTML
<div class="row">
<div class="input-group col-10 container">
<input type="text" class="form-control input-1" placeholder="email address">
<small class="text-muted small-1">Enter Business Email.</small>
</div>
<div class="col-2">
<button class="btn btn-light">Subscribe</button>
</div>
</div>
You can remove the padding overwriting it from the Bootstrap cols classes
Related
How can one display multiple form input-groups inline with Bootstrap 5?
Tried this, but input group components show on top of each other:
<div class="container">
Your username is
<div class="input-group form-control-inline">
<span class="input-group-text">#</span>
<input type="text" class="form-control " placeholder="Username">
</div>
and your email <is></is>
<div class="input-group form-control-inline">
<input type="text" class="form-control form-control-inline" placeholder="">
<span class="input-group-text">#example.com</span>
</div>
</div>
.form-control-inline {
display: inline-block;
width: auto;
vertical-align: middle;
margin-top: -8px;
}
.form-control-inline.form-control, .form-control-inline.input-group-text {
display: inline-block;
}
Fiddle: https://jsfiddle.net/46se871g/
I used flex to help me achieve what you need. as flex gives more controls from the parent to the entire child for your HTML. read more details for flex.
Also, I moved the text into the .input-group class to have every div with all of it's content.
HTML:
<div class="container">
<div class="input-group">
Your username is
<span class="input-group-text">#</span>
<input type="text" class="form-control" placeholder="Username">
</div>
<div class="input-group">
and your email <is></is>
<input type="text" class="form-control form-control-inline"
placeholder="">
<span class="input-group-text">#example.com</span>
</div>
</div>
CSS:
.input-group {
display: flex;
align-items: center
}
.container {
display: flex;
grid-template-columns: repeat(1, 1fr);
align-items: center;
}
In this simple example, when I have a validation error on the First Name input box on the left side of the form, the E-Mail form element gets pushed down the page.
When I have the same error on the right side of the form though, this behavior does not happen.
I don't see any difference in the code. Here are some screen shots. I was unable to get this to work in jsfiddle or codepen, so here is the link to the test page and I've also pasted my html below:
https://www.blastyourresume.com/testing/formalignment/test.cfm
To reproduce the error on that page, type something in the firstname field, then delete it. If you repeat this process on the right side, you don't get the same behavior.
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<div class="container" align="center">
<form name="frmSignup" id="frmSignup" method="post">
<div class="col-sm-6 col-xs-12 col-md-6">
<div class="form-group" align="left">
<label>First Name</label>
<input type="text" placeholder="First Name" id="firstname" name="firstname" maxlength="50" class="form-control">
</div>
</div>
<div class="col-sm-6 col-xs-12 col-md-6">
<div class="form-group" align="left">
<label>Last Name</label>
<input type="text" placeholder="Last Name" id="lastname" name="lastname" maxlength="50" class="form-control">
</div>
</div>
<div class="col-sm-6 col-xs-12 col-md-6">
<div class="form-group" align="left">
<label>E-Mail Address</label>
<input type="text" placeholder="E-Mail" id="email" name="email" maxlength="100" class="form-control">
</div>
</div>
<div class="col-sm-6 col-xs-12 col-md-6">
<div class="form-group" align="left">
<label>Confirm E-Mail Address</label>
<input type="text" placeholder="Confirm E-Mail" id="confirmemail" name="confirmemail" maxlength="100" class="form-control">
</div>
</div>
<div class="col-sm-12 col-xs-12 col-md-12">
<button type="submit" name="btnSubmit" class="btn btn-primary" id="btnSubmit"> Submit </button>
</div>
</form>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
Flex approach
add display:flex to your parent container. Display flex will make
your columns equal height.
#frmSignup {
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-ms-flex-wrap: wrap;
flex-wrap: wrap;
}
Absolute positioning of error messages.
.form-group {
position: relative;
}
.errormessageclass {
position: absolute;
top: 100%;
left: 0;
width: 100%;
}
For some reason when validation message is shown for the first name input it causes email field to be pushed left and confirm email goes to the next row. I found that it could be fixed if you wrap you form groups in <div class="row"></div> like:
<div class="row">
<div class="col-sm-6 col-xs-12 col-md-6" style="">
<div class='form-group>first name</div>
</div>
<div class="col-sm-6 col-xs-12 col-md-6" style="">
<div class='form-group>last name</div>
</div>
</div>
<div class="row">
<div class="col-sm-6 col-xs-12 col-md-6" style="">
<div class='form-group>email</div>
</div>
<div class="col-sm-6 col-xs-12 col-md-6" style="">
<div class='form-group>confirm email</div>
</div>
</div>
https://codepen.io/
But it sound like a rough hack.
I also would be grateful to get an explanation of why pushing effect happens.
I have a 4-column horizontal form in Bootstrap 3 (latest version):
<div class="container">
<div class="row">
<div class="col-xs-12">
<h1>Title is here</h1>
</div>
</div>
<div class="row">
<div class="col-xs-12 no-gutter">
<form class="form-horizontal">
<div class="col-md-3">
<input type="text" class="form-control input-lg" placeholder="Your Title">
</div>
<div class="col-md-3">
<input type="text" class="form-control input-lg" placeholder="Your Title">
</div>
<div class="col-md-3">
<input type="text" class="form-control input-lg" placeholder="Your Email Address">
</div>
<div class="col-md-3">
<button type="submit" class="form-control input-lg btn-danger">Check The Price</button>
</div>
</form>
</div>
</div>
And the following CSS (to make the input fields next to each other):
.col-xs-12.no-gutter [class*='col-'] {
padding-right: 0;
padding-left: 0;
}
Here's the issue. I want the button to be smaller (for it to take 2 columns instead of 3). So I changed the last <div> from col-md-3 to col-md-2 and got this:
http://www.bootply.com/JiNeG04Ifl
The code is the same like the pasted code above, just with the last <div> changed to col-md-2. Now, try to resize the viewport from 1194px to 992px (the medium size after the forms are switching to vertical). You will notice that the text overflows the box, although there is more than enough white space in the button itself.
How can I fix this? So far I've tried:
white-space: nowrap;
But the problem with this is that the text does not stay centered in the box, its alignment seems to be going off. Is there some better solution to keep the alignment centered?
Remove the l/r padding from the button...
.input-lg.btn-danger {
padding-left: 0;
padding-right: 0;
}
http://www.bootply.com/QkBiP8yHNE
#import url('https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css');
/* CSS used here will be applied after bootstrap.css */
.margin-bottom-30 {
margin-bottom: 30px;
}
.margin-top-30 {
margin-top: 30px;
}
.col-xs-12.no-gutter [class*='col-'] {
padding-right: 0;
padding-left: 0;
}
* {
border-radius: 0 !important;
}
.input-lg.btn-danger {
padding-left: 0;
padding-right: 0;
}
<div class="container">
<div class="row">
<div class="col-xs-12">
<h1 class="margin-bottom-30">Title Here</h1>
</div>
</div>
<div class="row no-gutter">
<div class="col-xs-12 form no-gutter">
<form class="form-horizontal">
<div class="col-md-3">
<input type="text" class="form-control input-lg" placeholder="Your Title">
</div>
<div class="col-md-3">
<input type="text" class="form-control input-lg" placeholder="Your Title">
</div>
<div class="col-md-3">
<input type="text" class="form-control input-lg" placeholder="Your Email Address">
</div>
<div class="col-md-2">
<button type="submit" class="form-control input-lg btn-danger">Check The Price</button>
</div>
</form>
</div>
</div>
</div>
<div id="push"></div>
I'm using the following code to display a login box with bg image.
body {
background: url(http://lorempixel.com/1920/1920/city/9/) no-repeat center center fixed;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
}
.panel-default {
opacity: 0.9;
margin-top:30px;
}
.form-group.last {
margin-bottom:0px;
}
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
<div class="row">
<div class="col-md-4 col-md-offset-7">
<div class="panel panel-default">
<div class="panel-heading">
<span class="glyphicon glyphicon-lock"></span> Login</div>
<div class="panel-body">
<form class="form-horizontal" role="form">
<div class="form-group">
<label for="inputEmail3" class="col-sm-3 control-label">
Email</label>
<div class="col-sm-9">
<input type="email" class="form-control" id="inputEmail3" placeholder="Email" required>
</div>
</div>
<div class="form-group">
<label for="inputPassword3" class="col-sm-3 control-label">
Password</label>
<div class="col-sm-9">
<input type="password" class="form-control" id="inputPassword3" placeholder="Password" required>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-3 col-sm-9">
<div class="checkbox">
<label>
<input type="checkbox"/>
Remember me
</label>
</div>
</div>
</div>
<div class="form-group last">
<div class="col-sm-offset-3 col-sm-9">
<button type="submit" class="btn btn-success btn-sm">
Sign in</button>
<button type="reset" class="btn btn-default btn-sm">
Reset</button>
</div>
</div>
</form>
</div>
<div class="panel-footer">
Not Registred? Register here</div>
</div>
</div>
</div>
</div>
I was able to align it horizontally using the below code
col-md-4 col-md-offset-4
Here, the top margin set in CSS doesn't look good in mobile devices since it is not center aligned. I want the login box to be vertically centered and I've tried various solutions such as using display: flex etc but none worked in this case.
Could you help me?
Thanks.
Firstly you have to say that your container should take 100% height of the body
which can be done with
.container{
height: calc(100vh);
}
After that you can keep display:table to parent div and display:table-cell to child div along with vertical-align: middle which will give you vertically align center
check this fiddle
In your code I have changed
HTML:
<div class="row login-box">
CSS:
.container{
height: calc(100vh);
display: table;
table-layout: fixed;
width: 100%;
}
.login-box{
display: table-cell;
width: 100%;
vertical-align: middle;
}
You can use CSS Flexbox. You also need to define height: 100vh for the body and make it flex container.
Like:
body {
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
}
Have a look at the snippet below (use full page view) :
body {
background: url(http://lorempixel.com/1920/1920/city/9/) no-repeat center center fixed;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
}
.panel-default {
opacity: 0.9;
margin-top:30px;
}
.form-group.last {
margin-bottom:0px;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
<div class="row">
<div class="col-md-6 col-md-offset-3">
<div class="panel panel-default">
<div class="panel-heading">
<span class="glyphicon glyphicon-lock"></span> Login</div>
<div class="panel-body">
<form class="form-horizontal" role="form">
<div class="form-group">
<label for="inputEmail3" class="col-sm-3 control-label">
Email</label>
<div class="col-sm-9">
<input type="email" class="form-control" id="inputEmail3" placeholder="Email" required>
</div>
</div>
<div class="form-group">
<label for="inputPassword3" class="col-sm-3 control-label">
Password</label>
<div class="col-sm-9">
<input type="password" class="form-control" id="inputPassword3" placeholder="Password" required>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-3 col-sm-9">
<div class="checkbox">
<label>
<input type="checkbox"/>
Remember me
</label>
</div>
</div>
</div>
<div class="form-group last">
<div class="col-sm-offset-3 col-sm-9">
<button type="submit" class="btn btn-success btn-sm">
Sign in</button>
<button type="reset" class="btn btn-default btn-sm">
Reset</button>
</div>
</div>
</form>
</div>
<div class="panel-footer">
Not Registred? Register here</div>
</div>
</div>
</div>
</div>
Hope this helps!
I have the below HTML and CSS - how do I make the search-bar and add-contacts the same height? I currently have their height set to 100% in CSS to expand inside their container div but that doesn't work Is it possible to change their height even though they are bootstrap units?
HTML
<div className="container">
<form className="form-inline search-bar" role="form">
<div className="form-group has-success has-feedback">
<label className="control-label" htmlFor="inputSuccess4"></label>
<input type="text" className="form-control" id="inputSuccess4" type="text" placeholder="Search" onChange={handleChange}/>
<span className="glyphicon glyphicon-search flipped form-control-feedback"></span>
</div>
</form>
<div className="add-contacts">
<button type="button" className="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">
+ Contacts Keeper
</button>
</div>
</div>
CSS
.table-container {
margin: 20px;
}
.search-bar {
margin-left: 20px;
float: left;
height: 100%;
}
.add-contacts {
margin-right: 20px;
float: right;
height: 100%;
}
Just change the height of the search bar.
Or you could put search bar in same div as add contacts bar.
I find simpler is better with Bootstrap. I try to avoid adding custom CSS for most of the HTML elements already styled by Bootstrap:
<div class="container">
<div class="row">
<div class="col-md-12">
<form class="form-horizontal">
<div class="col-md-3 text-left">
<div class="form-group">
<label class="sr-only" for="exampleInputAmount">Search</label>
<div class="input-group">
<input type="text" class="form-control" id="exampleInputAmount" placeholder="Search">
<div class="input-group-addon"><i class="fa fa-search"></i></div>
</div>
</div>
</div>
<div class="col-md-6"></div>
<div class="col-md-3 text-right">
<button type="submit" class="btn btn-primary">+ add stuff</button>
</div>
</form>
</div>
</div>
</div>