The root of the issue appears to be when using column-count
browsers treat margin
and padding
differently depending on whether there are an even or odd number of elements.
This means solutions like display: inline-block; width: 100%
; or playing with margin
or padding
create inconsistent results.
Solution
The only way I found to make this consistently work across browsers is to add a parent-element
and a spacer
. Then use break-inside: avoid
to keep the two elements together.
HTML
<div class="mainContentSection">
<div class="parent-element"> <!-- added this element -->
<p>Blah blah blah blah</p>
<div class="spacer"></div> <!-- added this element -->
</div>
<div class="parent-element">
<p>Blah2 blah2 blah2 blah2</p>
<div class="spacer"></div>
</div>
<!-- addition items... -->
</div>
CSS
.mainContentSection{
column-count:2
}
.parent-element{
-webkit-column-break-inside: avoid;
page-break-inside: avoid;
break-inside: avoid;
}
.spacer{
height: 10px;
}
You can add padding
to mainContentSection
or add another spacer
above the paragraph depending where you need space.
Problem
The inconsistent alignment appears to be is driven by margin
and padding
being handled differently for an even or odd number of elements. I found this to be true for -top
and -bottom
but to keep things simple I only use margin-bottom
in the example.
Even Number of Elements (works fine)
When there's an even number of elements margin-bottom
works.
Odd Number of Elements (does not work)
When there's an odd number of elements margin-bottom
gets moved to the second column creating the misalignment.
Other Solutions
This means that just playing with margin
or padding
leads to inconsistent results across browsers and devices.
Using display: inline-block; width:100%
almost works but I found for some browser/device variations if there are only two elements it keeps the two elements in one column because there is no break. This can be addressed by adding a break as proposed here but as the user mentions it seems to have inconsistent results if element heights vary.