When it comes to the justify-content
and align-content
properties in CSS, they can only be applied to the flex container, yet they have an impact on the flex items. To center the container, you would need to include justify-content: center
in the parent of .flex-container
(which is typically the body
).
Another way to achieve this is by using auto
margins on the .flex-container
.
The align-content
property focuses on the cross-axis of the flex container (the vertical axis in this situation) and may not always be necessary for adjusting your layout.
Consider implementing the following code:
body {
display: flex;
justify-content: center;
}
.flex-container {
display: flex;
justify-content: flex-start;
flex-wrap: wrap;
}
Check out the updated plunker demo.
You could also try this variation:
.flex-container {
display: flex;
justify-content: flex-start;
flex-wrap: wrap;
margin: 0 auto;
}
View the modified plunker demo.
UPDATE (in response to comments)
If dealing with excess whitespace on the right side of the container, explore these resources:
- How to center a flex container but left-align flex items
- Center align container and left align child elements
- Left aligned last row in centered grid of elements