0

Why does the last green div overflow if I change the width of the blue div from 212px to 211px? Where do the spaces between the green divs vome from?

The developer tools show me no margin, no padding, no border, nothing at all which could produce these gaps. Where do they come from?

212px width --> no overflow

212px width --> no overflow

211px width --> overflow

211px width --> overflow

#test {
  background-color: blue;
  height: 50px;
  width: 211px;
}

#test>div {
  display: inline-block;
  background-color: green;
  height: 50px;
  width: 50px;
}
<div id="test">
  <div></div>
  <div></div>
  <div></div>
  <div></div>
</div>
j08691
  • 204,283
  • 31
  • 260
  • 272
Michael Palm
  • 337
  • 5
  • 16

1 Answers1

2

You turned your divs into inline elements, and inline elements are sensitive to the whitespace in your code and take up space. Remove the whitespace and the gaps go away:

#test {
  background-color: blue;
  height: 50px;
  width: 211px;
}

#test>div {
  display: inline-block;
  background-color: green;
  height: 50px;
  width: 50px;
}
<div id="test">
  <div></div><div></div><div></div><div></div>
</div>
j08691
  • 204,283
  • 31
  • 260
  • 272