HTML Content Not Centering on Narrow Screens - html

I am using Bootstrap 3, and coding mainly for mobile websites. I am having trouble centering content when I don't want it to take up the whole line. When the screen is larger than a certain width, it doesn't get centered, & when it is smaller than that width (especially at the size we are optimizing it for), it won't even come away from the left border of the page. Also, some elements keep jumping in size when they get to be a certain size. I assume that last one is normal behavior, but can't for the life of me figure out why.
HTML:
<div class="col-xs-12">
<div class="container">
<div class="form-group col-xs-11">
<div class="col-xs-5 pull-left">
<input type="text" class='form-control' id="firstName" name="firstName" placeholder="First Name" required />
</div>
<div class="col-xs-5 pull-right">
<input type="text" class='form-control' id="lastName" name="lastName" placeholder="Last Name" required />
</div>
</div>
</div>
<div class="form-group">
<input type="email" class='form-control' id="email" name="email" placeholder="Email" required />
</div>
</div>
The #media's in bootstrap-custom.css are:
#media (min-width 1200)
.container {
max-width: 1140px;
}
#media (min-width 992)
.container {
max-width: 940px;
}
#media (min-width 768)
.container {
max-width: 720px;
}
.container {
margin-right: auto;
margin-left: auto;
padding-left: 0;
padding-right: 0;
}
I'm not sure if that's the right/accepted way to quote them.
I also have the rest of the bootstrap-custom.css, if you need that for your answer.
P.S. The whole reason I didn't have the first row (with firstName & lastName) take up the whole line is because I don't want it to. I wanted that row to be in the middle with space on either side.
P.P.S. Here is a fiddle.

Disclaimer: I haven't done any Bootstrap before - I'm just stabbing in the dark.
For a simple two column layout, create a .row and add the appropriate number of .span* columns. As this is a 12-column grid, each .span* spans a number of those 12 columns, and should always add up to 12 for each row (or the number of columns in the parent).
So, I changed your col-xs-5 elements to col-xs-6 so that they would add up to twelve, and therefore take up 100% of the row's width together. I moved your email input field into the .container and added the col-xs-11 class in order for it to mirror the other form-group's left and right margin. I wrapped your email input field in a div (as you had done with the other fields), and added the col-xs-12 class to make it stretch to 100% of its parent row's width.
Here is a fiddle.
Also, some elements keep jumping in size when they get to be a certain
size. I assume that last one is normal behavior, but can't for the
life of me figure out why.
Here is an updated fiddle that employs CSS3 transitions to ease between your different max-widths.

Have you tried text-align:centre; to your container class?

I think you need to change your markup a little. Wrap everything inside a container and make your form-group as rows..
For example:
<div class="container">
<div class="col-xs-12">
<div class="row form-group">
<div class="col-xs-6">
<input type="text" class="form-control" id="firstName" name="firstName" placeholder="First Name" required="">
</div>
<div class="col-xs-6 pull-right">
<input type="text" class="form-control" id="lastName" name="lastName" placeholder="Last Name" required="">
</div>
</div>
<div class="row form-group">
<div class="col-xs-12">
<input type="email" class="form-control" id="email" name="email" placeholder="Email" required="">
</div>
</div>
</div>
</div>
Working demo: http://bootply.com/86294
P.S. - Take a look at Option #2 in the demo so that your inputs don't get too small. This will allow them to stack vertically on the smallest screens.

Related

Center form in bootstrap

I am having issues centering a form in Bootstrap. I also don't seem to be able to resize the input boxes. I would like to have it centered.
This is what I have:
<div class="form-group row" id="SignupCreate">
<div class="col-sm-4">
<label>Email</label>
<input type="email" class="form-control" id="inputEmail" placeholder="Email">
</div>
<div class="col-sm-4">
<label>Password</label>
<input type="password" class="form-control" id="inputPassword" placeholder="Password">
<div class="col-sm-4">
<button type="submit">Submit</button>
</div>
</div>
</div>
And for CSS I started:
#SignUpCreate {
padding-top: 200px;
margin-right: 30px;
margin-left: 30px;
}
you only need to apply some width and make it center with margin: 0 auto
<div class="form-group row" id="signupcreate">
<div class="col-sm-4">
<label>Email</label>
<input type="email" class="form-control" id="inputEmail" placeholder="Email">
</div>
<div class="col-sm-4">
<label>Password</label>
<input type="password" class="form-control" id="inputPassword" placeholder="Password">
</div>
<div class="col-sm-4">
<button type="submit">Submit</button>
</div>
</div>
below code will center your form
#signupcreate{
width: 60%;
margin: 0 auto;
float: none;
}
You probably want to put it inside a container. Like this:
<div class="container">
<div class="row">
// Your code here
</div>
</div>
Possible duplicate of: Center Form using Twitter Bootstrap
There are a couple ways to solve this but I really recommend to use bootstrap itself to do it.
What I recommend is using offsets. So if you want your form centered just use something like:
<div class="form-group row" id="SignupCreate">
<div class="col-sm-4 col-sm-offset-4">
<label>Email</label>
<input type="email" class="form-control" id="inputEmail" placeholder="Email">
</div>
<div class="col-sm-4 col-sm-offset-4">
<label>Password</label>
<input type="password" class="form-control" id="inputPassword" placeholder="Password">
<div class="col-sm-4">
<button type="submit">Submit</button>
</div>
</div>
</div>
You can tweak your offsets for xs, sm, md and lg accordingly, make sure to set offset to 0 if you don't want offset in a particular view.
Now for your styles, you are giving css a SignUpCreate id when in fact is SignupCreate, remember css selectors are case sensitive.
Also keep in mind for the future that when using bootstrap you should try as much to stick to the framework and use all its features instead of coding your own CSS, and when you do, a good thing to keep in mind is that CSS uses "points" to know which styles are more relevant, I recommend checking Specifics on CSS Specificity
So let's say you want to style something that has padding right and left, I would avoid using a row or column element to do this and would add a second container div to "respect" bootstrap styles.
I hope this answer is helpful :)
There is also a good answer here: Center a column using Twitter Bootstrap 3
UPDATE:
With boostrap 4.5^ you can center any item using d-flex justify-content-center on the parent, if you also want to align the items vertically (relative to the parent's height) simply add align-items-center.

Label of the control is jumping to the right side when resized

The generated HTML I have is this:
I put it in Bootply so you can just see and play with it easier, notice it has a small CSS section too:
http://www.bootply.com/NrxiDfZJdC
Problem is it is not "bootstrappy" enough! If you start making the window smaller the labels jump to the right side of the control.
What have I done that has caused this issue?
You have a couple of major problems here.
Right Alignment:
You have set this by adding .text-right on your labels. Obviously this was meant for the desktop view only. Take it off of your labels and use a min-width media query to set the alignment or override the alignment with max-width at small resolutions
Overflowing text boxes:
You didn't use a row. You should most always use a row because it corrects the padding wit negative left and right margins. You tried to fix this yourself by instead adding a class that removes the padding on the .col-sm-4. The padding is there for a reason and should not be removed. Even adding in the row and removing the .multi-row doesn't completely correct the issue, however. When you do that you run into the text inputs being too wide. That is because you added the 100% width to the inputs. This is not a bad thing per se, but it causes problems because you have used spans for your inner columns. spans are naturally collapsed in width. They don't fill their parents' container like divs do. Swap them for divs.
Weird "Ext" label:
This is because you added a margin-left: 85% to the label to simulate the right alignment that the others have. Just remove that margin and add text-right to this label like you have on all the other similar labels.
No padding:
After all that, you'll have no padding on smaller resolutions. Add a .container around your form.
In the end, you should have this: http://www.bootply.com/uiPpBytre3
Demo:
#import url(https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css);
.form-input{
width :100%;
}
#media(max-width: 768px) {
.form-group .text-right {
text-align: left;
}
}
<div class="container">
<form class="form-horizontal" role="form">
<br>
<div class="row form-group">
<div class="col-sm-offset-2 col-sm-1 text-right"><label class="control-label" for="Name">Name</label></div>
<div class="col-sm-4"><select class="form-control" id="nameadmin" name="nameadmin"><option>77881</option>
<option>77882</option>
<option>77883</option>
<option>77884</option>
<option>77885</option>
</select></div>
<div class="col-sm-4 col-sm-offset-1">
<div>
<input class="checkbox-inline" id="ShowEmailInFooter" name="ShowEmailInFooter" type="checkbox" value="true"><input name="ShowEmailInFooter" type="hidden" value="false">
<label class="control-label" for="Show_Email_in_Footer">Show Email in Footer</label>
</div>
</div>
</div>
<div class="row form-group">
<div class="col-sm-offset-2 col-sm-1 text-right"><label class="control-label" for="Email">Email</label></div>
<div class="col-sm-4">
<input id="AdminEmail" name="AdminEmail" style="width:100%;padding-right:30px;" type="text" value="">
<span class="glyphicon glyphicon-envelope form-control-feedback" style="right: 10px; line-height: 27px; color: lightblue"></span>
</div>
<div class="col-sm-4 col-sm-offset-1">
<div>
<input class="checkbox-inline" id="ShowAdminPhone" name="ShowAdminPhone" type="checkbox" value="true"><input name="ShowAdminPhone" type="hidden" value="false">
<label class="control-label" for="Show_Admin_phone">Show Admin phone</label>
</div>
</div>
</div>
<div class="row form-group">
<div class="col-sm-offset-2 col-sm-1 text-right"><label class="control-label" for="Phone">Phone</label></div>
<div class="col-sm-4">
<div class="row">
<div class="col-sm-5"><input class="form-input" id="AdminPhone" name="AdminPhone" type="text" value=""></div>
<div class="col-sm-2 text-right"><label class="control-label" for="Ext">Ext</label></div>
<div class="col-sm-5"><input class="form-input" id="AdminExt" name="AdminExt" type="text" value=""></div>
</div>
</div>
</div>
</form>
</div>
Add responsive text align class text-sm-right instead of text-right
#media (min-width: #screen-sm-min) {
.text-sm-right { text-align: right; }
}
bootply

Bootstrap - 2 column form layout with fixed textarea height and column ordering

i'm trying to build a 2 column form layout in bootstrap with an fixed height textarea on the right. I got it working well on desktop screen with an .pull-right-md (own class) on the textarea box. But in "responsive" mode (on smaller screens) I have the problem with the order of the boxes. I want the textarea to be on the last position when displayed on small screens.. I already tried to solve this problem by ordering the col's with .col-*-pull and .col-*-push classes but it was not working.
Here you can see what I have done so far: http://www.bootply.com/BBFB4Tt97j
Do you have any ideas how I could solve this problem?
Plz don't ever do duplication of html elements for sake of styling, it's not worth it! You breaking semantics, and produce illogical code that is tricky to maintain.
You can easily achieve desired effect without duplicating anything:
http://output.jsbin.com/debenudiqi/2/
You can grab code here
http://jsbin.com/debenudiqi/2/edit
This is what I suggest, tried to work my way around with pull and push but i haven't found a solution with that. You can use the hidden-xs, visible-xs to create two different text-area, one visible only on small screen and the other on large screens only. But you make sure when you handle the form, you handle the two different text areas.
<form class="">
<div class="col-md-6 col-sm-12 form-group pull-right-md hidden-sm hidden-xs">
<textarea class="form-control textarea-fixed-height" id="textarea" name="textarea">default text</textarea>
</div>
<div class="col-md-6 col-sm-12 form-group">
<input class="form-control" id="inputEmail3" placeholder="Name" type="text">
</div>
<div class="col-md-6 col-sm-12 form-group">
<input class="form-control" id="inputPassword3" placeholder="Email" type="email">
</div>
<div class="col-md-6 col-sm-12 form-group">
<input class="form-control" id="inputPassword3" placeholder="Telefon" type="text">
</div>
<div class="col-md-6 col-sm-12 form-group">
<input class="form-control" id="inputPassword3" placeholder="Betreff" type="text">
</div>
<div class="col-md-6 col-sm-12 form-group pull-right-md hidden-lg hidden-md">
<textarea class="form-control textarea-fixed-height" id="textarea1" name="textarea1">default text</textarea>
</div>
</form>
Bootply link
So for everyone with the same kind of problem thats the solution of my problem: http://www.bootply.com/BBFB4Tt97j#
I have put two textareas in the form-tag. One as first box and the second on the last position but before the submit button. With .hidden-xs and .hidden-sm on the first textarea it's not visible for smaller devices and with .hidden-md and .hidden-lg I prevent the textarea-tag for showing up on big screens.
this is working fine now.. thx

Automatically resize parts of a div depending on the width of an image?

I'm developing a form in Bootstrap 3 that is to have a series of text controls and an image. The form takes up 100% of its container, and the text inputs also are set to 100% width. This works well when I don't have to worry about an image. I use the bootstrap .form-control and .form-horizontal classes to make it work (code at the bottom):
Current Look
I need to put the image to the right of these form controls and decrease their width appropriately:
Mockup
I would simply put in another column in Bootstrap's grid system to handle this, but I'd also like the columns to go back up to full width when the image is done, as shown in the mockup image above. Something similar to the "tight" image layout in Microsoft Word. I've tried setting float:right; and display:inline-block' to the image class but it doesn't turn out correctly:
Not Working Yet
Anyone know how to make the design work like how I described it in the mockup?
<!DOCTYPE html>
<html>
<head>
<script src="//code.jquery.com/jquery.min.js"></script>
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.0/js/bootstrap.min.js"></script>
<meta charset="utf-8">
<title>JS Bin</title>
</head>
<body>
<div>
<h2>New Guest</h2>
<form class="form-horizontal" role="form">
<img src="http://images.all-free-download.com/images/graphiclarge/man_silhouette_clip_art_9510.jpg" style="float: right; max-width: 200px; display: inline-block;" />
<div class="form-group" id="group-tbFirstName">
<label for="tbFirstName" class="col-sm-3 control-label">First Name</label>
<div class="col-sm-9">
<input type="text" class="form-control" id="tbFirstName" placeholder="First Name" value="" />
</div>
</div>
<div class="form-group" id="group-tbLastName">
<label for="tbLastName" class="col-sm-3 control-label">Last Name</label>
<div class="col-sm-9">
<input type="text" class="form-control" id="tbLastName" placeholder="Last Name" value="" />
</div>
</div>
<div class="form-group" id="group-optGender">
<label for="optGender" class="col-sm-3 control-label">Gender</label>
<div class="col-sm-9">
<input type="text" class="form-control" id="optGender" placeholder="Gender" value="" />
</div>
</div>
<div class="form-group" id="group-tbAge">
<label for="tbAge" class="col-sm-3 control-label">Age</label>
<div class="col-sm-9">
<input type="number" class="form-control" step="any" id="tbAge" value="" placeholder="Age" />
</div>
</div>
<div class="form-group" id="group-dtDateOfBirth">
<label for="dtDateOfBirth" class="col-sm-3 control-label">Date of Birth</label>
<div class="col-sm-9">
<input type="text" class="form-control" id="dtDateOfBirth" placeholder="Date of Birth" value="" />
</div>
</div>
</form>
</div>
</body>
</html>
The specific effect I'm going for is to have the inputs below the image expand out to 100% again. Like this:
I know that the image can be floated but I don't want all the inputs below it at the same width as the ones on the lines with the image. I want them to expand.
You just need to add some floats to the image and also to the form, as in the following:
(FIDDLE)
HTML
<img class="img-right">
<form class="form-horizontal">
</form>
CSS
.img-right {
float: right;
max-width: 200px;
padding-left: 20px }
.form-horizontal {
float: right }
Have you tried putting the first 3 or 4 inputs and the image in their own row? This will cause the row to expand out to the width of the container and keep your image on the right. You can also add the class class="img-responsive" to the image so that it will shrink down as the screen shrinks.
<div class="row">
<div class="col-sm-8">
//inputs go here
</div>
<div class="col-sm-4">
//image goes here
</div>
</div>
<div class="row">
<div class="col-sm-12>
//the rest of your inputs here
</div>
</div>
I see many kinds of format column in your code. This makes it behave uncontrollable.
Try to use one format only, say, class="col-lg-6.
As long as I know, bootstrap use 100% for form-control, we can modify it by choosing the suitable class of columns.
To handle your need, you can create new CSS file. place it below the bootstrap.min.css. Then Float the image to the right and set the min-width and max width in % to make it resized automatically based on browser's windows.
Besides, use class='img-responsive' inside the image attr to make it resized auto.
Use float property float: right;
.img-right {
float: right;
max-width: 200px;
padding-left: 20px;
}
.form-horizontal {
float: right;
}
<img class="img-right">
<form class="form-horizontal">
</form>

Bootstrap CSS vertical spacing disappears when columns get stacked

I'd like some form fields to be side by side when there's width available on the client, and stacked neatly when there isn't. It's almost working, but not quite "stacked neatly". Here's the markup ...
<div class="form-group">
<div class="col-md-3">
<input type="text" placeholder="State" class="form-control" ng-model="family.state">
</div>
<div class="col-md-4">
<input type="text" placeholder="Zip" class="form-control" ng-model="family.zip">
</div>
<div class="col-md-4">
<div class="checkbox">
<input type="checkbox" ng-model="family.listAddress">List address</input>
</div>
</div>
</div>
Here's the wide result, which is as desired:
But here's the narrow result which is not quite right (because of the missing vertical space):
What does your CSS look like? You can add a class to all of these divs and apply a simple margin: 5px; and that could solve your problem.