I want to vertically center a div (that is in another div) with flexbox.
I tried adding this, but doesn't work:
display: flex;
align-items: center;
Here jsfiddle:
I want to vertically center a div (that is in another div) with flexbox.
I tried adding this, but doesn't work:
display: flex;
align-items: center;
Here jsfiddle:
You need to put the flex properties in the parent div:
#boss {
border: 1px solid #000;
width: 300px;
height: 300px;
display: flex; /* moved here from child div */
align-items: center; /* moved here from child div */
}
#kids {
margin: 0 auto;
border: 1px solid #000;
width: 150px;
height: 150px;
}
OR...
You could remove the align-items
keyword property from the flex container and simply use auto
margins on the flex item.
#boss {
border: 1px solid #000;
width: 300px;
height: 300px;
display: flex;
/* align-items: center <-- REMOVE */
}
#kids {
margin: auto; /* adjusted */
border: 1px solid #000;
width: 150px;
height: 150px;
}
Learn more here: Methods for Aligning Flex Items