【问题标题】:Fake `display: run-in` with “correct” margin behavior具有“正确”边距行为的假“显示:磨合”
【发布时间】:2014-06-18 03:21:11
【问题描述】:

之前关于“让我们不支持display: run-in,因为它很复杂,没有人真正使用或想要使用它”,StackOverflow 版……


我希望文档中的 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 &lt;h5> and &lt;h6> as there is
between the &lt;h4> and &lt;h5>, but it gets “collapsed” because the
&lt;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 0 declaration,它不起作用,因为浮动的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 下拉。对不起我的英语不好:)

标签: css css-float


【解决方案1】:

也许我有一个你想要的妥协:DEMO

/* 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`... */
* {
 clear:left;
}
h5 {
    float:left;
    display: run-in;
    margin: 0 1em 0 0;

}
h5:after {
    content:'.';
    position:absolute;
    background:inherit;
    width:100%;
    right:0;
    z-index:-1;
}
body {
    position:relative; /* to include defaut margin of body to draw h5:after element within body */
}
p /* + any other phrasing tag you wish */ {
    clear:none;
    background:white;
}

【讨论】:

  • 这只是demo的变通,如果涉及背景和相对定位,则需要调整
  • 感谢您的回答,但这仍然使 h5 和下一个“块”(h6 或 ul)之间的边距折叠,这是我问题的重点。彩色背景只是为了说明元素的边界,我并不担心这些。
  • 从浮动元素伪造模拟边距的旧边框技巧......这开始需要大量调整。难以维护:jsfiddle.net/47W2C/24。仍然这是一个很容易破坏的解决方法:)(如果 bg 涉及真实,它可能是透明边框和背景剪辑)
猜你喜欢
  • 2014-12-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-15
  • 1970-01-01
  • 2013-01-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多