Previously on “let’s not support display: run-in
because it’s complicated and nobody actually uses or wants to use it,” StackOverflow edition…
- 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?
I would like the following behavior for h5 elements in my document:
- It acts like
run-in
when followed by a paragraph (p) - It acts like
block
when followed by a heading (h1, …, h6) or something else (ul, etc.)
This is essentially the same behavior as run-in
if the contents of headings are wrapped in (or contain) a block; i.e., by changing <h6>…</h6>
to <h6><div>…</div></h6>
or <h6>…<div /></h6>
.
(However, I would prefer not to modify the HTML if possible: it’s generated from markdown via pandoc.)
Here’s what I have so far, using floating inline-blocks. Notice how the margin between the h5 and h6 gets “collapsed.”
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.
Here’s a demo page from 7 years ago I found that attempts (unsuccessfully) to fake the same behavior.
Screenshots of Safari
Faked:
Using run-in
(expected behavior, with correct margins between the h5 and the h6 or ul):