How to set auto-margin boxes in flexible-width design using CSS?

后端 未结 8 1716
一整个雨季
一整个雨季 2020-12-11 02:04

I have DIV with flexible width set e.g. min-width:800px and max-width:1400px. In this DIV, there are many boxes with fix width 200px and display:inline-block. So depending o

8条回答
  •  没有蜡笔的小新
    2020-12-11 02:44

    On my project I have faced with the same problem and I came to the next decision - the best way for me is to go with js, in my case you can have xxx count of block inside container, if there is enough space in 1st row the block from 2nd row goes up to the 1st row, and so on. here is an example http://jsfiddle.net/gVAjN/11/

     $(function() {
      // Call function when DOM is ready
      settingsAlignment();
    
    $(window).resize(function() {
          // Call function on window resize
            settingsAlignment();
        })
    
    $('#new_div').click(function() {
        box_number = $('.element').size();
        box_add = box_number + 1;
        $('.container').append($('
    Box'+ box_add + '
    ')) settingsAlignment(); }) function settingsAlignment() { // calculation of ul's padding-left and li's margin-right var settingsUl = $('.container'); settingsLi = $('.element'); ul_width = settingsUl.outerWidth(true); item_width = settingsLi.width(); min_gap = 7; effective_item_width = item_width + min_gap; items_in_row = Math.floor((ul_width - min_gap) / effective_item_width); gaps_sum = ul_width - items_in_row * item_width; new_gaps = gaps_sum / (items_in_row + 1); item_margin = Math.floor(new_gaps); row_width = (item_width + item_margin) * items_in_row - item_margin; console.log(row_width + '= row_width'); console.log(ul_width + '= ul_width'); ul_left_padding = Math.ceil((ul_width - row_width) / 2); console.log(ul_left_padding + '=ul_left_padding'); settingsUl.css('padding-left', ul_left_padding + 'px'); settingsLi.css('margin-right', item_margin + 'px'); console.log(settingsLi); } });

提交回复
热议问题