Wrap CSS grid with auto placement

前端 未结 2 1163
耶瑟儿~
耶瑟儿~ 2021-01-18 12:23

I\'m trying to build a grid that contains card-like items. The cells of each type (headline, image, text, button, ...) should have equal height in each row, determined by th

2条回答
  •  孤城傲影
    2021-01-18 12:50

    You have already a solution now, with a not ideal browser support, but anyway working, that is display: content

    You need to wrap your elements in an auxiliary div, I set those to the class card. Then, make card disappear of the layour with the display : content:

    .grid {
      display: grid;
      grid-gap: 10px;
      grid-auto-columns: 1fr;
      grid-auto-flow: dense;
    }
    
    .card {
      display: contents;
    }
    
    .card:nth-child(odd) * {
      grid-column-start: 1;
    }
    
    .card:nth-child(even) * {
      grid-column-start: 2;
    }
    
    @media screen and ( min-width: 1300px) {
      .card:nth-child(3n + 0) * {
        grid-column-start: 3;
      }
      .card:nth-child(3n + 1) * {
        grid-column-start: 1;
      }
      .card:nth-child(3n + 2) * {
        grid-column-start: 2;
      }
    }

    Header 1

    text

    Header 2 is longer and may span multiple lines

    Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis et cursus ligula. Maecenas non pharetra dui, eu tincidunt mi. Vivamus vitae luctus risus. Etiam vehicula sem est.

    Header 1

    Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis et cursus ligula. Maecenas non pharetra dui, eu tincidunt mi. Vivamus vitae luctus risus. Etiam vehicula sem est, non ultricies lectus placerat eget.

    Header 1

    Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis et cursus ligula. Maecenas non pharetra dui, eu tincidunt mi. Vivamus vitae luctus risus. Etiam vehicula sem est, non ultricies lectus placerat eget. Suspendisse pulvinar arcu massa, quis varius velit facilisis tincidunt. Proin sed cursus orci.

    Header 1

    Lorem ipsum dolor sit amet, consectetur adipiscing elit.

    I just missed the easiest solution:

    Since you have already a flat HTML, and are intending to use media queries, you can just use the same idea, (nth- selectors) but on the flat HTMl

    .grid {
      display: grid;
      grid-gap: 10px;
      grid-auto-flow: dense;
      grid-auto-rows: 1fr;
    }
    
    .card {
      display: contents;
    }
    
    h2,
    img,
    p,
    button {
      grid-column-start: 1;
    }
    
    h2:nth-of-type(even),
    img:nth-of-type(even),
    p:nth-of-type(even),
    button:nth-of-type(even) {
      grid-column-start: 2;
    }
    
    @media screen and ( min-width: 1300px) {
    
    h2:nth-of-type(3n + 1),
    img:nth-of-type(3n + 1),
    p:nth-of-type(3n + 1),
    button:nth-of-type(3n + 1) {
      grid-column-start: 1;
    }
    h2:nth-of-type(3n + 2),
    img:nth-of-type(3n + 2),
    p:nth-of-type(3n + 2),
    button:nth-of-type(3n + 2) {
      grid-column-start: 2;
    }
    h2:nth-of-type(3n),
    img:nth-of-type(3n),
    p:nth-of-type(3n),
    button:nth-of-type(3n) {
      grid-column-start: 3;
    }
    
    }

    Header 1

    text

    Header 2 is longer and may span multiple lines

    Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis et cursus ligula. Maecenas non pharetra dui, eu tincidunt mi. Vivamus vitae luctus risus. Etiam vehicula sem est.

    Header 1

    Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis et cursus ligula. Maecenas non pharetra dui, eu tincidunt mi. Vivamus vitae luctus risus. Etiam vehicula sem est, non ultricies lectus placerat eget.

    Header 1

    Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis et cursus ligula. Maecenas non pharetra dui, eu tincidunt mi. Vivamus vitae luctus risus. Etiam vehicula sem est, non ultricies lectus placerat eget. Suspendisse pulvinar arcu massa, quis varius velit facilisis tincidunt. Proin sed cursus orci.

    Header 1

    Lorem ipsum dolor sit amet, consectetur adipiscing elit.

提交回复
热议问题