How to center div horizontally, and vertically within the container using flexbox. In below example, I want each number below each other (in rows), which are centered horizontally.
.flex-container {
padding: 0;
margin: 0;
list-style: none;
display: flex;
align-items: center;
justify-content: center;
}
row {
width: 100%;
}
.flex-item {
background: tomato;
padding: 5px;
width: 200px;
height: 150px;
margin: 10px;
line-height: 150px;
color: white;
font-weight: bold;
font-size: 3em;
text-align: center;
}
<div class="flex-container">
<div class="row">
<span class="flex-item">1</span>
</div>
<div class="row">
<span class="flex-item">2</span>
</div>
<div class="row">
<span class="flex-item">3</span>
</div>
<div class="row">
<span class="flex-item">4</span>
</div>
</div>
Add
to the container element of whatever you want to center. Documentation: justify-content and align-items.
1 - Set CSS on parent div to
display: flex;
2 - Set CSS on parent div to
flex-direction: column;
Note that this will make all content within that div line up top to bottom. This will work best if the parent div only contains the child and nothing else.
3 - Set CSS on parent div to
justify-content: center;
Here is an example of what the CSS will look like:
diplay: flex;
for it's container andmargin:auto;
for it's item works perfect.NOTE: You have to setup the
width
andheight
to see the effect.How to Center Elements Vertically and Horizontally in Flexbox
Below are two general centering solutions.
One for vertically-aligned flex items (
flex-direction: column
) and the other for horizontally-aligned flex items (flex-direction: row
).In both cases the height of the centered divs can be variable, undefined, unknown, whatever. The height of the centered divs doesn't matter.
Here's the HTML for both:
CSS (excluding decorative styles)
When flex items are stacked vertically:
DEMO
When flex items are stacked horizontally:
Adjust the
flex-direction
rule from the code above.DEMO
Centering the content of the flex items
The scope of a flex formatting context is limited to a parent-child relationship. Descendants of a flex container beyond the children do not participate in flex layout and will ignore flex properties. Essentially, flex properties are not inheritable beyond the children.
Hence, you will always need to apply
display: flex
ordisplay: inline-flex
to a parent element in order to apply flex properties to the child.In order to vertically and/or horizontally center text or other content contained in a flex item, make the item a (nested) flex container, and repeat the centering rules.
More details here: How to vertically align text inside a flexbox?
Alternatively, you can apply
margin: auto
to the content element of the flex item.Learn about flex
auto
margins here: Methods for Aligning Flex Items (see box#56).Centering multiple lines of flex items
When a flex container has multiple lines (due to wrapping) the
align-content
property will be necessary for cross-axis alignment.From the spec:
More details here: How does flex-wrap work with align-self, align-items and align-content?
Browser support
Flexbox is supported by all major browsers, except IE < 10. Some recent browser versions, such as Safari 8 and IE10, require vendor prefixes. For a quick way to add prefixes use Autoprefixer. More details in this answer.
Centering solution for older browsers
For an alternative centering solution using CSS table and positioning properties see this answer: https://stackoverflow.com/a/31977476/3597276
I think you want something like the following.
See demo at: http://jsfiddle.net/audetwebdesign/tFscL/
Your
.flex-item
elements should be block level (div
instead ofspan
) if you want the height and top/bottom padding to work properly.Also, on
.row
, set the width toauto
instead of100%
.Your
.flex-container
properties are fine.If you want the
.row
to be centered vertically in the view port, assign 100% height tohtml
andbody
, and also zero out thebody
margins.Note that
.flex-container
needs a height to see the vertical alignment effect, otherwise, the container computes the minimum height needed to enclose the content, which is less than the view port height in this example.Footnote:
The
flex-flow
,flex-direction
,flex-wrap
properties could have made this design easier to implement. I think that the.row
container is not needed unless you want to add some styling around the elements (background image, borders and so on).A useful resource is: http://demo.agektmr.com/flexbox/
Don't forgot to use important browsers specific attributes:
align-items: center; -->
justify-content: center; -->
You could read this two links for better understanding flex: http://css-tricks.com/almanac/properties/j/justify-content/ and http://ptb2.me/flexbox/
Good Luck.