Grid

Create a fully responsive, fluid and nestable grid layout.

Grid

Create a fully responsive, fluid and nestable grid layout.

The Grid system of UIkit allows you to arrange block elements in columns. It works closely together with the Width component to determine how much space of the container each item will take up, and it can also be combined with the Flex component to align and order grid items.


Usage

To create the grid container, add the uk-grid attribute to a <div> element. Add child <div> elements to create the cells. By default, all grid cells are stacked. To place them side by side, add one of the classes from the Width component. Using .uk-child-width-expand will automatically apply equal width to items, regardless of how many there are.

Note There’s no need to add a .uk-grid class as it will be added via JavaScript. However, if UIkit’s JavaScript is deferred, the class should be added to prevent stacking while loading.

<div uk-grid>
    <div></div>
    <div></div>
</div>

Note Often cards from the Card component are used inside a grid. This also goes for the following examples for visualization.


Gap modifiers

The Grid component comes with a default gap that is decreased automatically from a certain breakpoint usually on a smaller desktop viewport width. To apply a different gap, add one of the following classes.

ClassDescription
.uk-grid-smallAdd this class to apply a small gap.
.uk-grid-mediumAdd this class to apply a medium gap like the default one, but without a breakpoint.
.uk-grid-largeAdd this class to apply a large gap with breakpoints.
.uk-grid-collapseAdd this class to remove the grid gap entirely.
<div class="uk-grid-small" uk-grid></div>

Column and Row

To apply a different gap to just the column or row, add one of the following classes.

ClassDescription
.uk-grid-column-small
.uk-grid-row-small
Add one of these classes to apply a small gap to the column or row.
.uk-grid-column-medium
.uk-grid-row-medium
Add one of these classes to apply a medium gap to the column or row.
.uk-grid-column-large
.uk-grid-row-large
Add one of these classes to apply a large gap to the column or row.
.uk-grid-column-collapse
.uk-grid-row-collapse
Add one of these classes to remove the grid gap entirely from the column or row.
<div class="uk-grid-column-small uk-grid-row-large" uk-grid></div>

Nested grid

You can easily extend your grid layout with nested grids.

<div uk-grid>
    <div>
        <div uk-grid>
            <div></div>
            <div></div>
        </div>
    </div>
    <div>
        <div uk-grid>
            <div></div>
            <div></div>
        </div>
    </div>
</div>

Divider modifier

Add the .uk-grid-divider class to separate grid cells with lines. This class can be combined with the gap modifiers. As soon as the grid stacks, the divider lines will become horizontal.

<div class="uk-grid-divider" uk-grid></div>

Match height

To match the height of the direct child of each cell, add the .uk-grid-match class. This is needed to match the height of cards from the Card component.

<div class="uk-grid-match" uk-grid></div>

Match only one cell

You can also match the height of the direct child of just one cell. To do so, add the .uk-grid-item-match class to the grid item whose child you want to match.

<div uk-grid>
    <div class="uk-grid-item-match"></div>
    <div></div>
</div>

Targets

For a more specific selection of the elements whose heights should be matched, add the target: SELECTOR option to the uk-height-match attribute from the Height component.

<div uk-grid uk-height-match="target: > div > .uk-card">
    <div>
        <div class="uk-card uk-card-default"></div>
    </div>
    <div>
        <div class="uk-card uk-card-default"></div>
    </div>
</div>

Grid and width

The grid is mostly used in combination with the Width component. This allows for great flexibility when determining the column widths.

<div uk-grid>
    <div class="uk-width-auto@m"></div>
    <div class="uk-width-1-3@m"></div>
    <div class="uk-width-expand@m"></div>
</div>

Child width

If the grid columns are evenly split, you can add one of the .uk-child-width-* classes to the grid container instead of adding a class to each of the items.

<div class="uk-child-width-1-2@s uk-child-width-1-3@m" uk-grid></div>

For more detailed information, take a look at the Width component.


Grid and flex

You can easily combine the grid with the Flex component. That way you can modify the items’ alignment, ordering, direction and wrapping. This allows you, for example, to flip the cells’ display order for wider viewports. All this works together with the gap and divider modifiers.

<div class="uk-flex-center" uk-grid>
    <div></div>
    <div class="uk-flex-first"></div>
</div>

Masonry

If grid cells have different heights, a layout free of gaps can be created by adding masonry: true to the attribute.

<div uk-grid="masonry: true"></div>

Note You can view more examples in the tests for the Grid Masonry.


Filter and order

Grid items can also be filtered and sorted by category, date or other meta data. Take a look at the Filter component.


Parallax

To move single columns of a grid at different speeds while scrolling, just add parallax: NUMBER to the attribute. The number sets the parallax translation in pixels.

<div uk-grid="parallax: 150"></div>

This effect can be applied to two types of markup. The following example uses three defined columns with three items each.

The parallax effect is also applied if grid columns wrap into the next row, as shown in the next example.

Note You can view more examples in the Grid Parallax tests.


Component options

Any of these options can be applied to the component attribute. Separate multiple options with a semicolon. Learn more

OptionValueDefaultDescription
margin Stringuk-grid-marginThis class is added to items that break into the next row, typically to create margin to the previous row.
first-columnStringuk-first-columnThis class is added to the first element in each row.
masonryBooleanfalseEnables masonry layout for this grid.
parallaxNumber0Parallax translation value. The value must be a positive integer. Falsy disables the parallax effect (default).

JavaScript

Learn more about JavaScript components.

Initialization

UIkit.grid(element, options);