-1

Check the code below :

Why is there spaces beetwen the divs ? How can I remove them (without using float) ?

* {
  padding: 0px;
  margin: 0px;
}
.section {
  display: inline-block;
  width: 20%;
  min-height: 50px;
  box-sizing: border-box;
  border: solid 1px;
  /*float: left;*/
}
<div id="container">
  <div class="section">a</div>
  <div class="section">b</div>
  <div class="section">c</div>
  <div class="section">d</div>
  <div class="section">e</div>
</div>
Dionys
  • 3,083
  • 1
  • 19
  • 28

4 Answers4

1

There are a few ways to solve this. My favourite is:

* {
  padding: 0px;
  margin: 0px;
}

#container {
  letter-spacing: -0.32em;
}

.section {
  display: inline-block;
  width: 20%;
  min-height: 50px;
  letter-spacing: normal;
  box-sizing: border-box;
  border: solid 1px;
  /*float: left;*/
}

Here is a fiddle

David Jones
  • 4,275
  • 6
  • 27
  • 51
1

I believe the problem is that the divs are spaced by a line break, which is interpreted as a space in the resulting HTML.

Try changing it to:

<div id="container">
  <div class="section">a</div><div class="section">b</div><div class="section">c</div><div class="section">d</div><div class="section">e</div>
</div>

If you want to preserve some white space, simply include it in comments:

<div id="container">
     <div class="section">a</div><!--
  --><div class="section">b</div><!--
  --><div class="section">c</div><!--
  --><div class="section">d</div><!--
  --><div class="section">e</div>
</div>

Lastly, you can change display: inline-block; to display: table-cell, but then you will need to re-work the section width.

lpytel
  • 105
  • 1
  • 9
0

Best answer is @emmanuel one (see question comment). Even if there is no text, spaces beetwen divs does count.

* {
  padding: 0px;
  margin: 0px;
}
.section {
  display: inline-block;
  width: 20%;
  min-height: 50px;
  box-sizing: border-box;
  border: solid 1px;
  /*float: left;*/
}
<div id="container">
  <div class="section">a</div><!--
  --><div class="section">b</div><!--
  --><div class="section">c</div><!--
  --><div class="section">d</div><!--
  --><div class="section">e</div>
</div>
Dionys
  • 3,083
  • 1
  • 19
  • 28
0

Try this:-

<div id="container">
  <div class="section">a</div><div class="section">b</div><div class="section">c</div><div class="section">d</div><div class="section">e</div>
</div>

Demo

Mukul Kant
  • 7,074
  • 5
  • 38
  • 53