Say I have 3 divs displayed horizontally with flexbox
:
| |-div1-| |-center-div-| |-wider-div-| |
And I want the center div to be aligned to the middle of the parent. How can I accomplish this? justify-content
will center all 3 of the divs based the sum of all their widths, and applying align-self: center
to the middle div does nothing because the align property manipulates positioning on the other axis.
Is there a responsive CSS solution, or should I resort to jQuery?
ul {
display: flex;
justify-content: center;
width: 100%;
background-color: purple;
}
li {
background-color: red;
border: 5px solid blue;
list-style: none;
}
<ul>
<li><a href = "#">short</a></li>
<li><a href = "#">want center</a></li>
<li><a href = "#">loooooooooooooooooong</a></li>
</ul>
Illustration of this problem: https://jsfiddle.net/7w8mp8Lj/2/