Why auto-fit or auto-fill not working properly with minmax?

后端 未结 4 619
臣服心动
臣服心动 2021-02-10 16:17

It doesn\'t work properly when I add auto-fit or auto-fill instead of a number.

 grid-template-columns: repeat(auto-fit, minmax(max-content, max-content));
         


        
4条回答
  •  北海茫月
    2021-02-10 17:01

    I recently came across this problem as well. From the specs

    The generic form of the repeat() syntax is, approximately,

    repeat( [ | auto-fill | auto-fit ] , ) The first argument specifies the number of repetitions. The second argument is a track list, which is repeated that number of times. However, there are some restrictions:

    The repeat() notation can’t be nested.

    Automatic repetitions (auto-fill or auto-fit) cannot be combined with intrinsic or flexible sizes.

    After a few days of trying, it seems that any use of min-content, max-content, or auto in the repeat will flat-out be ignored, even inside of minmax. Replacing one of them with a fixed-number works because it simply defaults to that. I did manage to create a programmatic workaround using ResizeSensor and jQuery, though.

    var GRID_GAP = 3; // GRID-GAP of the grid
    var iGridW = 0; // LAST comparative value of grid item width
    var sizingWIP = false;
    var GRID_LAYOUT = {
      'maxwidth': { /* CSS settings to compare item widths */
        display: "grid",
        gap: GRID_GAP + "px",
        "grid-template-columns": "none",
        "grid-auto-columns": "max-content"
      },
      'normal': { /* Normal CSS style to display the grid */
        display: "grid",
        gap: GRID_GAP + "px",
        "grid-template-columns": "repeat(auto-fit, minmax(" + iGridW + "px, 1fr)",
        "grid-auto-columns": "none"
      }
    };
    
    $(function() { // Shorthand for document.ready event
      evalGridItemMaxSize();
    };
    
    new ResizeSensor(document.getElementById("grdContainer"), function() {
      if(sizingWIP) return;
      sizingWIP = true;
      evalGridItemMaxSize();
      sizingWIP = false;
    });
    
    
    // EVALUATE GRID ITEM WIDTH TO MAX-CONTENT FOR *ALL* COLUMNS (equivalent to "grid-template-columns: repeat(auto-fit, max-content)" which *cannot* be set in CSS
    //    (repeat(auto-fill... & repeat(auto-fit... *cannot* be used with intrinsic {min-content, max-content, auto})
    function evalGridItemMaxSize() {
      $("#grdSelector").css(GRID_LAYOUT['maxwidth']);
      if ($("#someGridItemID").outerWidth() === iGridW) {
        $("#grdSelector").css(GRID_LAYOUT['normal']);
        return; //Exit if grid item max-width is same as last time
      }
    
      // Re-evaluate grid item width as a function of max-content of *all* items
      iGridW = $("#someGridItemID").outerWidth();
      GRID_LAYOUT['normal']['grid-template-columns'] = "repeat(auto-fit, minmax(" + iGridW + "px, 1fr)"
      $("#grdSelector").css(GRID_LAYOUT['normal']);
    }
    
    
    

    evalGridItemMaxSize() sets the whole grid to a single column with size of max-content so all items will be as wide as the widest item. It then captures that width from one of the elements in the grid and recreates the columns by inserting that width (as a fixed number) into the minmax function in grid-template-columns: repeat(auto-fit....
    Not sure if it's exactly what you're looking for, but I was sure happy when I figured this out :) Hope it helps.
    NOTE: As I stated, this will size all columns to be the same (minimum) width as the widest element

提交回复
热议问题