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

Reverse the order of only certain Div children elements

How can you reverse the order of only certain div’s children elements with pure CSS?

For example:

I want

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

<div id="parent">
    <a>A</a>
    <a>B</a>
    <a>C</a>
    <a>D</a>
    <div id="sub-child">
        <a>X</a>
        <a>Y</a>
        <a>Z</a>
   </div>
</div>

to be displayed as:

D

C

B

A

X

Y

Z

I tried:

#parent {
    display:flex;
    flex-direction: column-reverse; 
}

However, It is reversing everything inside parent (Not giving desired result)

>Solution :

  1. apply display: flex on both #parent and #sub-child;
  2. give flex-direction: column-reverse for the #parent and flex-direction: column for #sub-child;
  3. use a negative order for #sub-child so to keep that node at the bottom
#parent {
 display: flex;
 flex-direction: column-reverse;
}

#sub-child {
  order: -1;
  display: flex;
  flex-direction: column;
}
<div id="parent">
    <a>A</a>
    <a>B</a>
    <a>C</a>
    <a>D</a>
    <div id="sub-child">
      <a>X</a>
      <a>Y</a>
      <a>Z</a>
    </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