In my application the number of items in a flex container is variable, and I want them to be both vertically and horizontally centred. I'm doing this by using justify-content
for horizontal centring, and align-content
for vertical centring.
However, reading from CSS Tricks' almanac states the following:
Note, this property has no effect when the flexbox has only a single line.
This becomes quickly apparent in my application. While there aren't enough items to wrap onto another line, vertical centring from align-content
doesn't work at all.
As soon as a second line of content appears, align-content
kicks in and works as expected.
I'm sure there's a reason for this behaviour, however in my case the result is an inconsistent user experience.
I know that I can achieve vertical centring by other means, for instance by wrapping the entire container in another flexbox, or a table, and applying vertical centring there. However, I feel like there should be a better way?
Is there some other flexbox option I'm missing that would solve this problem without introducing more HTML elements? If so, what is that option? :P