49

I want to remove the space between multiple columns in the Bootstrap grid.

How can we overwrite Bootstrap's CSS to achieve this task or any other better solution with pure CSS?

main.html

<div class="row">
  <div class="col-md-2">
    // stuff here for this column
  </div>
  <div class="col-md-10">
    // stuff here for columns
  </div>
</div>
Alexander Abakumov
  • 13,617
  • 16
  • 88
  • 129
hussain
  • 6,587
  • 18
  • 79
  • 152
  • Bootstrap 3 introduced `row-no-gutters` in v3.4.0 https://getbootstrap.com/docs/3.4/css/#grid-remove-gutters See also https://stackoverflow.com/questions/21254889/bootstrap-how-to-remove-the-gutter-between-columns (possible duplicate) – dyve Mar 08 '19 at 13:49

11 Answers11

73

In Bootstrap 5, the no-gutters class has changed to g-0

In Bootstrap 4, the no-gutters class is included. No extra CSS is needed...

<div class="row no-gutters">
  <div class="col-md-2">
    // stuff here for this column
  </div>
  <div class="col-md-10">
    // stuff here for columns
  </div>
</div>
Carol Skelly
  • 351,302
  • 90
  • 710
  • 624
66

add a class when you need to remove spaces use it

.padding-0{
    padding-right:0;
    padding-left:0;
}

in html write this class

<div class="row">
   <div class="col-md-2 padding-0">
       //stuff here for this column
   </div>
   <div class="col-md-10 padding-0">
       //stuff here for columns
   </div>
</div>
Morteza Negahi
  • 3,305
  • 8
  • 24
  • 42
38

In the Bootstrap 4 you can add class mx-* and px-*

<div class="row mx-0">
    <div class="col-md-6 px-0">
        <p>Your content</p>
    </div>
    <div class="col-md-6 px-0">
        <p>Your content</p>
    </div>
</div>

The classes of mx-* and px-* is in default bootstrap style, so you don't have to add it manually

mx-* stand for margin-right: * plus margin-left: *. While px-* stands for padding right and left.

There is also my-* m-* mt-* mb-* mr-* ml-* and py-* p-* pt-* pb-* pr-* pl-*.

Edit:

Bootstrap 5 used ps-* pe-* ms-* me-* and dropped pl-* pr-* ml-* mr-*. It uses "start" and "end" instead of "left" and "right".

vozaldi
  • 1,014
  • 9
  • 18
13

You can create a class no-gutter for your row and remove the margin/padding that's added by bootstrap, like suggested in this post:

.row.no-gutter {
  margin-left: 0;
  margin-right: 0;
}
.row.no-gutter [class*='col-']:not(:first-child),
.row.no-gutter [class*='col-']:not(:last-child) {
  padding-right: 0;
  padding-left: 0;
}
.row > div {
  background: lightgrey;
  border: 1px solid;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<div class="row no-gutter">
  <div class="col-md-2">
    1
  </div>
  <div class="col-md-10">
    2
  </div>
</div>

I've set a gray background and a border for demonstration purposes. Watch the result in fullscreen to see, that there is no space anymore between the columns. More information about the predefined margin and padding in Bootstrap can be found here.

Community
  • 1
  • 1
andreas
  • 16,357
  • 12
  • 72
  • 76
  • 1
    I found this the best solution. Thanks! – Omar Mneimneh Jun 14 '19 at 16:10
  • This solution seems close to answering the question, but I think the author was most concerned about the "internal" space between columns and this removes padding everywhere. I would suggest a modification to your selector to pick columns except the first and last: .row.no-internal-gutter [class*='col-']:not(:first-child):not(:last-child). – Ryan B Jul 23 '21 at 06:10
4

As of BootStrap 5, here is how to remove gutter https://getbootstrap.com/docs/5.0/layout/gutters/

<div class="row g-0">
  <div class="col-sm-6 col-md-8">.col-sm-6 .col-md-8</div>
  <div class="col-6 col-md-4">.col-6 .col-md-4</div>
</div>
Mozes Ong
  • 1,204
  • 1
  • 11
  • 20
1

I would add a class to the row and target the column children with a descendant wildcard selector:

HTML:

<div class="row nopadding">
    <div class="col-md-2">
        //stuff here for this column
    </div>
    <div class="col-md-10">
        //stuff here for columns
    </div>
</div>

CSS:

.nopadding > div[class^="col-"] {
    padding-right: 0;
    padding-left: 0;
}
David Wilkinson
  • 5,060
  • 1
  • 18
  • 32
  • Still, the columns will go out of the row. Add an additional css : .nopadding{margin-left: 0px;margin-right: 0px;} to keep the columns inside the row. – sudip Mar 06 '18 at 16:33
1

If you are using LESS you can use snippet and apply it to the row.

.no-gutter {
   [class*="col-"] {
       padding-left: 0 !important;
       padding-right: 0 !important;
   }
}

If normal CSS

.no-gutter > [class*="col-"]{
}

Like so:

<div class="row no-gutter">
  <div class="col-md-2">
   //stuff here for this column
  </div>
  <div class="col-md-10">
   //stuff here for columns
  </div>
</div>
Andrew Birks
  • 792
  • 9
  • 26
1

On the columns there is a 15px padding on either side, if you remove that they should sit together.

.col-lg-1, .col-lg-10, .col-lg-11, .col-lg-12, .col-lg-2, .col-lg-3, .col-lg-4, .col-lg-5, .col-lg-6, .col-lg-7, .col-lg-8, .col-lg-9, .col-md-1, .col-md-10, .col-md-11, .col-md-12, .col-md-2, .col-md-3, .col-md-4, .col-md-5, .col-md-6, .col-md-7, .col-md-8, .col-md-9, .col-sm-1, .col-sm-10, .col-sm-11, .col-sm-12, .col-sm-2, .col-sm-3, .col-sm-4, .col-sm-5, .col-sm-6, .col-sm-7, .col-sm-8, .col-sm-9, .col-xs-1, .col-xs-10, .col-xs-11, .col-xs-12, .col-xs-2, .col-xs-3, .col-xs-4, .col-xs-5, .col-xs-6, .col-xs-7, .col-xs-8, .col-xs-9 {
    padding-right: 0px;
    padding-left: 0px;
}

This snippet is from modifed code found in grid-framework.less.

andreas
  • 16,357
  • 12
  • 72
  • 76
Ginger Squirrel
  • 663
  • 7
  • 22
1

Simple add .no-gutter on row.

https://getbootstrap.com/docs/4.0/layout/grid/#how-it-works

0

The solution by Anshuman actually works! It gets rid of the bootstrap column spaces when you set the padding to zero in the styles, i.e padding: 0;

<div class = "row">
  <div class = "col-sm-3" style= "padding:0;">
    //html code here..
  </div>
  <div class = "col-sm-3" style= "padding:0;">
    //html code here..
  </div>
</div>
0

Bootstrap 5 replace no-gutters class has changed to g-0

Abd Abughazaleh
  • 4,615
  • 3
  • 44
  • 53