【问题标题】:What is the actual purpose of display: inline [duplicate]显示的实际目的是什么:内联[重复]
【发布时间】:2019-11-13 13:56:42
【问题描述】:

我读到了display: inlinedisplay: inline-block 之间的区别。这个问题不是关于它们的区别,而是关于它们的用法非常不同。

我不明白为什么我需要使用内联显示。

似乎 inline-block 可以做所有 inline 可以做的事情,甚至更多。用display: inline-block 声明一个没有任何宽度、高度或垂直边距/填充的元素与将显示声明为内联不一样吗?

是否存在您必须使用display : inline 而尝试使用display: inline-block 实现相同目标的情况下不起作用?

【问题讨论】:

  • 阻止“继承”的 CSS 属性不适用于内联
  • 一个简单的用例:你有一个很长的句子,你想改变它的颜色,你把它包裹在一个 <span> 这是一个内联元素,然后你改变颜色。使用 inline-block 并查看它在换行时的表现
  • @LoveenDyall 你能举例说明你的意思吗?
  • 这是另一个用例,其中 inline 是强制实现结果的:stackoverflow.com/a/53882402/8620333
  • this commentthis answer 有必要使用display: inline; 的场景。

标签: html css


【解决方案1】:

Temani Afif 评论中的一个例子:

<h1>inline</h1>
<p>
This is a sentence. <span style="background-color:red">This is another sentence that's really long and has a lot of words and a lot of clauses and needs to be this way to make the example work so that it breaks to a new line and may or may not be a run-on sentence and is probably long enough now.</span> This is yet another sentence.
</p>

<h1>inline-block</h1>
<p>
This is a sentence. <span style="background-color:red;display:inline-block">This is another sentence that's really long and has a lot of words and a lot of clauses and needs to be this way to make the example work so that it breaks to a new line and may or may not be a run-on sentence and is probably long enough now.</span> This is yet another sentence.
</p>

第一个隐式为display:inline,第二个隐式为display:inline-block。请注意,span 中的句子前后都有换行符,而第一个则没有。

【讨论】:

  • 我刚刚尝试在我的浏览器中运行这段代码,在这两种情况下,它们的行为完全相同:

    之前的一些文本。 里面有更多单词。 和文本之后。

  • @YonatanNir 什么浏览器?在 Chrome 中,桌面和 Android 上的区别非常明显。
  • 好吧,看来我需要更长的句子来重现它。并且它们之间存在差异的问题并没有解释 inline-block 元素的换行
猜你喜欢
  • 2013-02-28
  • 2018-02-12
  • 1970-01-01
  • 2014-10-30
  • 2012-07-19
  • 1970-01-01
  • 2013-05-16
  • 1970-01-01
相关资源
最近更新 更多