Moving checked items to the top of a list is a common way to make selection state readable at a glance — a to-do list being the canonical case, where finished tasks step aside so the remaining ones stay in focus. The pattern below goes one step further: selected items are not just relocated but laid out horizontally with CSS Grid, so they read as a distinct group rather than a relocated slice of the same column.

Two Grid techniques get there. auto-fill keeps the layout stable as long as the selected items stay within the container's bounds; the span keyword is the more flexible option when they don't.

The shared markup

Both methods render the same HTML, so the difference lives entirely in CSS.

<ul>
  <li>
    <label>
      <input type="checkbox" />
      <div class=icon>🍱</div>
      <div class=text>Bento</div>
    </label>
  </li>
  <li>
    <label>
      <input type="checkbox" />
      <div class=icon>🍡</div>
      <div class=text>Dangos</div>
    </label>
  </li>
  <!-- more list items -->
</ul>

An unordered list carries the items, though <ul> and <li> are not strictly required since the grid properties do the positioning. Each <input> is placed inside an implicit <label> — a way to avoid an extra wrapper rather than an accessibility recommendation, since explicit labels are generally better supported by assistive technologies.

Approach 1: letting auto-fill place the selection

ul {
  width: 250px;
  display: grid;
  gap: 14px 10px;
  grid-template-columns: repeat(auto-fill, 40px);
  justify-content: center;
  /* etc. */
}

The list becomes a grid container with 14px row gaps and 10px column gaps, and its content justified to center.

On grid-template-columns, auto-fill takes the place of the repeat count: columns of the declared size (40px here) repeat as many times as fit within the grid container. At rest, the items need to sit in a single column, so all of them are pinned to column one:

li {
  width: inherit;
  grid-column: 1;
  /* Equivalent to: 
      grid-column-start: 1;
      grid-column-end: auto; */
  /* etc. */
}

The switch happens through :has(): an <li> that contains a :checked checkbox is given a grid-area placing it in the first row, with its column auto-placed in that row according to the container's grid-template-columns. Selected items therefore collect at the top of the list, each in its own column:

li {
  width: inherit;
  grid-column: 1;
  /* etc. */
  &:has(:checked) {
    grid-area: 1;
    /* Equivalent to: 
      grid-row-start: 1;
      grid-column-start: auto;
      grid-row-end: auto;
      grid-column-end: auto; */
    width: 40px;
    /* etc. */
  }
  /* etc. */
}

Approach 2: spanning tracks with span

This version drops grid-template-columns altogether. The updated container ruleset gains justify-items, which handles item alignment later on:

ul {
  width: 250px;
  display: grid;
  gap: 14px 10px;
  justify-content: center;
  justify-items: center;
  /* etc. */
}

The item ruleset changes too: items still start out in the first column, but each now spans six column tracks — one per item. When selecting items introduces extra columns, the unselected items below stay in a single column under the selected group, yet their spans reach across those tracks, and justify-items: center keeps everything centered.

li {
  width: inherit;
  grid-column: 1 / span 6;
  /* Equivalent to: 
     grid-column-start: 1;
     grid-column-end: span 6; */
  /* etc. */
}

The final styles for the item:

li {
  width: inherit;
  grid-column: 1 / span 6;
  /* etc. */
  &:has(:checked) {
    grid-area: 1;
    width: 120px;
    /* etc. */
  }
  /* etc. */
}

Selected items are wider than in the first example, which makes it possible to see how the selection UI behaves once the selected items overflow the container.

Matching selection order

Selected and unselected items always follow source order. When the on-screen sequence has to track the order in which the user made selections, assign each item an incremental order value dynamically at selection time.

onload = ()=>{
  let i=1;
  document.querySelectorAll('input').forEach((input)=>{
    input.addEventListener("click", () => {
      input.parentElement.parentElement.style.order = input.checked ? i++ : (i--, 0);
    });
  });
}

Choosing between the two

Both approaches need very little configuration. With auto-fill, selected items can be grouped along either axis without disturbing the unselected items sharing the same container — provided the selection does not overflow it. Once overflow is possible, span holds the layout together no matter how long the selected group grows along a given axis. Other layouts are possible within the same idea, such as grouping selections at the end of the list or transposing the horizontal and vertical structure.