CSS Grid - Auto height rows, sizing to content

后端 未结 3 2113
花落未央
花落未央 2020-11-30 03:23

I have two grids nested within a grid. Unfortunately the right nested grid .grid-3 sets the height of the rows so that both the left and right grid are the same

3条回答
  •  温柔的废话
    2020-11-30 03:52

    You can try minmax(min-content, max-content) ref

    div {
      border: 1px dotted black;
    }
    .grid-2 {
      display: grid;
      grid-template-columns: repeat(2, 1fr);
      grid-auto-rows: minmax(min-content, max-content);
    }
    
    .grid-3 {
      display: grid;
      grid-template-columns: 1fr 1fr 1fr;
      grid-auto-rows: minmax(min-content, max-content);
    }
    
    .left {
      background-color: red;
    }
     
    .right {
      background-color: green;
    }
    L
    L
    L
    L
    L
    L
    L
    L
    L
    L
    L
    L
    L
    L
    L
    L
    L
    L
    R
    R
    R
    R
    R
    R
    R
    R
    R
    R
    R
    R
    R

    You can also use only max-content or min-content

    div {
      border: 1px dotted black;
    }
    .grid-2 {
      display: grid;
      grid-template-columns: repeat(2, 1fr);
      grid-auto-rows: max-content; /* min-content*/
    }
    
    .grid-3 {
      display: grid;
      grid-template-columns: 1fr 1fr 1fr;
      grid-auto-rows: max-content; /* min-content*/
    }
    
    .left {
      background-color: red;
    }
     
    .right {
      background-color: green;
    }
    L
    L
    L
    L
    L
    L
    L
    L
    L
    L
    L
    L
    L
    L
    L
    L
    L
    L
    R
    R
    R
    R
    R
    R
    R
    R
    R
    R
    R
    R
    R

提交回复
热议问题