Home CSS How to change the order of elements in a container with CSS?
CSS

How to change the order of elements in a container with CSS?

There are many scenarios where one would want to change the order of elements in a container using CSS, but I find I use the CSS order property the most when I want to change the order of elements stacked on the smaller resolutions.

So this is how the issue might occur. We have a few elements laid out one next to the other on desktop and large-screen devices.

Css order property

However, when we change the resolution or open our layout on the small screen, the elements would stack in the order they are added in the code (be it HTML, JSX, or whatever). And here lies our problem.

Css order property

Even though everything is displaying correctly, the order of the columns is wrong. We want it to follow a natural flow, in this case, “text-image followed by text-image”. We can easily fix this with a line or two of CSS using the CSS order property.

HOW TO USE CSS ORDER PROPERTY

CSS order property specifies the visual order of the elements in a container, which are by default lined up as in our source code.

One thing to note is that the order property will work only for containers that are displayed as grid or flex.

So, what I might do in the example above is to display the container (div, section..) as grid (or flex):

.container { display: grid; }

and then change the order of my elements by adding a class (for example, .order-2) to the element I want to change, and on which resolution (in my case, on mobile phones, below the width of 480px):

@media screen and (max-width: 480px) {

    .order-2 { order: 2; }

}

And we’re good to go.

CSS order property solution
Written by
Igor Bujas

I am a UI designer and front-end developer professional working mostly in the Shopify ecosystem. I've been working in the industry for so long that I was fixing bugs for IE5, and animating in Flash while it was still a Macromedia product. While not differentiating between the UI and the code, I am always looking to get outdoors for some alpine climbs or scuba dives. I also work as a Krav Maga instructor in Zagreb, Croatia.

Leave a comment

Related Articles

Tailwind CSS
CSSEnvironment Setup

A step-by-step tutorial on adding Tailwind CSS to your website

* Photo by Ross Parmly Tailwind CSS is a utility-first CSS framework...

CSSShopify for DevelopersShopify OS 2.0

New CSS for specific sections field in the Shopify 2.0 themes

Shopify launched a neat new feature for 2.0 themes to quickly add...

CSSShopify for Developers

Append a class to the Shopify menu item

Sometimes you need one menu item to be different than the rest...

CSS

Create a smooth scroll effect with CSS only

We used to reach for Javascript to achieve a smooth scrolling effect...

4 proven ways to speed up your Shopify store.

    We respect your privacy. Unsubscribe at anytime.