Let's consider two items A and B within their container C. When width(C) > width(A)+width(B)
, I want the items to stay on the same line: A aligned left and B right. But when width(C) < width(A)+width(B)
, I want them to use their own line, and be centered within C horizontally.
I have achieved all the above except for the latter requirement — centering them when they are on their own line each. How do I do that? Keen to use flexbox but any working solution would be good, even with floats.
.container {
display: flex;
flex-wrap: wrap;
justify-content: space-between;
}
.container div:nth-child(1) {
background-color: orange;
}
.container div:nth-child(2) {
background-color: green;
}
<div class="container">
<div>Item1 Item1 Item1 Item1 Item1</div>
<div>Item2 Item2 Item2 Item2 Item2</div>
</div>