Float from the bottom and then rise up

I'm attempting to use CSS to make code from further down in the HTML appear above code from a section above it.

Here is how my HTML structure looks:

<div class="showbelow">
show below
</div>

<div class="showabove">
show above
</div>

I would like the content of the showabove div to display above the showbelow div, even though it appears below it in the HTML. Is there any way to achieve this without relying on negative margins in the CSS? For example, without setting a negative top margin for showabove and a positive top margin for showbelow? Both divs take up 100% of the container's width.

Thank you in advance :)

Answer №1

Utilizing the order property in flexbox allows for precise item placement within a container.

Take a look at this example:

.container {
  display:flex;
  flex-direction:column;
}
.container div {
  border:1px solid red;
}
.showabove {
  order:1;
}
.showbelow {
  order:2;
}
<div class="container">
  <div class="showbelow">show below</div>
  <div class="showabove">show above</div>
</div>

Flex items typically follow the same order as they appear in the source document. However, the order property can override this.
https://drafts.csswg.org/css-flexbox-1/#order-property

Exercise caution when using order:-1!

.container {
  display:flex;
  flex-direction:column;
}
.container div {
  border:1px solid red;
}
.showabove {
  order:-1;
}
<div class="container">
  <div class="showbelow">show below</div>
  <div class="showmiddle">show middle</div>
  <div class="showabove">show above</div>
</div>

Explanation: Setting order:-1 places the item first within the container due to all other items defaulting to order:0. It does not shift the item before the previous one! While this solution may work initially, adding more items could lead to complications, requiring you to set specific order values for each item within the container, as showcased in the example above.

Answer №2

Utilizing Flexbox, you have the ability to rearrange the order of elements. To accomplish this, simply apply order: -1 to the .showabove div.

body {
  display: flex;
  flex-direction: column;
}
.showabove {
  order: -1;
}
<div class="showbelow">show below</div>
<div class="showabove">show above</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the process to create a sticky Material UI grid element?

Currently, I am in the process of developing the front-end for a shopping cart. To organize the content, I have split the container into two columns using Grid container and Grid item. In each column, various components are placed - the left side containin ...

Having trouble with uploading image files in Laravel Vue.js?

I have been trying to upload my image file to a storage folder using Laravel and Vue.js. However, when I try to print the data using dd();, I get no response or error. How can I tell if I am doing it correctly? I suspect the error lies in the formData whe ...

Using jQuery to Toggle Visibility of Table Rows

I'm facing a challenge in creating a basic table layout where all the table rows are initially hidden when the document loads. The goal is to display specific rows when a corresponding button is clicked, but my current implementation doesn't seem ...

Retrieving data from an HTML input tag and storing it in a variable

I'm currently working on a project that involves reading the contents of an uploaded file through an input tag and storing it in a variable. My goal is to then use an algorithm to decrypt the .txt file: <input type="button" value="decrypt" id="dec ...

Is there a way to make all Bootstrap column heights equal using only jQuery?

I am currently working on matching the heights of two columns without using an existing library like match-height.js. In this specific case, I have a row with 2 columns where the first column contains a centered black square and the second column contains ...

Interactive calendar feature displaying events upon hovering over a date

I need some assistance with displaying a drop-down list on full calendar events when hovering over the events. Can someone provide guidance? Here is a glimpse of what I currently have: I've attempted setting the z-index, but I can't seem to get ...

Adjusting the grid for various mobile screen sizes

I am currently working on designing a user interface (UI) for mobile screens that includes a header, grid, and buttons. I want the UI to look consistent across all mobile devices. Here are screenshots from various mobile screens: Samsung S5 Pixel 2 XL I ...

Discover the name of a color using its HEX code or RGB values

Is there a way to retrieve the color name from RBG or HEX code using JavaScript or JQuery? Take for instance: Color Name RGB black #000000 white #FFFFFF red #FF0000 green #008000 ...

Ensure each checkbox within a list is selected

My dilemma involves an assortment of checkboxes enclosed within LIs and SPANs in an unordered list. To simplify the checking process, I attempted using jQuery to automatically check all boxes when a button positioned above the UL is clicked. However, my ...

CSS login validator immobilized

In my Visual Studio project, I am facing an issue with applying CSS to two requiredfield validators. I have tried using the following code: #vdtrUserLogin { top:65px; left:750; position:absolute} #vdtrPasswordLogin0 { top:65px; left:900; position:absolute ...

Tips for evenly and fully stretching a set number of horizontal navigation items across a designated container

I want to evenly distribute 6 navigation items across a 900px container, with equal amounts of white space between each item. Here is an example: ---| 900px Container |--- ---| HOME ABOUT BASIC SERVICES SPECIALTY SERVICES OUR STAFF CONTACT ...

Seeking particular section of online content in an asynchronous manner

Is there a method for asynchronously requesting a specific portion of a web resource (like the initial 100 bytes) using JavaScript? I believed this could be accomplished through XmlHttpRequest by adjusting its Range header. However, if the server utilizes ...

Is there a way to include individual Facebook comments for each image within a jQuery lightbox?

After completing my own version of a lightbox using HTML5, CSS, and jQuery, I encountered an issue with the comments feature. While each image in the gallery should have separate comments, Facebook's comment system only records comments for the entire ...

Unveiling concealed content with JQuery

Here is a link to my pen: http://codepen.io/anon/pen/IszKj I am looking to achieve the functionality where clicking on either "any status" or "any date" will reveal a hidden div containing a list of options. My main query is about the best approach to ta ...

How can a block be made to stay fixed and float in a flexbox layout?

There are three blocks within the body of this page. Title bar Content block Bottom block I have employed a flex display for the body layout. My goal is to make the title bar float, meaning it should always remain at the top when scrolling. I attempted ...

The Typescript Select is displaying an incorrect value

Here's the code snippet I've been working with: <select #C (change)="changeSelect(zone.id, C.value)"> <option *ngFor="let town of townsLocal" [attr.value]="town.data" [attr.selected]="town.data === zone.town && 'selected& ...

The video is not appearing on mobile devices using Safari, Firefox, and Chrome, but it is displaying properly on desktop computers

My website has a video in the header that works fine on desktop but not on mobile. I am using next.js 13.4 and here is my code: <video id="background-video" autoPlay playsInline loop muted classN ...

Top-to-Bottom Border Vanishing

I have a div that has a left border with the following style: border-left: 3px solid #F90; Now, I want the border to fade out gradually from top to bottom, going from full opacity at the top to completely transparent at the bottom. However, my attempt usi ...

What is the difference between finding the XPath of immediate child text and all descendant text?

Looking to extract the top-level category from this structure: <ul class="categories"> <li>My top level category <ul> <li>my second level category</li> </ul> </li> </ul> ...

Tips on how to loop a song continuously in jPlayer's circular mode

Can you help me figure out how to make a song repeat in jPlayer circle? I have the code for autoplay, but I also want to add a repeat functionality. I tried adding options like repeat:, but it didn't work as expected. <script type="text/javascript ...