CSS 显示:内联 vs 内联块
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9189810/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
CSS display: inline vs inline-block
提问by user926958
In CSS, display
can have values of inline
and inline-block
. Can anyone explain in detail the difference between inline
and inline-block
?
在 CSS 中,display
可以有inline
和 的值inline-block
。谁能详细解释一下inline
和之间的区别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?
我到处搜索,最详细的解释告诉我inline-block
放置为inline
,但表现得像block
。但它并没有解释“表现得像块”究竟是什么意思。它有什么特别之处吗?
An example would be an even better answer. Thanks.
一个例子会是一个更好的答案。谢谢。
回答by Oldskool
Inline elements:
内联元素:
- respect left & right margins and padding, but nottop & bottom
- cannothave a width and height set
- allow other elements to sit to their left and right.
- see very important side notes on this here.
- 尊重左右边距和填充,但不尊重顶部和底部
- 不能设置宽度和高度
- 允许其他元素坐在他们的左右。
- 在这里查看非常重要的附注。
Block elements:
块元素:
- respect all of those
- force a line break after the block element
- acquires full-width if width not defined
- 尊重所有这些
- 在块元素后强制换行
- 如果未定义宽度,则获取全角
Inline-block elements:
内联块元素:
- allow other elements to sit to their left and right
- respect top & bottom margins and padding
- respect height and width
- 允许其他元素坐在他们的左右
- 尊重顶部和底部边距和填充
- 尊重高度和宽度
From W3Schools:
来自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.
内联元素之前或之后都没有换行符,并且可以容忍旁边的 HTML 元素。
块元素的上下都有一些空白,并且不允许在它旁边有任何 HTML 元素。
内联块元素作为内联元素放置(与相邻内容在同一行),但它的行为与块元素相同。
When you visualize this, it looks like this:
当你想象这个时,它看起来像这样:
The image is taken from this page, which also talks some more about this subject.