I have a flexbox-controlled footer. It can contain 1,2 or 3 subordinate divs.
.footer {
display: flex;
flex-direction: row;
align-items: center;
justify-content: space-between;
}
<div class='footer'>
<div>Left</div>
<div>Middle</div>
<div>Right</div>
</div>
With 2 or 3 divs, the css above works fine:
But with a single div, it is left-justified. I would prefer it would be centered.
How can I adjust the css so that in the case of a single div it will be centered, while retaining the behaviour for 2 or 3 divs?
To center the inner div element we will make the parent a flex container. By adding the display: flex; property we make the section element a flex container allowing us to adjust the layout of the div which is now a flex item. To center out item horizontally we use the justify-content: center; .
One use of auto margins in the main axis is to separate flex items into distinct "groups". The following example shows how to use this to reproduce a common UI pattern - a single bar of actions with some aligned on the left and others aligned on the right.
position:fixed makes flexbox not workable, since it overrides the position of <p>Hello</p> Try to remove it and see the difference. If you want align-items:center works; try to set height: 100vh; since the body height of webview is not set to screen height by default.
.footer > :only-child
{
margin-left: auto;
margin-right: auto;
}
you can make sure your items grow with the available space so their content will be located relatively to the item
.footer > div {
flex-grow:1:
text-align: center; // for example
}
.footer {
display: flex;
flex-direction: row;
align-items: center;
justify-content: space-between;
}
.footer > div {
flex-grow:1;
text-align:center; //for example
}
<div class='footer'>
<div>Left</div>
<div>Middle</div>
<div>Right</div>
</div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With