Bootstrap Rows? - css

I have a form and im getting confused with rows.
Where should I put in rows? Do I need them? Do I need one for a modal? One for the entire form or each form input?
Here's what I have:
<div class="container">
<div id="modal" class="modal fade">
//modal stuff
</div><!-- /.modal -->
<h1>Title Here</h1>
<form id="content-add-form" class="form-horizontal" role="form" name="content-add-form" enctype="multipart/form-data">
<div class="form-group">
<label for="title" class="col-md-2 control-label">Title:</label>
<div class="col-md-4">
<input name="title" type="text" class="form-control" placeholder="Title">
</div>
</div>
<div class="form-group">
<label for="date" class="col-md-2 control-label">Date:</label>
<div class="col-md-2">
<div class='input-group date' id='date-picker'>
<input type='text' class="form-control" name="date" value="{{ date("d-m-Y") }}" data-format="dd-MM-yyyy" readonly/>
<span class="input-group-btn">
<button class="btn btn-default datepicker-invoker" type="button"><i class="icon-calendar"></i></button>
</span>
</div>
</div>
</div>
</form>

You use <div class="row"> whenever you start a section of cols for an example, lets say I have have 3 sections. The first row I require 12 columns. I wrap those twelve columns in a row I listed below an example counting to 12. The second I need 3 columns, In those columns lets say for an example I need a nav-menu, some text-content and an image, I will wrap the columns in a row. Same like the first two, the third column I need only a image and some content. I follow the same rules.
<div class="row">
<div class="col-md-1">one</div>
<div class="col-md-1">two</div>
<div class="col-md-1">three</div>
<div class="col-md-1">four</div>
<div class="col-md-1">five</div>
<div class="col-md-1">six</div>
<div class="col-md-1">seven</div>
<div class="col-md-1">eight</div>
<div class="col-md-1">nine</div>
<div class="col-md-1">ten</div>
<div class="col-md-1">eleven</div>
<div class="col-md-1">twelve</div>
</div>
<div class="row">
<div class="col-md-4">nav-menu</div>
<div class="col-md-4">content</div>
<div class="col-md-4">image</div>
</div>
<div class="row">
<div class="col-md-6">image</div>
<div class="col-md-6">content</div>
</div>

You do need the rows because if you don't follow the structure defined in the documentation-
<div class="container-fluid">
<div class="row">
<div class="col-md-12">form</div>
</div>
</div>
-the grid won't behave as expected. There are a couple ways of working around this, neither of them ideal.
You can wrap the form around the container and then create the whole grid structure inside that container putting your form groups inside the columns. You might have to set the form width to 100%. Another limitation is that your form groups need to be inside columns and can't wrap them. Therefore if you really need to control widths inside form groups then you need to create a new container inside the group. This is fairly easily managed if you wrap containers in columns with factors of 2, 4 or 6 then in is clear where the columns in your new container will align with the columns in the outer container.
Send your inputs using javascript. Then you don't need a form.

I think I can see the confusion. A form has many fields, on different rows, but you wouldn't necessarily use a Bootstrap "row" for each.
What we can do is use just one Bootstrap "row", and then put each label/field pair in its own div. Within that div the label and the field have their own divs, with Boostrap col- information. This will give us a form with many rows, and will give the desired wrapping effect you are expecting with Bootstrap.
The example below is an MVC form. Don't let the MVC syntax confuse you - you can replace the #Html.Label & Editor with HTML labels & input fields.
<div class="container">
<div class="row">
#using (Html.BeginForm("MyAction", "MyController", Model))
{
#Html.AntiForgeryToken()
<div class="form-group">
<div class="col-md-4">
#Html.LabelFor(model => model.PersonFirstName)
</div>
<div class="col-md-8">
#Html.EditorFor(model => model.PersonFirstName, new { htmlAttributes = new { #class = "form-control", placeholder = "Enter your first name" } })
#Html.ValidationMessageFor(model => model.PersonFirstName, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-4">
#Html.LabelFor(model => model.PersonSurname)
</div>
<div class="col-md-8">
#Html.EditorFor(model => model.PersonSurname, new { htmlAttributes = new { #class = "form-control", placeholder = "Enter your surname" } })
#Html.ValidationMessageFor(model => model.PersonSurname, "", new { #class = "text-danger" })
</div>
</div>
}
</div>
</div>

Related

Bootstrap column width adjustments

I am new to using Bootstrap. I tried adjusting a 3-coulmn row to fit, with the last column divided into another 3, but it overlaps and having difficulty in making it look right.
My cshtml for the 1st row:
<div class="row">
<div class="col-md-4 col-sm-4">
<strong>Last Name</strong> <span class="required">*</span>
#Html.TextBoxFor(m => m.Patient_Lastname, new { #class = "text-primary", required = "required" })
</div>
<div class="col-md-4 col-sm-4">
<strong>First Name</strong> <span class="required">*</span>
#Html.TextBoxFor(m => m.Patient_Firstname, new { #class = "text-primary", required = "required" })
</div>
<div class="col-md-1 col-sm-1">
<strong>birth</strong> <span class="required">*</span>
#Html.DropDownListFor(m => m.YearOfBirth, Enumerable.Range(1900, 119).Select(i => new SelectListItem { Value = i.ToString(), Text = i.ToString() }),
"--Year--", new { #class = "form-control", style = "width: 90px; height:30px;", required = "required" })
</div>
<div class="col-md-1 col-sm-1">
<strong>Month</strong> <span class="required">*</span>
#Html.DropDownListFor(m => m.YearOfBirth, Enumerable.Range(1, 12).Select(i => new SelectListItem { Value = i.ToString(), Text = i.ToString() }),
"--Month--", new { #class = "form-control", style = "width: 90px; height:30px;", required = "required" })
</div>
<div class="col-md-2 col-sm-2">
<strong>Day</strong> <span class="required">*</span>
#Html.DropDownListFor(m => m.YearOfBirth, Enumerable.Range(1, 31).Select(i => new SelectListItem { Value = i.ToString(), Text = i.ToString() }),
"--Day--", new { #class = "form-control", style = "width: 80px; height:30px;", required = "required" })
</div>
</div>
The 2nd and 3rd row are straight forward with col-md-4
Could you help me fix this ?
TIA
Ron.
I think the problem is the input filled width is bigger than available try resizing it.
There is no fixed layout you have to follow so that your screen will look nice automatically! It's your job to try and experiment what's the best to fit your need.
Obviously there is no way for col-sm-1 to fit a dropdown perfectly so you have to think of different layouts for different breakpoints. And bootstrap has many nice CSS classes you can use out of the box already for that purpose.
Extra Small Devices
For extra small devices, you might still be able to put year, month and day dropdown in a single row.
HTML Structure
<form>
<div class="form-row">
<div class="form-group">Last Name Label & Input</div>
<div class="form-group">First Name Label & Input</div>
<div class="form-group">
<div class="form-row">
<div class="form-group col">Year Label & Input</div>
<div class="form-group col">Month Label & Input</div>
<div class="form-group col">Day Label & Input</div>
</div>
</div>
</div>
</form>
I used .form-row here instead of .row because it gives you tighter gutters between columns.
.form-group is used to give each input group a nice margin-bottom.
Use of .col is to give you auto width.
How it looks like
Now you have a layout that works for extra small devices. You kind of start building up from this layout for each different devices with different screen sizes and add necessary CSS classes.
Small Devices
For small devices, you might want to put last name and first name input as 2 columns in a row.
HTML Structure
<form>
<div class="form-row">
<div class="form-group col-sm-6">Last Name Label & Input</div>
<div class="form-group col-sm-6">First Name Label & Input</div>
<div class="form-group col-sm-12">
<div class="form-row">
<div class="form-group col">Year Label & Input</div>
<div class="form-group col">Month Label & Input</div>
<div class="form-group col">Day Label & Input</div>
</div>
</div>
</div>
</form>
See those .col-sm-* classes are added?
How it looks like
...
...
...
Extra Large Devices
HTML Structure
<form>
<div class="form-row">
<div class="form-group col-sm-6 col-xl-4">Last Name Label & Input</div>
<div class="form-group col-sm-6 col-xl-4">First Name Label & Input</div>
<div class="form-group col-sm-12 col-xl-4">
<div class="form-row">
<div class="form-group col">Year Label & Input</div>
<div class="form-group col">Month Label & Input</div>
<div class="form-group col">Day Label & Input</div>
</div>
</div>
</div>
</form>
How it looks like

Bootstrap input labels on left of input (rather than on top)

I am fairly new to Bootstrap and I'm trying to make a simple form, all elements of which are in a single line on the screen. My html code for the form looks something like this:
<form class="form-horizontal">
<fieldset>
<div class="form-container">
<div class="form-group">
<label>Search by:</label>
<input class="form-control" type="text">
</div>
<div class="form-group">
<label>Something:</label>
<input class="form-control" type="text">
</div>
</div>
</fieldset>
</form>
By default, the labels for my input fields appear about the input fields. I want them to be on the left side of the input fields. I've googled and checked on here how to implement this, but none of the solutions I found have worked (I tried adding classes to my form and changing their CSS, and tried using bootstrap's form-inline functionality).
Does anyone know how I could make the form look how I want it to look?
You will want to make two modifications to each form group in order to utilize the bootstrap grid system.
not sure which version of BS you are using but for grid it is the same concept: https://v4-alpha.getbootstrap.com/layout/grid/
You will want to use bootstrap grid system to define that the label and input on are the same line. Keep in mind that there are 12 columns in 1 row so items you want to be on the same line must add up to 12.
Add the following classes to your label: "col-lg-2 col-md-2 col-sm-2 control-label"
Wrap your input in a div and add the following classes: "col-lg-10 col-md-10 col-sm-10"
You will notice that between these two elements, the columns add up to 12 so in the bootstrap grid system, they will be on the same line.
(I also wrapped this in a container just for a more tidy display on this post)
<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.7/js/bootstrap.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<div class="container">
<form class="form-horizontal">
<fieldset>
<div class="form-container">
<div class="form-group">
<label class="col-lg-2 col-md-2 col-sm-2 col-xs-2 control-label">Search by:</label>
<div class="col-lg-4 col-md-4 col-sm-4 col-xs-4">
<input class="form-control" type="text">
</div>
<label class="col-lg-2 col-md-2 col-sm-2 col-xs-2 control-label">Something:</label>
<div class="col-lg-4 col-md-4 col-sm-4 col-xs-4">
<input class="form-control" type="text">
</div>
</div>
</div>
</fieldset>
</form>
</div>

Bootstrap col-md-x nested inside col-md-x

I suppose it's wrong to nest a col-md-x immediately inside a col-md-x in Bootstrap 3. Am I right?
I have following at the moment:
<link href="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.2/css/bootstrap.css" rel="stylesheet"/>
<div class="row">
<input class="col-md-4" value="something" />
<div class="col-md-8">Something here</div>
</div>
In this case, the input border starts at the very beginning of the row. It doesn't have any padding on the outside of it.
I would like the input to show 15px away from the row border. The way I know of how to achieve this is to put this input inside a col-md-x. However, could this cause any issues?
From the bootstrap docs:
To nest your content with the default grid, add a new .row and set of
.col-sm-* columns within an existing .col-sm-* column.
So as long as you are nesting within child rows, you are gonna be fine. Another option would be custom css rules and ids for your nested structure to achieve the desired padding or margin.
UPDATE
To refer to your comment, since this is about validation states: let me add that bootstrap already offers great validation-highlighting. See this quick sample. The bootstrap docs on forms offer great documentation on this topic. As for the padding: I like to put most of my "not-inline" forms into a .well, which shows the user, where action is required and allows a consistent styling of forms...
var resetSec = function(){
$('#sth-form-section').removeClass('has-error');
$('#sth-form-section').removeClass('has-warning');
$('#sth-form-section').removeClass('has-success');
$('#helpBlock-sth').addClass('sr-only');
$('#helpBlock-sth').html('');
};
$('#invalid').click(function(){
resetSec();
$('#sth-form-section').addClass('has-error');
$('#helpBlock-sth').removeClass('sr-only');
$('#helpBlock-sth').html('Oh snap! Better think about this one more time!');
});
$('#valid').click(function(){
resetSec();
$('#sth-form-section').addClass('has-success');
$('#helpBlock-sth').removeClass('sr-only');
$('#helpBlock-sth').html('Well done! I think your input was the best so far!');
});
$('#reset').click(function(){
resetSec();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script>
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet"/>
<div class="well">
<form class="form-horizontal" role="form">
<div id="sth-form-section" class="form-group">
<label for="something" class="col-lg-2 control-label">Something:</label>
<div class="col-lg-10">
<input type="text" class="form-control" id="something" placeholder="Something here">
<span id="helpBlock-sth" class="help-block sr-only"></span>
</div>
</div>
<div class="form-group">
<div class="col-lg-offset-2 col-lg-10">
<button type="submit" class="btn btn-default">Submit</button>
</div>
</div>
</form>
</div>
<button class="btn btn-primary" id="reset">Reset</button>
<button class="btn btn-danger" id="invalid">Invalid</button>
<button class="btn btn-success" id="valid">Valid</button>
Ah, just write the markup a little differently, like so:
<div class="row">
<div class="col-md-4">
<input class="form-control" value="something" />
</div>
<div class="col-md-8">
<p class="form-control-static">Something here</p>
</div>
</div>
The problem was putting the col-md-4 class on the input.

bootstrap form-group vs row CSS which one prefer

I have used row and now using form-group CSS.
I am confused between these 2, which one should I prefer if I want to build form controls. Seems like both do same job.
I usually follow this pattern:
<div class="form-group">
<div class="row">
<div class="col-md-6"></div>
<div class="col-md-6"></div>
</div>
</div>
Note the col-md-6 and col-md-5 are examples and you can use any col-md-x class with unlimited number. just sum MUST be 12.
The direct answer to your question is in the documentation:
".form-row" is a variation of our standard grid class ".row" which overrides the
default column gutters for tighter and more compact layouts
See here: https://getbootstrap.com/docs/4.0/components/forms/#form-grid
This is for Bootstrap.
form-group per set of controls on a form. If your row has multiple cols, and each col has a label and an input text, put a form-group around each label and input control, such as:
<div class="row">
<div class="col-6">
<div class="form-group">
<label>Client Name: </label>
<input type="text" name="clientName" />
</div>
</div>
<div class="col-6">
<div class="form-group">
<label>Client Email: </label>
<input type="text" name="clientEmail />
</div>
</div>
</div>
... etc.

Cannot change the size of text box

Im using the following code and I cannot change the size of the text box to make it bigger than 700,how can I change it to 1000?
<div class="form-group">
#Html.LabelFor(model => model.Service, new { #class = "col-md-2 oper-label" })
<div class="col-md-10">
#Html.TextBoxFor(model => model.Service, new { #style = "width: 700px;" })
</div>
</div>
</div>
I am not familiar with the language that you are using, but if you just use an HTML <textarea> it will work.
Bootply example
<div class="form-group">
<div class="col-md-10">
<textarea class="form-control" style="width: 700px;"></textarea>
</div>
</div>
This code will produce a <textarea> that is 700 pixels wide like this:
Adjust the width to change the width of the textarea. Another option would be to do this, which produces a textarea that will be approximately 80% of the browser width:
<div class="form-group">
<div class="col-md-10">
<textarea class="form-control"></textarea>
</div>
</div>
You cannot just change class="col-md-10" to class="col-md-11" (or col-md-12) or simply remove <div class="col-md-10"> ?
Check this link out for more information about bootstrap grid system : http://getbootstrap.com/css/#grid-example-basic

Resources