My responsive website includes the following HTML code:
<div id="fpma">
<span class="fpm"></span>
<span class="fpm"></span>
<span class="fpm"></span>
<span class="fpm"></span>
<span class="fpm"></span>
<span class="fpm"></span>
<span class="fpm"></span>
<span class="fpm"></span>
</div>
The width of #fpma container varies based on the screen size, while the elements within .fpm have fixed widths. The current CSS looks like this:
#fpma {
text-align: right;
width: 100%;
}
.fpm {
width: 48px;
height: 33px;
display: inline-block;
margin: 4px;
background:url('images/bar.png') center center no-repeat;
}
I am seeking a reusable solution to automatically rearrange the items as the parent container's width decreases in the following manner:
1 row - 8 elements
2 rows - 4 elements
4 rows - 2 elements
8 rows - 1 element
I would prefer a solution that does not rely on media queries and allows for flexibility without requiring additional wrappers in the HTML structure.
If utilizing CSS3 features like nth-child, :before/:after, flexbox, etc., is necessary, it is welcomed. However, any solution should aim to avoid dependency on media queries due to their fixed max-width/min-width constraints. If providing a media query alternative along with reasons for its necessity, that would also be acceptable. I have experimented with various approaches but have yet to achieve the desired result.
If needed, changing the display form of the elements is permissible.