1

my code is as follows:

html

<div class="wrapper">
  <div class="first"></div>
  <div class="second"></div>
  <div class="third"></div>
</div>

css

.wrapper > div {
  display: inline-block;
}
.wrapper .first {
  width: 33.3%;
}
.wrapper .second {
  width: 33.3%;
}
.wrapper .third {
  width: 33.3%;
}

I use the above code, but it end up breaking into two line. I wonder what's wrong with it?
I know i can use float to do this. but the above code is haulting me for a long time . and hope someone can tell me the reason,thanks in advance.

cimmanon
  • 67,211
  • 17
  • 165
  • 171
Frank Lee
  • 161
  • 10
  • It's because inline elements [respect the white-space](http://stackoverflow.com/questions/19038799/why-is-there-an-unexplainable-gap-between-these-inline-block-div-elements/19038859#19038859) in the markup. Remove the line breaks or follow one of the alternates in the link. – Josh Crozier Dec 08 '15 at 14:02

1 Answers1

2

Remove the spacing between the divs, I would use HTML comments for this to maintain the readability of your HTML.

.wrapper > div {
  width: 33.33%;
  display: inline-block;
  height: 80px;
}
.first {
  background: red;
}
.second {
  background: yellow;
}
.third {
  background: blue;
}
<div class="wrapper">
  <div class="first"></div><!--
  --><div class="second"></div><!--
  --><div class="third"></div>
</div>
Aaron
  • 10,187
  • 3
  • 23
  • 39