---
title: "Sizing and direction"
description: "Give the grid a probe so it knows the item size, then make it scroll sideways."
canonical_url: "https://vvirtual-grid.netlify.app/guides/sizing-and-direction"
---
# Sizing and direction

> Give the grid a probe so it knows the item size, then make it scroll sideways.

The grid places items with arithmetic, so it needs to know how big one item is. It cannot read the size of items that are not rendered. In this guide you give the grid a probe that tells it the item size, make that size change with the screen width, and then turn the grid sideways.

The grid places every item as if it were the size of the probe. Your CSS sets the actual size, so keep the item size equal to the probe's size.

<note>

The samples use Tailwind CSS classes for the track sizes, gaps and breakpoint. The grid reads the computed CSS, so your own classes work the same.

</note>

## Set the item size with a probe

The grid needs three things from your markup: a scroll container, grid tracks, and a probe that has the size of one item.

<steps>

### Set up the scroll container, the tracks and the probe

Put the grid inside an element with `overflow-y: auto` and a fixed height. Put `overflow` and the height on that element, not on `<VirtualGrid>`. The [VirtualGrid reference](https://vvirtual-grid.netlify.app/reference/virtual-grid) explains what happens when you set them on the grid.

Add `grid` and column tracks to the class of `<VirtualGrid>`. The grid reads the column count from the computed `grid-template-columns`, and the gaps from `column-gap` and `row-gap`.

Add a `probe` slot with one empty element that has the same classes as your items. The grid renders the probe hidden and measures it with a `ResizeObserver`, a browser API that reports when an element's size changes. It places every item as if it had the probe's size. The item classes set the actual size, so give them the same height as the probe.

The page provider in this sample builds each page from its page number.

```vue
<script setup>
const length = 10_000;
const pageProvider = (page, pageSize) =>
  Array.from({ length: pageSize }, (_, i) => ({ title: `Item ${page * pageSize + i}` }));
</script>
```

The template has the scroll container, the column tracks and the probe.

```vue
<template>
  <div class="h-[600px] overflow-y-auto">
    <VirtualGrid
      :length="length"
      :page-size="40"
      :page-provider="pageProvider"
      class="grid grid-cols-[repeat(auto-fill,minmax(160px,1fr))] gap-4"
    >
      <template #default="{ item, style }">
        <div :style="style" class="h-40 md:h-64">{{ item.title }}</div>
      </template>

      <template #probe>
        <div class="h-40 md:h-64" />
      </template>
    </VirtualGrid>
  </div>
</template>
```

### Check the size

Open the page, right-click an item and choose Inspect. In the Elements panel, the item's box is 160px tall below the `md` breakpoint and 256px tall above it. Resize the window across the breakpoint. The items move to their new positions, and the rows stay even, with no overlap.

</steps>

<warning>

The grid positions every item as if it had the probe's size. An item that is taller than the probe overlaps the next row, and a shorter one leaves a gap.

</warning>

The [`probe` slot reference](https://vvirtual-grid.netlify.app/reference/virtual-grid#slots) explains the alternative: set the row height on the grid with `grid-template-rows` or `grid-auto-rows`.

## Scroll sideways

The grid scrolls sideways when you set `grid-auto-flow: column` and a fixed number of rows. The grid then sets its width to the width of the whole list, and the parent scrolls on the x axis.

<steps>

### Put the grid in a horizontally scrolling container and set the flow

Use an element with `overflow-x: auto`. It needs no height. The fixed rows give the grid its height.

Add `grid-flow-col` to the grid. Fix the height of the rows with `grid-rows-[repeat(2,150px)]` and the width of each column with `auto-cols-[200px]`. Give the items and the probe the size of one cell. The script stays the same as in [Set up the scroll container, the tracks and the probe](#set-up-the-scroll-container-the-tracks-and-the-probe).

```vue
<template>
  <div class="overflow-x-auto">
    <VirtualGrid
      :length="length"
      :page-size="40"
      :page-provider="pageProvider"
      class="grid grid-flow-col grid-rows-[repeat(2,150px)] auto-cols-[200px] gap-4"
    >
      <template #default="{ item, style }">
        <div :style="style" class="h-[150px] w-[200px]">{{ item.title }}</div>
      </template>

      <template #probe>
        <div class="h-[150px] w-[200px]" />
      </template>
    </VirtualGrid>
  </div>
</template>
```

### Check the order

Scroll to the start of the list. In the Elements panel, items 0 and 1 sit in the first column, one above the other, and item 2 starts the second column. [`getItemCell`](https://vvirtual-grid.netlify.app/reference/primitives#getitemcell) returns the same row and column for an index, and [`getIndexByCell`](https://vvirtual-grid.netlify.app/reference/primitives#getindexbycell) returns the index from a cell.

</steps>


## Sitemap

See the full [sitemap](https://vvirtual-grid.netlify.app/sitemap.md) for all pages.
