<div class="aligner">
<div class="aligner-item">…</div>
</div>
.aligner {
display: flex;
align-items: center;
justify-content: center;
}
.aligner-item {
max-width: 50%; /*for demo. Use actual width instead.*/
}
Here is a demo.
Property | Value | Description |
---|---|---|
align-items | center | This centers the elements along the axis other than the one specified by flex-direction , i.e., vertical centering for a horizontal flexbox and horizontal centering for a vertical flexbox. |
justify-content | center | This centers the elements along the axis specified by flex-direction . I.e., for a horizontal (flex-direction: row ) flexbox, this centers horizontally, and for a vertical flexbox (flex-direction: column ) flexbox, this centers vertically) |
All of the below styles are applied onto this simple layout:
<div id="container">
<div></div>
<div></div>
<div></div>
</div>
where #container
is the flex-box
.
justify-content: center
on a horizontal flexboxCSS:
div#container {
display: flex;
flex-direction: row;
justify-content: center;
}
Outcome:
Here is a demo.
justify-content: center
on a vertical flexboxCSS:
div#container {
display: flex;
flex-direction: column;
justify-content: center;
}
Outcome:
Here is a demo.
align-content: center
on a horizontal flexboxCSS:
div#container {
display: flex;
flex-direction: row;
align-items: center;
}
Outcome:
Here is a demo.
align-content: center
on a vertical flexboxCSS:
div#container {
display: flex;
flex-direction: column;
align-items: center;
}
Outcome:
Here is a demo.
div#container {
display: flex;
flex-direction: row;
justify-content: center;
align-items: center;
}
Outcome:
Here is a demo.
div#container {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
Outcome:
Here is a demo.