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

HtmlCssTwitter BootstrapTwitter Bootstrap-3

Html Problem Overview


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.

Html Solutions


Solution 1 - Html

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

Solution 2 - Html

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 https://stackoverflow.com/questions/16699672/why-does-the-bootstrap-row-has-a-default-margin-left-of-30px

Solution 3 - Html

Are you using Bootstrap 3? My version of the css has -15px, not -13px. In any case, I've simply done what you've down, and overwritten the style. I believe it's because the .container class has a 15px padding on the left and right, and this negative margin on the rows will pull that content back out to the edge of the container.

Solution 4 - Html

Old topic, but I think I found another descent solution. Adding class="row" to a div will result in this CSS configuration:

.row {
    display: -webkit-box;
    display: flex;
    flex-wrap: wrap;
    margin-right: -15px;
    margin-left: -15px;
}

We want to keep the first 3 rules and we can do this with class="d-flex flex-wrap" (see https://getbootstrap.com/docs/4.1/utilities/flex/):

.flex-wrap {
    flex-wrap: wrap !important;
}
.d-flex {
    display: -webkit-box !important;
    display: flex !important;
}

It adds !important rules though but it shouldn't be a problem in most cases...

Solution 5 - Html

Here is your simple and easy answer

Go to your class where you want to give a negative margin then copy and paste this inside the class.

Example for negative margin top

mt-n3

Example for negative margin bottom

mb-n2

Solution 6 - Html

Old topic, but I was recently affected by this.

Using a class "row-fluid" instead of "row" worked fine for me but I'm not sure if it's fully supported going forward.

So after reading Why does the bootstrap .row has a default margin-left of -30px I just used the <div> (without any row class) and it behaved exactly like <div class="row-fluid">

Solution 7 - Html

I was facing this same issue and initially, I removed the row's right and left -ve margin and it removed the horizontal scroll, but it wasn't good. Then after 45 minutes of inspecting and searching, I found out that I was using container-fluid and was removing the padding and its inner row had left and right negative margins. So I gave container-fluid it's padding back and everything went back to normal.

If you do need to remove container-fluid padding, don't just remove every row's left and right negative margin in your project instead introduce a class and use that on your desired container

Solution 8 - Html

To remove the margins on all rows:

.row {
    margin: 0px !important;
}

Solution 9 - Html

Any row that you create, wrap each row inside a container div to solve the problem.

<!-- row 1 -->

<div class="container">
<div class="row"></div>
</div>

 <!-- row 2 -->

<div class="container">
<div class="row"></div>
</div>

Solution 10 - Html

I used div class="form-control" instead of div class="row"

That fixed for me.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTarun GuptaView Question on Stackoverflow
Solution 1 - HtmlZimView Answer on Stackoverflow
Solution 2 - Html4dgauravView Answer on Stackoverflow
Solution 3 - HtmlJesseEarleyView Answer on Stackoverflow
Solution 4 - HtmlFranck WolffView Answer on Stackoverflow
Solution 5 - HtmlMuktaView Answer on Stackoverflow
Solution 6 - HtmlShhTotView Answer on Stackoverflow
Solution 7 - HtmlJunaidView Answer on Stackoverflow
Solution 8 - HtmlSasinoView Answer on Stackoverflow
Solution 9 - HtmlBaraja SwargiaryView Answer on Stackoverflow
Solution 10 - HtmlVkl125View Answer on Stackoverflow