Lets say i have this html
.container {
height: 50px;
width: 100%;
display: flex;
justify-content: space-between;
}
.left {
height: 50px;
width: 20%;
background: red;
}
.middle {
height: 50px;
width: 20%;
background: yellow;
}
.right {
height: 50px;
width: 20%;
background: blue;
}
.real-center {
width: 20%;
margin: auto;
height: 50px;
background: lightgreen;
text-align: center;
}
<div class='container'>
<div class='left'></div>
<div class='middle'></div>
<div class='right'></div>
</div>
<br/>
<div class='container'>
<div class='real-center'>This is Real Center</div>
</div>
So my main goal is left always at the start, middle always center and right always stick at the end of the container.
So if the width of all div the same ( or at least the width of left and right are the same), this can be achieved. However, if the width of these divs are different, space-between no longer be able to achieve this. For example
.container {
height: 50px;
width: 100%;
display: flex;
justify-content: space-between;
}
.left {
height: 50px;
width: 10%;
background: red;
}
.middle {
height: 50px;
width: 50%;
background: yellow;
}
.right {
height: 50px;
width: 20%;
background: blue;
}
.real-center {
background: lightgreen;
height: 50px;
width: 50%;
margin:auto;
text-align:center;
}
<div class='container'>
<div class='left'></div>
<div class='middle'></div>
<div class='right'></div>
</div>
<br/>
<div class='container'>
<div class='real-center'>This is Real Center</div>
</div>
As you can see the middle div (yellow) is no longer at the center of the container.
IS there any css that can make yellow div always at center, regardless the width of the 3 children. Thanks
P/s: as the width of all of 3 divs may change alot, so i try solutions with position (relative,absolute), or adding some margin, it may not work for all cases, so im looking for a more general solution, that auto center this, thanks