【发布时间】:2014-06-18 03:21:11
【问题描述】:
之前关于“让我们不支持display: run-in,因为它很复杂,没有人真正使用或想要使用它”,StackOverflow 版……
- display: run-in dropped in Chrome?
- Style a definitition list as simple key value set
- CSS method instead of display:run-in; to position a block inline?
我希望文档中的 h5 元素具有以下行为:
- 它的作用类似于
run-in后跟一个段落 (p) - 后跟标题(h1、...、h6)或其他内容(ul 等)时,它的作用类似于
block
如果标题的内容被包裹在(或包含)一个块中,这本质上就是same behavior as run-in;即,将<h6>…</h6> 更改为<h6><div>…</div></h6> 或<h6>…<div /></h6>。
(但是,如果可能,我不希望修改 HTML:它是通过 pandoc 从 markdown 生成的。)
这是我目前使用的浮动内联块。注意 h5 和 h6 之间的边距是如何“折叠”的。
CSS
/* Just some basic styles to start off */
body { line-height: 1.5; }
h4,h5,h6 { font-size: 1em; margin: 1em 0; }
h4 { background: #fee; }
h5 { background: #eef; }
h6 { background: #dfd; font-style: italic; font-weight: normal; }
/* Now let’s try to emulate `display: run-in`... */
h4,h5,h6 {
clear: both;
}
h5 {
float: left;
display: inline-block;
margin: 0 1em 0 0;
}
HTML
<h4>Section</h4>
<h5>Heading</h5>
<p>Paragraph here. This is some text to fill out the line and make it wrap,
so you can get a feel for how a heading with <code>display: run-in;</code>
might look.</p>
<h5>Heading, but without immediately following text</h5>
<h6><div>Subheading</div></h6>
<p>There should be as much space between the <h5> and <h6> as there is
between the <h4> and <h5>, but it gets “collapsed” because the
<h5> floats.</p>
<h5>Heading followed by a list</h5>
<ul><li>A list item</li></ul>
Here is a jsfiddle containing the HTML and CSS.
Here is one using run-in for browsers that still support it, like Safari.
这是 7 年前的 demo page,我发现有人尝试(未成功)伪造相同的行为。
Safari 截图
伪造:
使用run-in(预期行为,在 h5 和 h6 或 ul 之间有正确的边距):
【问题讨论】:
-
h5 {float:left;} h5 + * {clear:left;} h5 + p {clear:none;}会做吗?因为无论如何,CSS 都无法爬上 dom 树。 -
Without the
margin: 0 1em 0 0declaration,它不起作用,因为浮动的h5高于段落的一行,并且h5的边距不会与前面的p一起折叠。 With it,唯一的区别是现在ul被推到下一行了;保证金问题仍然存在。 -
在我看来,问题不在于磨合显示,而在于 ypu 想要根据下一个元素切换显示行为。 CSS 不能做这样的事情。浮点数会杀死显示值,因为它会从文档流中部分移除元素。对我来说,Safari 的行为对我来说很奇怪,在 W3C 上,它并没有说,它的行为是在段落前面运行,而不是在 hx 标签或 ul 前面:w3.org/TR/css3-box/#run-in-boxes
-
我很难解析你的评论,但对我来说,Safari 的行为符合规范。在 h6 和 ul 的情况下,标题成为下一个块元素的第一个内联框。恰好 h6 和 ul 的以下内容显示为块 (div) 或列表项 (li),因此内联的 h5 看起来“块状”。
-
这是 Safari 呈现的内容:img4.hostingpics.net/pics/457234safarirunin.jpg,ul 下拉。对不起我的英语不好:)