Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bootstrap row class contains margin-left and margin-right which creates problems

I am using Bootstrap 'row' class to align divs one on top of another, which works fine but

.row {    margin-left: -15px;    margin-right: -15px;  } 

What I noticed is that it specifies margin-left and margin-right attributes to be -13px because of which my contents get shifted towards left. so what I have done is added another class as follows :

.row-no-margin {    margin-left: 0px;    margin-right: 0px; }  

This solves the purpose, but I would still like to know if there is any specific reason for 'margin-left: -15px;'. And what is the best approach to solve my problem.

like image 843
Tarun Gupta Avatar asked Apr 18 '14 11:04

Tarun Gupta


People also ask

Why do Bootstrap rows have negative margin?

Rows have a negative left/right margin of -15px. The Container padding of 15px is used to counteract the negative margins of the Row. This is to keep content evenly aligned on the edges of the layout. If you don't put a Row in a Container, the Row will overflow it's container, causing an undesirable horizontal scroll.

Why margin-right is not working on table?

margin-right will not work if you set width to 100%. What you could do is : wrap table in a div tag.

How do I add a margin to a bootstrap row?

If you need to separate rows in bootstrap, you can simply use . form-group . This adds 15px margin to the bottom of row.

What does row class do in bootstrap?

In Bootstrap, the "row" class is used mainly to hold columns in it. Bootstrap divides each row into a grid of 12 virtual columns. In the following example, the col-md-6 div will have the width of 6/12 of the "row"s div, meaning 50%.


2 Answers

The .row is meant to be used inside a container. Since the container has padding to adjust the negative margin in the .row, grid columns used inside the .row can then adjust to the full width of the container. See the Bootstrap docs: http://getbootstrap.com/css/#grid

Here's an example to illustrate: https://codeply.com/p/zHFaC7JR5K

So, a better solution may for you to place your .row inside a .container or .container-fluid

like image 78
Zim Avatar answered Sep 23 '22 11:09

Zim


You can use row-fluid instead of row, then you won't have this problem. (for previous versions of bootstrap)

I am not sure of recent versions 3, any way :

The issue is that the first column should not have half a gutter on the left, and the last should not have half a gutter on the right. Rather than use some sort of .first or .last class on those columns as some grid systems do, they instead set the .row class to have negative margins that match the padding of the columns. This "pulls" the gutters off of the first and last columns, while at the same time making it wider.

For more information on this Why does the bootstrap .row has a default margin-left of -30px?

like image 28
4dgaurav Avatar answered Sep 21 '22 11:09

4dgaurav