How to Center a Flex Container But Left-Align Flex Items

How to center a flex container but left-align flex items

Flexbox Challenge & Limitation

The challenge is to center a group of flex items and left-align them on wrap. But unless there is a fixed number of boxes per row, and each box is fixed-width, this is currently not possible with flexbox.

Using the code posted in the question, we could create a new flex container that wraps the current flex container (ul), which would allow us to center the ul with justify-content: center.

Then the flex items of the ul could be left-aligned with justify-content: flex-start.

#container {
display: flex;
justify-content: center;
}

ul {
display: flex;
justify-content: flex-start;
}

This creates a centered group of left-aligned flex items.

The problem with this method is that at certain screen sizes there will be a gap on the right of the ul, making it no longer appear centered.

enter image description here
enter image description here

This happens because in flex layout (and, actually, CSS in general) the container:

  1. doesn't know when an element wraps;
  2. doesn't know that a previously occupied space is now empty, and
  3. doesn't recalculate its width to shrink-wrap the narrower layout.

The maximum length of the whitespace on the right is the length of the flex item that the container was expecting to be there.

In the following demo, by re-sizing the window horizontally, you can see the whitespace come and go.

DEMO


A More Practical Approach

The desired layout can be achieved without flexbox using inline-block and media queries.

HTML

<ul>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
<li>6</li>
</ul>

CSS

ul {
margin: 0 auto; /* center container */
width: 1200px;
padding-left: 0; /* remove list padding */
font-size: 0; /* remove inline-block white space;
see https://stackoverflow.com/a/32801275/3597276 */
}

li {
display: inline-block;
font-size: 18px; /* restore font size removed in container */
list-style-type: none;
width: 150px;
height: 50px;
line-height: 50px;
margin: 15px 25px;
box-sizing: border-box;
text-align: center;
}

@media screen and (max-width: 430px) { ul { width: 200px; } }
@media screen and (min-width: 431px) and (max-width: 630px) { ul { width: 400px; } }
@media screen and (min-width: 631px) and (max-width: 830px) { ul { width:600px; } }
@media screen and (min-width: 831px) and (max-width: 1030px) { ul { width: 800px; } }
@media screen and (min-width: 1031px) and (max-width: 1230px) { ul { width: 1000px; } }

The above code renders a horizontally-centered container with left-aligned child elements like this:

enter image description here

DEMO


Other Options

  • Properly sizing and aligning the flex item(s) on the last row

  • Desandro Masonry

    Masonry is a JavaScript grid layout library. It
    works by placing elements in optimal position based on available
    vertical space, sort of like a mason fitting stones in a wall. You’ve
    probably seen it in use all over the Internet.

    source: http://masonry.desandro.com/

  • CSS Grid Layout Module Level 1

    This CSS module defines a two-dimensional grid-based layout system, optimized for user interface design. In the grid layout model, the children of a grid container can be positioned into arbitrary slots in a predefined flexible or fixed-size layout grid.

    source: https://drafts.csswg.org/css-grid/

Displaying flexbox centered but align items left like text align left

If you are open to include another wrapper in your markup, it is easy:

  1. Use align-items: flex-start (or let it take the default stretch value) for the #donateList

  2. Center align vertically and horizontally the new wrapper div.

See demo below (also removed some redundant styles):

main { /* ADDED */  display: flex;  align-items: center;  justify-content: center;}#donateList {  display: flex;  justify-content: center;  align-items: flex-start; /* CHANGED */  /*align-self: center;*/  flex-direction: column;  flex-wrap: wrap;}
.donateItem { flex: 0 1 auto; /*align-items: flex-start; justify-content: flex-start; align-self: center;*/}
.donateItem * { display: inline-block;}
.donateItem p { vertical-align: bottom;}
.donateItem img{ height: 64px; width: 64px;}
<main>  <div id="donateList">    <div class="donateItem">      <img src="http://placehold.it/100x100">      <p>Bitcoin:</p>      <p>fkewjhf;eiwhf;iewfhwehfewifhew</p>    </div>    <div class="donateItem">      <img src="http://placehold.it/100x100">      <p>Paypal:</p>      <p>eijfhewfwifhefefewf</p>    </div>  </div></main>

Flex item should align left, not center, when it wraps

Solution

Instead of justify-content: space-around use justify-content: space-between.


Explanation

Take a look at the flexbox spec:

8.2. Axis Alignment: the justify-content property

The justify-content property aligns flex items along the main axis
of the current line of the flex container.

There are five values that apply to justify-content. Here are two of them:

space-around

Flex items are evenly distributed in the line, with half-size spaces
on either end.

If the leftover free-space is negative or there is
only a single flex item on the line, this value is identical to
center.

Emphasis mine. That's the problem you're having.

Now check out space-between:

space-between

Flex items are evenly distributed in the line.

If the leftover free-space is negative or there is only a single flex item on the line, this value is identical to flex-start.

So, to left-align your flex item on wrap, use space-between.

Then, if necessary, you can add some left and right padding to the container to simulate space-around.


Of course, the next problem you'll face is when two items wrap, and each item aligns at opposite ends. But that's another question altogether :-)

Center one and right/left align other flexbox element

Below are five options for achieving this layout:

  • CSS Positioning
  • Flexbox with Invisible DOM Element
  • Flexbox with Invisible Pseudo-Element
  • Flexbox with flex: 1
  • CSS Grid Layout

Method #1: CSS Positioning Properties

Apply position: relative to the flex container.

Apply position: absolute to item D.

Now this item is absolutely positioned within the flex container.

More specifically, item D is removed from the document flow but stays within the bounds of the nearest positioned ancestor.

Use the CSS offset properties top and right to move this element into position.

li:last-child {  position: absolute;  top: 0;  right: 0;  background: #ddd;}ul {  position: relative;  padding: 0;  margin: 0;  display: flex;  flex-direction: row;  justify-content: center;  align-items: center;}li {  display: flex;  margin: 1px;  padding: 5px;  background: #aaa;}p {  text-align: center;  margin-top: 0;}span {  background-color: aqua;}
<ul>  <li>A</li>  <li>B</li>  <li>C</li>  <li>D</li></ul><p><span>true center</span></p>

Aligning elements left and center with flexbox

EDIT: See Solo's answer below, it is the better solution.


The idea behind flexbox is to provide a framework for easily aligning elements with variable dimensions within a container. As such, it makes little sense to provide a layout where the width of one element is totally ignored. In essence, that is exactly what absolute positioning is for, as it takes the element out of the normal flow.

As far as I know, there is no nice way of doing this without using position: absolute;, so I would suggest using it... but If you REALLY don't want to, or can't use absolute positioning then I suppose you could use one of the following workarounds.


If you know the exact width of the "Left" div, then you could change justify-content to flex-start (left) and then align the "Center" div like this:

#center {
position: relative;
margin: auto;
left: -{half width of left div}px;
}

If you do not know the width, then you could duplicate "Left" on the right side, use justify-content: space-between;, and hide the new right element:
Just to be clear, this is really, really ugly... better to use absolute positioning than to duplicate content. :-)

#parent {  align-items: center;  border: 1px solid black;  display: flex;  justify-content: space-between;  margin: 0 auto;  width: 500px;}#right {    opacity: 0;}
<div id="parent">  <span id="left">Left</span>  <span id="center">Center</span>  <span id="right">Left</span></div>

Centering with align-items center everything but not left-aligned

You can have one more div inside the list-wrapper and make its text align left.

#topic-list {
display: flex;
flex-wrap: wrap;
}

#topic-list div.list-wrapper {
flex: 1 0 26%;
margin: 10px;
border: 1px solid #ccc;
padding: 10px;
display: flex;
flex-direction: column;
align-items: center;
}

.test {
text-align: left;
}
<div id="topic-list">
<div class="list-wrapper">
<div class='test'>
<div class="el">A</div>
<div>
<a href="#">Apple</a>
</div>
</div>
</div>

<div class="list-wrapper">
<div class='test'>
<div class="el">B</div>
<div>
<a href="#">Ball</a>
</div>
</div>
</div>

<div class="list-wrapper">
<div class='test'>
<div class="el">F</div>
<div>
<a href="#">Fan</a>
</div>
<div>
<a href="#">Fanta</a>
</div>
<div>
<a href="#">Follow</a>
</div>
</div>
</div>

<div class="list-wrapper">
<div class='test'>
<div class="el">I</div>
<div>
<a href="#">Inspire</a>
</div>
</div>
</div>

<div class="list-wrapper">
<div class='test'>
<div class="el">L</div>
<div>
<a href="#">Love</a>
</div>
</div>
</div>

<div class="list-wrapper">
<div class='test'>
<div class="el">M</div>
<div>
<a href="#">Mad</a>
</div>
<div>
<a href="#">Money</a>
</div>
<div>
<a href="#">Mother</a>
</div>
</div>
</div>

<div class="list-wrapper">
<div class='test'>
<div class="el">S</div>
<div>
<a href="#">Sad</a>
</div>
<div>
<a href="#">Son</a>
</div>
<div>
<a href="#">Sick</a>
</div>
</div>
</div>

<div class="list-wrapper">
<div class='test'>
<div class="el">T</div>
<div>
<a href="#">Tea</a>
</div>
<div>
<a href="#">Total</a>
</div>
</div>
</div>

<div class="list-wrapper">
<div class='test'>
<div class="el">W</div>
<div>
<a href="#">Wrap</a>
</div>
</div>
</div>

</div>


Related Topics



Leave a reply



Submit