Plz wait before marking this question as duplicate or possible duplicate.
I aligned the child item
vertically centered but the content i.e, text, of that child item
wasn't aligned at the center of box initially. But when I added display:flex;
,
justify-content:center;
& flex-direction:column;
inside the child class
, the text was vertically centered.
I checked these questions:
How to vertically align text inside a flexbox?
Is there a way to change the vertical-align of items inside flex-items?
Some answers helped me to vertically align the child item
but it wasn't mentioned how to align the contents of child
at its vertical center at middle.
HTML
<div class="flex-container">
<div class="flex-item">hello hello hello hello </div>
</div>
CSS before
.flex-container {
display: flex;
justify-content:center;
align-items:center;
width: 300px;
height: 300px;
background-color: lightgrey;
text-align:center;
}
.flex-item {
background-color: cornflowerblue;
width: 100px;
height: 100px;
the result is:
after adding display:flex;
,
justify-content:center;
& flex-direction:column;
CSS after
.flex-item {
background-color: cornflowerblue;
width: 100px;
height: 100px;
display:flex; //<---- added property supposed to be work on parent
justify-content:center; //<---- added property supposed to be work on parent
flex-direction:column; //<---- added property supposed to be work on parent
the result is:
I want to know if there is some other approach using flexbox
to align the content of child at vertical center. Also, why adding parent properties i.e, display:flex;
,
justify-content:center;
& flex-direction:column;
worked on child class
? Aren't these properties supposed to be applied & work only on parents?