Re-order the appearance of grid items using the order property

Rory Smith
InstructorRory Smith
Share this video with your friends

Social Share Links

Send Tweet
Published 8 years ago
Updated 6 years ago

As with flex items, we can set an order value on grid items. Let’s see how this affects the DOM and the appearance of our grid.

[00:00] In this example, we have five grid items inside a container. The container's a display grid with a 10-pixel gutter and it's taking up all the available height. Each grid item is being displayed in a linear way as per the way we've described it in our markup.

[00:19] In some instances, we might find that we want to keep the order specified in our markup, but we might want to reorder how each item looks on the page. For example, we might want to reorder the elements on a mobile layout differently to how they look on tablet layout.

[00:41] By default, each element has an order value of 0If we were to go under the hood here and inspect each element, here's the order value and by default it's 0Each one of these values has an order value of 0If we were to target one of our grid items -- let's say the second one -- and we gave it order one, then it moves right to the end. The reason is that everything else has an order of 0and 2 has an order of 1.

[01:22] We could also use minus values here. Let's target the fifth one. Say order-1 and it puts it to the start. Note that the order in the dom hasn't changed at all from how we specified it in our markup.

[01:42] Also, note that if we specify grid column and grid row values for our grid items they will also reorder the elements. In this case, if we took off our order property and we said grid row 1, we'd get the same result.