1

As shown in the example, the two of 150px divs won't stay in the 300px parent div even if I set them as inline-blocks.

Why would they occupy extra space?

There is no problem when using float:left.

http://jsfiddle.net/r2LLzwbu/1/

leetom
  • 723
  • 1
  • 6
  • 27
  • 4
    possible duplicate of [How to remove the space between inline-block elements?](http://stackoverflow.com/questions/5078239/how-to-remove-the-space-between-inline-block-elements) – Hidden Hobbes Sep 25 '15 at 07:30
  • You will find your answer in the link above. For future reference please include your code in the question itself. A JSFiddle alone is not suitable as the link may die making the question useless. – Hidden Hobbes Sep 25 '15 at 07:33
  • @HiddenHobbes Thank you for your kind remind. – leetom Sep 25 '15 at 07:49

3 Answers3

1

The problem is the indentation causes extra spaces to appear in the div. To illustrate, if you change the HTML to a single line:

<div class="outer"><div class="inner-1"></div><div class="inner-2"></div></div>

it works. Updated Fiddle.

Renzo Poddighe
  • 338
  • 2
  • 12
1

Chris Coyier explains this very well in one of his post here -

Fighting the Space Between Inline Block Elements

To summarize what Chris said -

The inline blocks are similar to independent words in a sentence. Inline-block carries the space in between them the way words do in a sentence.

Ravi Khandelwal
  • 718
  • 1
  • 5
  • 15
0

you can do this :

.inner-1{
    background:green;
    width:150px;
    height:100px;
    margin:0;border:0;padding:0;

}

Remove .inner-1 css display:inline-block;

DEMO

Ivin Raj
  • 3,448
  • 2
  • 28
  • 65
  • I think you may have misunderstood the question. The OP is asking why `inline-block` forces the blocks onto separate lines. – Hidden Hobbes Sep 25 '15 at 07:51