I have two buttons in a container. One should display centrally, the second should be always positioned on the right.
However, the button-right
is not always visible in the DOM, in which case then the button-center
then aligns to the right.
Is it possible to avoid using margin-left: auto
and instead using flex properties so that the elements are always positioned in the desired locations, regardless of the presence of each other?
I tried using justify-content: center
and justify-content: flex-end
but that didn't help.
.container {
display: flex;
justify-content: center;
}
.container .button-center {
display: flex;
margin-left: auto;
}
.container .button-right {
margin-left: auto;
}
<div class="container">
<button class="button-center">Centered Button</button>
<button class="button-right">Right Button</button>
</div>