Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do form-groups have a negative margin in Bootstrap?

Tags:

I have this very simple code using Bootstrap 3:

<html>   <body>     <main class="container" role="main">       <form class="simple_form form-horizontal">          <div class="form-group text required campaign_url">            <label class="text required control-label" for="campaign_url"><abbr title="required">*</abbr> Url</label>            <textarea class="text required form-control" name="campaign[url]" id="campaign_url"></textarea>          </div> 

and it appears like this:

enter image description here

Notice how tho labels and the inputs are sticking to the left. Inspecting those elements I found this:

.form-horizontal .form-group {   margin-left: -15px;   margin-right: -15px; } 

Why is that there? I know it's trivial to remove, but it makes me wonder whether the way I'm using Bootstrap is wrong. How should I use it?

like image 402
pupeno Avatar asked Apr 13 '15 22:04

pupeno


People also ask

Why does bootstrap use negative margins?

The Bootstrap . row uses negative margins (-15px) to counteract the padding (15px) of the container. The end result is no visual spacing (margin or padding) on the sides of the row within the container.

How do I set margins in bootstrap?

l - sets margin-left or padding-left. r - sets margin-right or padding-right. x - sets both padding-left and padding-right or margin-left and margin-right. y - sets both padding-top and padding-bottom or margin-top and margin-bottom.

What is form group row in bootstrap?

The .form-group class is the easiest way to add some structure to forms. It provides a flexible class that encourages proper grouping of labels, controls, optional help text, and form validation messaging. By default it only applies margin-bottom , but it picks up additional styles in .form-inline as needed.

How do I remove spaces between bootstrap rows?

If you want to use bootstrap to control line spacing it is recommended to use the margin and padding settings in bootstrap. i.e. my-0 and py-0.


1 Answers

It's happening because you are using form-horizontal which is meant to be used as a row along with col-*'s for layout. From the Bootstrap docs:

Use Bootstrap's predefined grid classes to align labels and groups of form controls in a horizontal layout by adding .form-horizontal to the form (which doesn't have to be a <form>). Doing so changes .form-groups to behave as grid rows, so no need for .row.

So if you simply remove the form-horizontal the negative margin goes away.

http://codeply.com/go/QQnqgfKv9v

like image 119
Zim Avatar answered Sep 24 '22 18:09

Zim