假`显示:run-in`具有“正确”的保证金行为

时间:2014-05-01 15:46:48

标签: css css-float

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


我希望文档中的h5元素具有以下行为:

  • 当后跟段落(p)
  • 时,它就像run-in
  • 当后跟标题(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之间有正确的边距):

  

1 个答案:

答案 0 :(得分:0)

也许我有一个你想要的妥协: 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;
}