gpt4 book ai didi

css - 显示 :block inside display:inline

转载 作者:数据小太阳 更新时间:2023-10-29 09:15:58 32 4
gpt4 key购买 nike

我想了解当 CSS 为 display:block 的元素是 CSS 为 display:inline 的元素的 DOM 子元素时会发生什么(以便 block 元素是内联元素的子元素)。

此场景在 Anonymous block boxes 中进行了描述CSS 2.1 规范部分:示例包括以下规则......

body { display: inline }
p { display: block }

...以及随附的文字说...

The BODY element contains a chunk (C1) of anonymous text followed by a block-level element followed by another chunk (C2) of anonymous text. The resulting boxes would be an anonymous block box around the BODY, containing an anonymous block box around C1, the P block box, and another anonymous block box around C2.

如果你有一个 display:inline 父元素,并且如果这个父元素有一个 display:block 的子元素,那么这个子元素的存在似乎使parent nearly 表现得像 display:block,并忽略它被定义为 display:inline 的事实(因为 parent 现在只包含匿名和非匿名 block child ,即它不再包含任何内联 child )?

我的问题是,在这种情况下(有一个 display:block 子级)那么定义父级 display:inline 而不是 之间有什么区别>显示: block ?


编辑:我更感兴趣的是理解 CSS 2.1 标准,而不是各种浏览器实现在实践中的行为方式和行为。


第二次编辑:

规范中指出了一处差异。在以下文档中,第二个“ block ”段落的边框包围了整个段落和页面的整个宽度;而第一个'内联段落的边框围绕段落内的每一行(即使有几行)并且不超过每行的确切宽度(每行都短于页面宽度)。

<html>
<head>
<style type="text/css">
p.one
{
border-style:solid;
border-width:1px;
display: inline;
}
p.two
{
border-style:solid;
border-width:1px;
}
</style>
</head>
<body>
<p class="one">Some text. <b>Note:</b> The "border-width"
property does not work if it is used alone. Use the
"border-style" property to set the borders first.</p>
<p class="two">Some text.</p>
</body>
</html>

如果我添加以下样式规则...

b
{
display: block;
}

...然后第一个内联段落中的“注意:”变成一个 block ,它将段落拆分(根据规范,段落的第一部分和最后一部分现在在一个匿名 block 中)。然而,段落第一部分和最后一部分的边框仍然是“内联”样式的边框:因此,仍然与声明了 p.onedisplay:block 不一样 首先。

规范中有一段话是这样说的,

Properties set on elements that cause anonymous block boxes to be generated still apply to the boxes and content of that element. For example, if a border had been set on the BODY element in the above example, the border would be drawn around C1 (open at the end of the line) and C2 (open at the start of the line).

“border-style”属性是唯一可见差异的属性类型吗?

最佳答案

当我阅读 the spec ,我发现你的问题实际上是quite well answered :

When an inline box contains a block box, the inline box [...] [is] broken around the block. The [in]line boxes before the break and after the break are enclosed in anonymous boxes, and the block box becomes a sibling of those anonymous boxes.

<BODY style="display: inline; ">
This is anonymous text before the P.
<P>This is the content of P.</P>
This is anonymous text after the P.
</BODY>

The resulting boxes would be an anonymous block box around the BODY, containing an anonymous block box around C1, the P block box, and another anonymous block box around C2.

或者,视觉上:

+- anonymous block box around body ---+
| +- anonymous block box around C1 -+ |
| | + |
| +---------------------------------+ |
| |
| +- P block box -------------------+ |
| | + |
| +---------------------------------+ |
| |
| +- anonymous block box around C2 -+ |
| | + |
| +---------------------------------+ |
+-------------------------------------+

现在回答您的问题:这与 <BODY style="display: block; "> 不同吗? ?

是的,是的。虽然它仍然是 4 个框( body 周围的匿名 block 框现在是 BODY block 框),规范告诉区别:

Properties set on elements that cause anonymous block boxes to be generated still apply to the [generated anonymous block] boxes and content of that element. For example, if a border had been set on the BODY element in the above example, the border would be drawn around C1 (open at the end of the line) and C2 (open at the start of the line):

+--------------------------------------
| This is anonymous text before the P.
+--------------------------------------
This is the content of P.
--------------------------------------+
This is anonymous text after the P. |
--------------------------------------+

这与 <BODY style="display: block; "> 不同:

+--------------------------------------+
| This is anonymous text before the P. |
| |
| This is the content of P. |
| |
| This is anonymous text after the P. |
+--------------------------------------+

关于css - 显示 :block inside display:inline,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1371307/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com