search

Home  >  Q&A  >  body text

How to place grid elements at the end of a container

<p>I have a grid container and I want my items to be at the bottom. </p> <p>I have this actual</p> <p>I want this Expectations</p> <p> <pre class="brush:css;toolbar:false;">.container { display: grid; grid-template-columns: repeat(14, 1fr); grid-template-rows: repeat(6, 1fr); grid-gap: 8px; width: 200px; background: blue; } .item { background-color: red; color: white; } .item-1 { grid-column: span 5; grid-row: span 6; } .item-2 { grid-column: span 4; grid-row: span 5; } .item-3 { grid-column: span 3; grid-row: span 4; }</pre> <pre class="brush:html;toolbar:false;"><div class="container"> <div class="item item-1">item 1</div> <div class="item item-2">item 2</div> <div class="item item-3">item 3</div> </div></pre> </p> <p>PS: I have tried place-items: end; and align-items: end;</p>
P粉387108772P粉387108772510 days ago443

reply all(1)I'll reply

  • P粉710478990

    P粉7104789902023-08-29 09:06:26

    You can use the two values ​​​​grid-column and grid-rowstart / end syntax to determine the start and end position of the item.

    For example, grid-row: 2 / span 3; will start at the 2nd grid line, span 3 grid lines, and end at the 5th grid line.

    .container {
      display: grid;
      grid-template-columns: repeat(14, 1fr);
      grid-template-rows: repeat(6, 1fr);
      grid-gap: 8px;
      width: 200px;
      background: blue;
    }
    .item {
      background-color: red;
      color: white;
    }
    .item-1 {
      grid-column: span 5;
      grid-row: span 6;
    }
    .item-2 {
      grid-column: 6 / span 4;
      grid-row: 2 / span 5;
    }
    .item-3 {
      grid-column: 10 / span 3;
      grid-row: 3 / span 4;
    }
    <div class="container">
      <div class="item item-1">Item 1</div>
      <div class="item item-2">Item 2</div>
      <div class="item item-3">Item 3</div>
    </div>

    reply
    0
  • Cancelreply