My Flow utility class was applying margins around invisible elements like <style> and <script> tags, throwing off the vertical rhythm around them. Here's what I did to fix it, and why I also swapped em for lh while I was in there.
This article uses some custom styles that may or may not work in your feed reader, so I recommend checking out the web version instead: https://chrisburnell.com/article/ghost-wrangling/
If you’re not already familiar with the Flow utility class in CSS, I recommend reading through Flow CSS Utility on Piccalilli.
In short, the Flow utility class is used to provide consistent and dynamic vertical spacing between adjacent children of a given element. Here’s what that CSS looks like:
.flow > * + * {
margin-block-start: var(--flow-space, 1em);
}
Since I learned about this idea, I’ve been using it everywhere, and I’ve made a few modifications since then. The CSS side of things is a real set it and forget it
situation (just the best, man), but because the selector is so beautifully terse, it overlooks a situation I sometimes find myself in when applying the flow class in HTML, and it has to do with invisible elements.
Invisible?
Web browsers ship with user agent
styles for many HTML elements with inherent layout or visual semantics. These styles do things like apply display: block; to <div> elements or font sizes to heading elements. Some HTML elements are designed to be invisible, and the browser does this by applying default styles of display: none; to these elements, e.g.:
base, basefont, datalist, head, link, meta, noembed, noframes, param, rp, script, style, template, title
Fortunately, applying display: none; to an element means that any margins it has won’t be represented in the layout of the page. So we can let our Flow utility happily apply margins to these invisible elements wherever they appear within a flow block — their margins won’t affect the page.
<section class="flow">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
<p>Vestibulum suscipit id nulla vitae maximus. In bibendum risus in commodo fermentum.</p>
<p>Vestibulum elementum turpis ac aliquam pulvinar.</p>
<style>p { font-weight: bold; }</style>
</section>
In the above HTML, we can expect our margin to be applied to the 2nd and 3rd paragraphs as well as the <style> element, but because it’s invisible, its margin won’t show. The empty lines represent where we would see margins on the front end.
But now let’s consider a slight change in DOM order, where the <style> element is the first child:
<section class="flow">
<style>p { font-weight: bold; }</style>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
<p>Vestibulum suscipit id nulla vitae maximus. In bibendum risus in commodo fermentum.</p>
<p>Vestibulum elementum turpis ac aliquam pulvinar.</p>
</section>
Now we’ve hit a snag.
The Flow utility isn’t taking into consideration that the <style> element is invisible, so it’s applying a margin to the 1st paragraph, which we don’t want.
What we do want is for space to be created only between the visible elements. Here’s what that CSS looks like:
.flow > :not(datalist, link, script, style, template, .visually-hidden, .ignore-flow) + * {
margin-block-start: var(--flow-space, 1lh);
}
Here, we’re instead saying that only when an element is preceded by a visible element (i.e. not an invisible element) should a margin be applied.
In addition to this change in logic, I have also changed the default/fallback value of the margin to 1lh, which represents the amount of space taken up by the line-height of the element receiving the margin, rather than its font-size (1em). I think this creates a better vertical rhythm, as the height of each space between elements is equal to the height taken up by a line of text.
Not all elements in the previous list of invisible elements are necessary to include in the selector, as they should never reasonably appear inside a flow block (they’re not valid inside body, etc.).
Ahmad Shadeed has a great article all about The CSS lh unit that is great reading to learn about why using sizes relative to the line-height of some text is often preferable to sizes relating to font-size.
Now let’s revisit our previous snippet of HTML where our old Flow utility was too greedy in applying margins to elements. I’ve again added empty lines to show where we would see margins on the front end:
<section class="flow">
<style>p { font-weight: bold; }</style>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
<p>Vestibulum suscipit id nulla vitae maximus. In bibendum risus in commodo fermentum.</p>
<p>Vestibulum elementum turpis ac aliquam pulvinar.</p>
</section>
Awesome. The 1st paragraph is now excluded from receiving the margin because it is preceded by a <style> element, one of the invisible ones we’re looking for in our new CSS selector.
These changes provide even better spacing, and only between visible, adjacent children of a given element.
Hey, are you participating in HTML Day 2026 this year ()? Find out if there’s an event local to you on the website, and if not, maybe I’ll see you at the Online Events run by Zachary Kai!
If I was only allowed to write three lines of CSS tomorrow, it would be the Flow utility class without question!
Thanks for subscribing and reading this post via RSS!
You can read this post and others on my website.





