Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Switching the order of child elements on the body with CSS

Short Story
Let’s say my HTML is already set in stone:

<body>

<div id="blockA">Block A</div>

<div>
<div id="blockB">Block B</div>
<div id="blockC">Block C</div>
</div>

</body>

It will look like this:

| Block A |
| Block B |
| Block C |

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

These are display flex and I was able to change the order using order: 0; and order: 1;
and It’s now look like this

| Block B |
| Block C |
| Block A |

Now I want to switch the order of the blocks. How can I do that with only CSS?
Kindly note that B and C are on the div and cannot be separated

| Block B |
| Block A |
| Block C |

>Solution :

You need to "unwrap" the other divs using display:contents.

body {
  display: flex;
}

div {
  border: 1px solid grey;
}

#wrap {
  display: contents;
}

#blockB {
  order: -1;
}
<div id="blockA">Block A</div>

<div id="wrap">
  <div id="blockB">Block B</div>
  <div id="blockC">Block C</div>
</div>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading