CSS display: inline vs inline-block

Css

Css Problem Overview


In CSS, display can have values of inline and inline-block. Can anyone explain in detail the difference between inline and inline-block?

I searched everywhere, the most detailed explanation tells me inline-block is placed as inline, but behaves like block. But it does not explain what exactly "behave as a block" means. Is it any special feature?

An example would be an even better answer. Thanks.

Css Solutions


Solution 1 - Css

Inline elements:

  1. respect left & right margins and padding, but not top & bottom
  2. cannot have a width and height set
  3. allow other elements to sit to their left and right.
  4. see very important side notes on this [here][1].

Block elements:

  1. respect all of those
  2. force a line break after the block element
  3. acquires full-width if width not defined

Inline-block elements:

  1. allow other elements to sit to their left and right
  2. respect top & bottom margins and padding
  3. respect height and width

From [W3Schools][2]:

> * An inline element has no line break before or after it, and it tolerates HTML elements next to it. > > * A block element has some whitespace above and below it and does not tolerate any HTML elements next to it. > > * An inline-block element is placed as an inline element (on the same line as adjacent content), but it behaves as a block element.

When you visualize this, it looks like this:

![CSS block vs inline vs inline-block][3]

The image is taken from [this page][4], which also talks some more about this subject.

[1]: https://hacks.mozilla.org/2015/03/understanding-inline-box-model/ "mozzila docs" [2]: http://www.w3schools.com/cssref/pr_class_display.asp [3]: http://i.stack.imgur.com/mGTYI.png [4]: http://dustwell.com/div-span-inline-block.html

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser926958View Question on Stackoverflow
Solution 1 - CssOldskoolView Answer on Stackoverflow