Flex columns only center when packaged

I show list items in columns using flexbox. Items must be inserted into more columns after a certain height, columns must be centered horizontally, and list items in each column must be justified. I use max-heightto limit the height of the list, flex-flow: column wrapto create wrapping columns, and align-content: centerto center the columns.

I understand that a multi-column solution may be more obvious, but I do not want to define column-widthor column-count, so I chose the flexbox solution.

Problem
Columns are only horizontally positioned when items are wrapped across multiple columns. If there is only one column, then the column is not centered. I see this behavior in Chrome 63 on both Windows 10 Home and MacOS Sierra. In Firefox, it looks as I expected (screenshots below).

Am I missing something?
How can I make a column always be horizontally centered, cross browser?

.filter_drop {
  display: flex;
  flex-flow: column wrap;
  align-content: center;
  list-style: none;
  margin: 0;
  padding: 0;
  max-height: 7em;
  border-bottom: 1px solid black;
}

.filter_drop li {
  margin: 0 1em 0 0;
  line-height: 1.2;
}
<ul class="filter_drop">
  <li>One</li>
  <li>Two </li>
  <li>Three</li>
  <li>Four</li>
  <li>Five</li>
  <li>Six</li>
  <li>Seven</li>
  <li>Eight </li>
  <li>Nine</li>
  <li>Ten</li>
  <li>Eleven</li>
  <li>Twelve</li>
</ul>
<ul class="filter_drop">
  <li>One</li>
  <li>Two</li>
  <li>Three</li>
  <li>Four</li>
  <li>Five</li>
  <li>Six</li>
  <li>Seven</li>
</ul>
<ul class="filter_drop">
  <li>One</li>
  <li>Two</li>
  <li>Three</li>
  <li>Four</li>
</ul>
<ul class="filter_drop">
  <li>One</li>
  <li>Two</li>
  <li>Three</li>
</ul>
Run codeHide result

View in JSFiddle


Chrome 63:
Chrome layout

Firefox 57:
Firefox Layout

+4
source share
1 answer

align-content only works if there are multiple lines in the flex container.

align-itemsor align-selfrequired to align one line.

Here is the full explanation:

.filter_drop {
  display: flex;
  flex-flow: column wrap;
  align-content: center;
  align-items: center; /* NEW */
  list-style: none;
  margin: 0;
  padding: 0;
  max-height: 7em;
  border-bottom: 1px solid black;
}

.filter_drop li {
  margin: 0 1em 0 0;
  line-height: 1.2;
}
<ul class="filter_drop">
  <li>One</li>
  <li>Two </li>
  <li>Three</li>
  <li>Four</li>
  <li>Five</li>
  <li>Six</li>
  <li>Seven</li>
  <li>Eight </li>
  <li>Nine</li>
  <li>Ten</li>
  <li>Eleven</li>
  <li>Twelve</li>
</ul>
<ul class="filter_drop">
  <li>One</li>
  <li>Two</li>
  <li>Three</li>
  <li>Four</li>
  <li>Five</li>
  <li>Six</li>
  <li>Seven</li>
</ul>
<ul class="filter_drop">
  <li>One</li>
  <li>Two</li>
  <li>Three</li>
  <li>Four</li>
</ul>
<ul class="filter_drop">
  <li>One</li>
  <li>Two</li>
  <li>Three</li>
</ul>
Run codeHide result
+1

Source: https://habr.com/ru/post/1691056/


All Articles