Hello. Task: Parent element with rubber width (width: 94%, width: 96% and similar variants). Inside the blocks are fixed width (width: 120px or 180px or another size). The number of these blocks is not fixed, it can be from 1 to infinity.
Task: align the current width of the justify parent element.
On pure CSS, this is not done, so go to js (jquery). The only thing that could be found:
$(function () { changeMargin(); }); $(window).resize(function() { changeMargin(); }); function changeMargin() { var container_width = $('div.list').width(); var item_width = $('div.item').width(); var items_count = Math.floor(container_width / item_width); var items_width = item_width * items_count; var difference = container_width - items_width; var margin = difference / (items_count - 1); $('div.item').each(function(index) { if (index > 0 && index % items_count != 0) $(this).css('margin-left', margin+'px'); else $(this).css('margin-left', '0px'); }); } .list { width: 95%; margin: 0 auto; } .item { width: 163px; height: 110px; float: left; background: #ff0; margin-bottom: 10px; } <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script> <div class="list"> <div class="item">Content</div> <div class="item">Content</div> <div class="item">Content</div> <div class="item">Content</div> <div class="item">Content</div> <div class="item">Content</div> </div> But there are two problems: 1. If the elements are not enough even to fill the first and only line, the blocks stick together. - it is necessary in such cases to add indent anyway, at least 10px 2. On small screens, for some reason, the structure is completely broken, i.e. The script defines crookedly where the last element in the line is, and on the new line two elements are stuck together.
Here, too, everything broke ... Tell me, maybe there are options to correctly implement automatic selection of the indent?
Explanatory picture. Suppose that the width of the blocks is set in styles (the way it is) and is equal to 180px. Depending on the width of the screen, the script calculates how much it can fit and selects indentation for all elements (except the first one if margin-left is used, or the last one if margin-right is used). At the same time, the indent should not be less than 5px. If it does not fit, the block is transferred to the next row. The width of the parent element is not rubber. Also, the blocks also have several widths (for adaptability) - all sizes are written in styles for the desired screen resolutions. Those. the width of the block in the script itself cannot be hardcoded.

выровнять по текущей ширине родительского элементаHOW? - Jean-Claude