CSS显示:内联与内联块[重复]

在CSS中,display可以具有inline和的inline-block谁能详细解释inline之间的区别inline-block

我到处搜索,最详细的说明告诉我inline-block放置为inline,但行为类似block但这并没有解释“作为一个整体”的确切含义。有什么特别的功能吗?

一个例子将是一个更好的答案。谢谢。

斯丁Gil2020/03/17 17:18:23

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.

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:

  • 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块与内联与内联块

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