Inline vs Inline-Block Display

Datetime:2016-08-23 03:00:11          Topic: HTML           Share

display: inline-block brought a new way to create side by side boxes that collapse and wrap properly depending on the available space in the containing element. It makes layouts that were previously accomplished with floats easier to create. No need to clear floats anymore.

Compared to display: inline , the major difference is that inline-block allows to set a width and height on the element. Also, with display: inline , top and bottom margins & paddings are not respected, and with display: inline-block they are.

Now, the difference between display: inline-block and display: block is that, with display: block , a line break happens after the element, so a block element doesn’t sit next to other elements. Here are some visual examples:

display: inline

Notice here how the width and height are not respected, and how the padding top and bottom are present, but overlap over the lines above and under.

span.box {
  display: inline; /* the default for span */
  width: 100px;
  height: 160px;
  padding: 18px;
}

Cheese and wine ricotta danish fontina. Brie cheesy grin paneer squirty cheese taleggio cheesecake goat taleggio goat taleggio . Bavarian bergkase emmental fromage cheesecake cheese slices cheesy grin queso caerphilly.

display: inline-block

Here the width, height and padding are respected, but the two copies of the element can still sit side by side.

span.box {
  display: inline-block;
  width: 100px;
  height: 160px;
  padding: 18px;
}

Cheese and wine ricotta danish fontina. Brie cheesy grin paneer squirty cheese taleggio cheesecake goat taleggio goat taleggio . Bavarian bergkase emmental fromage cheesecake cheese slices cheesy grin queso caerphilly.

display: block

Here again everything is respected, but the elements don’t sit side by side.

span.box {
  display: block;
  width: 100px;
  height: 160px;
  padding: 18px;
}

Cheese and wine ricotta danish fontina. Brie cheesy grin paneer squirty cheese taleggio cheesecake goat taleggio goat taleggio . Bavarian bergkase emmental fromage cheesecake cheese slices cheesy grin queso caerphilly.

inline-block → Browser Support

Can I Use inline-block? Data on support for the inline-block feature across the major browsers from caniuse.com.





About List