2

If i have the following HTML snippet

...
<fieldset>
  <div>above or below</div>
  <div>content</div>
</fieldset>
...

How can I create CSS such that the <div>above or below</div> could be positioned above or below <div>content</div> without having to add repetitive markup and without affecting the document flow?

I have tried using position: relative; on the fieldset and then position: absolute; on the field div but the element then sits over any elements below it.

2
  • Are you referring to above and below as depth, i.e: z-index or are you talking about a vertical position? Commented Jun 12, 2018 at 14:53
  • 1
    vertical position Commented Jun 12, 2018 at 16:52

1 Answer 1

2

One option you have would be to use display: flex and flex-direction in order to visually reverse the order of the two elements:

fieldset {
display: flex;
flex-direction: column;
}

.below {
flex-direction: column-reverse;
}
<fieldset>
  <div>above or below</div>
  <div>content</div>
</fieldset>

<fieldset class="below">
  <div>above or below</div>
  <div>content</div>
</fieldset>

You can also use display: flex and order if you need to add more elements into fieldset:

fieldset {
  display: flex;
  flex-direction: column;
}

.first {
  order: 1;
}

div {
  order: 2;
}
<fieldset>
  <div>above or below</div>
  <div>content</div>
  <div>other stuff</div>
  <div>other stuff</div>
</fieldset>

<fieldset>
  <div>above or below</div>
  <div class="first">content</div>
  <div>other stuff</div>
  <div>other stuff</div>
</fieldset>

Sign up to request clarification or add additional context in comments.

1 Comment

For some reason I could not get it to work with fieldset when I switched to a div it worked fine

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.