I need to place some DIVs inside their parent container, no matter if it is the body, table's cell or another div, with Bootstrap 5, like shown on the attached illustration:
In case of even number of DIVs they should take 50% of the available width placed by 2 at a single line. Or, in case there is odd number of DIVs, the last one should take 100% of the available width while all previous still take 50% of the available width and are placed by 2 at a single line.
Preferably with possibility to change the DIVs order (like on the Mobile View example).
There is no problem to make this with the UIKit using some simple code like
<div class="uk-grid">
<div class="uk-width-large-1-2">DIV 1 takes 50% of available width</div>
<div class="uk-width-large-1-2">DIV 2 takes 50% of available width</div>
<div class="uk-width-large-1-2">DIV 3 takes 100% of available width</div>
</div>
But whatever I've searched for with Bootstrap's documentation (as well as Stack Overflow) I still can't find the solution.

Bootstrap is mobile-first, meaning whatever we define at smaller breakpoints will cascade up to larger breakpoints until overridden.
There are 5 explicit breakpoints in addition to the implicit (default) mobile breakpoint:
To resize the
divsUse columns with the responsive breakpoint syntax:
col-12specifies full width (12 of 12) at mobile and abovecol-md-6specifies half width (6 of 12) atmdand above (i.e., starting atmd, this rule overrides thecol-12rule)g-2specifies gutters to auto-pad the columns (or use manual spacing utilities)Note that the written order (
col-12 col-md-6vscol-md-6 col-12) is irrelevant, as with any css classes. Bootstrap applies the styles in mobile-first order.To auto-expand the last
divIf you are using a templating language, I suggest putting this logic in the template. That's a bit out of scope for this question, but for example with django, a minimal template might look like:
Or to handle it with pure css, you could add a
widthrule targeting the lastcolif odd:To reorder the
divsUse the responsive flex
orderutilities:order-2 order-md-1specifies position 2 at mobile and above, position 1 atmdand aboveorder-1 order-md-2specifies position 1 at mobile and above, position 2 atmdand aboveNote that the parent container needs to be a flex container. A bootstrap
rowis flex by default, but for non-flex containers, add thed-flexclass explicitly.Minimal example