4

I'm want to know why not just use display:inline-block all of the time INSTEAD of float:left. Inline-block seems to be much easier to control in terms of layout and not having issues with having to clear floats etc. I'm trying to get my head around why use one instead of the other.

Many thanks,

Emily.

Temani Afif
  • 245,468
  • 26
  • 309
  • 415
pjk_ok
  • 618
  • 7
  • 35
  • 90
  • 1
    Possible duplicate of [Difference Between 'display: block; float: left' vs. 'display: inline-block; float: left'?](http://stackoverflow.com/questions/27511533/difference-between-display-block-float-left-vs-display-inline-block-flo) – Ruhul Amin Mar 11 '17 at 23:31
  • 1
    This is not a duplicate. The example you gave and other similar questions are about using inline-block / block with float. Mine is about using inline-block in a parent element INSTEAD OF floating elements. Please remove this as a duplicate question. – pjk_ok Mar 12 '17 at 00:03
  • See this question: http://stackoverflow.com/q/39368992/5641669 – Johannes Mar 12 '17 at 00:06

2 Answers2

6

The purpose of float is to allow text to wrap around it. So it's moved to the left or right side and taken out of the page flow. Line boxes containing the other text then avoid overlapping with the floated element. It forms a block-level, block container. It is not vertically aligned with any other content.

body {
  width:300px; 
}
.float-example span {
  float:left;
  width: 100px;
  border:2px dashed red;
}
<section class="float-example">Lorem ipsum dolor sit amet, consectetur 
adipiscing elit, <span>I like to use float!</span> sed do eiusmod tempor
incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis
nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</section>

The purpose on inline-block is to be a block container that sits inside a line box. It forms an inline-level, block container within the normal flow of the content. It's vertically aligned with the other content of the line box it is in.

body {
  width:300px; 
}

.inline-block-example span {
  display:inline-block;
  width: 100px;
  border:2px dashed red;
}
<section class="inline-block-example">Lorem ipsum dolor sit amet, consectetur
adipiscing elit, <span>I like to use inline-block!</span> sed do eiusmod tempor
incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis
nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
</section>
Alohci
  • 78,296
  • 16
  • 112
  • 156
3

The biggest difference is that you dont need to clear inline elements like you do floats.

Float removes an element from the normal DOM flow - allowing content to wrap it. This also means that you need to clear the float on the next object in the markup in order to break out of the float condition.

Inline-block does not require you to clear.

Korgrue
  • 3,430
  • 1
  • 13
  • 20