Andrius Andrius - 5 months ago 22
CSS Question

CSS - completely remove spacing between columns?

I have this HTML:

<div class="container">
<div class="box center-block">
<div class="row">
<div class="col-md-4 col-xs-4">
<div class="thumbnail">A</div>
</div>
<div class="col-md-4 col-xs-4">
<div class="thumbnail">A</div>
</div>
<div class="col-md-4 col-xs-4">
<div class="thumbnail">A</div>
</div>
</div>
<div class="row">
<div class="col-md-4 col-xs-4">
<div class="thumbnail">A</div>
</div>
<div class="col-md-4 col-xs-4">
<div class="thumbnail">A</div>
</div>
<div class="col-md-4 col-xs-4">
<div class="thumbnail">A</div>
</div>
</div>
<div class="row">
<div class="col-md-4 col-xs-4">
<div class="thumbnail">A</div>
</div>
<div class="col-md-4 col-xs-4">
<div class="thumbnail">A</div>
</div>
<div class="col-md-4 col-xs-4">
<div class="thumbnail">A</div>
</div>
</div>
</div>
</div>


And CSS:

.box {
width: 300px;
padding-top: 100px;
}
.row > div > div {
width: 100px;
height: 100px;
margin: 0px;
}


I'm also using bootsrap (version 3).

I was able to remove margin between rows, but can't remove margin between columns. Is there a way to force it somehow?
I need all "squares" next to each other without any margins (both between rows and columns).

You can see how it looks here: https://jsfiddle.net/a91zujkj/

Answer Source

If you really want to use col- class, make sure to set the .thumbnail elements width to 100% and use a no-padding class on the col- elements.

Here is an example:

@import url('https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css');

.box {
  width: 300px;
  padding-top: 100px;
}
.no-padding {
  padding-left: 0;
  padding-right: 0;
}
.thumbnail {
  width: 100%;
  height: 100px;
  margin: 0;
}
<div class="container">
    <div class="box center-block">
        <div class="row">
            <div class="col-md-4 col-xs-4 no-padding">
                <div class="thumbnail">A</div>
            </div>
            <div class="col-md-4 col-xs-4 no-padding">
                <div class="thumbnail">A</div>
            </div>
            <div class="col-md-4 col-xs-4 no-padding">
                <div class="thumbnail">A</div>
            </div>
        </div>
        <div class="row">
            <div class="col-md-4 col-xs-4 no-padding">
                <div class="thumbnail">A</div>
            </div>
            <div class="col-md-4 col-xs-4 no-padding">
                <div class="thumbnail">A</div>
            </div>
            <div class="col-md-4 col-xs-4 no-padding">
                <div class="thumbnail">A</div>
            </div>
        </div>
        <div class="row">
            <div class="col-md-4 col-xs-4 no-padding">
                <div class="thumbnail">A</div>
            </div>
            <div class="col-md-4 col-xs-4 no-padding">
                <div class="thumbnail">A</div>
            </div>
            <div class="col-md-4 col-xs-4 no-padding">
                <div class="thumbnail">A</div>
            </div>
        </div>
    </div>
</div>

You actually do not need to use col- classes at all and floats are not needed either. Another way to do this is using display: inline-block.

Here is how that looks:

@import url('https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css');
.wrapper {
  width: 300px;
  padding-top: 100px;
  margin: 0 auto;
  font-size: 0;
}

.item {
  border: 1px solid #eee;
  width: 100px;
  height: 100px;
  display: inline-block;
  font-size: initial;
}
<div class="wrapper">
  <div class="item">A</div>
  <div class="item">A</div>
  <div class="item">A</div>
  <div class="item">A</div>
  <div class="item">A</div>
  <div class="item">A</div>
  <div class="item">A</div>
  <div class="item">A</div>
  <div class="item">A</div>
</div>

And if you're really brave you could also use CSS Grid layout. ;)

CSS Grid Layout

CSS Grid layout excels at dividing a page into major regions, or defining the relationship in terms of size, position, and layer, between parts of a control built from HTML primitives.

Here is how that goes:

.wrapper {
  padding-top: 100px;
  width: 300px;
  margin: 0 auto;
  display: grid;
  grid-template-columns: repeat(3,100px);
  grid-template-rows: repeat(3,100px);
}

.item {
  border: 1px solid #ddd;
  height: 100px;
  width: 100px;
}
<div class="wrapper">
  <div class="item">A</div>
  <div class="item">A</div>
  <div class="item">A</div>
  <div class="item">A</div>
  <div class="item">A</div>
  <div class="item">A</div>
  <div class="item">A</div>
  <div class="item">A</div>
  <div class="item">A</div>
</div>