Within a div with text-align:center
, I have multiple same-size blocks set to display:inline-block
.
| _____ _____ _____ _____ |
| | | | | | | | | |
| | 1 | | 2 | | 3 | | 4 | |
| |_____| |_____| |_____| |_____| |
| _____ _____ _____ _____ |
| | | | | | | | | |
| | 5 | | 6 | | 7 | | 8 | |
| |_____| |_____| |_____| |_____| |
| |
These blocks align horizontally and adjust to new rows as the browser window is resized. To maintain center alignment with the last row left-aligned, the layout should look like this:
| _____ _____ _____ |
| | | | | | | |
| | 1 | | 2 | | 3 | |
| |_____| |_____| |_____| |
| _____ _____ _____ |
| | | | | | | |
| | 4 | | 5 | | 6 | |
| |_____| |_____| |_____| |
| _____ _____ |
| | | | | |
| | 7 | | 8 | |
| |_____| |_____| |
| |
However, the current issue is that the last row does not align as intended:
| _____ _____ _____ |
| | | | | | | |
| | 1 | | 2 | | 3 | |
| |_____| |_____| |_____| |
| _____ _____ _____ |
| | | | | | | |
| | 4 | | 5 | | 6 | |
| |_____| |_____| |_____| |
| _____ _____ |
| | | | | |
| | 7 | | 8 | |
| |_____| |_____| |
| |
It is crucial that the center alignment is maintained regardless of the number of columns. The solution should not involve adding filler divs or styling individual blocks. The layout must adapt dynamically without relying on a fixed number of blocks or rows.
Here is a reference pen for demonstration:
http://codepen.io/anon/pen/IDsxn
Is there a possible solution that doesn't rely on flexbox, considering support for older browsers like IE9? If a JavaScript solution is the only option, I would like to explore that as well.
Similar questions have been asked, but none have provided a comprehensive explanation:
How to align left last row/line in multiple line flexbox
CSS - Left align the last row of images in a centered div
Fix centering last line of elements in fluid container grid to be left aligned while container stays centered
Center multiple inline blocks with CSS and align the last row to the left