Is it possible to arrange the final divs on top of the initial ones without any overlap between them?

I have a script for uploading files that displays all previously uploaded images in divs with the class "download".

When I use a jQuery script to add more pictures, it puts them in divs with the class "upload".

I want these new uploads to be displayed above the divs with the class "download".

However, the code automatically places them below.

I know there should be a way to achieve this using CSS, but I'm having trouble making it work.

<div id="files" class="files">
<div id="download" class="download"></div>
<div id="download" class="download"></div>
<div id="download" class="download"></div>
<div id="download" class="download"></div>
<div id="download" class="download"></div>
<div id="upload" class="upload"></div>
<div id="upload" class="upload"></div>
<div id="upload" class="upload"></div>
</div>

Answer №1

To tackle this issue, consider utilizing flexbox along with the order attribute. This method is effective even when dealing with a mixture of uploads and downloads while maintaining the original sequence of elements (e.g., the first upload remains the first upload).

.files {
  display:flex;
}
.files .download {
  order:2;
}
.files .upload {
  order:1;
}
<div id="files" class="files">
  <div id="download" class="download">1.1</div>
  <div id="download" class="download">1.2</div>
  <div id="download" class="download">1.3</div>
  <div id="download" class="download">1.4</div>
  <div id="download" class="download">1.5</div>
  <div id="upload" class="upload">2.1</div>
  <div id="upload" class="upload">2.2</div>
  <div id="upload" class="upload">2.3</div>
  <div id="download" class="download">1.6</div>
  <div id="download" class="download">1.7</div>
  <div id="upload" class="upload">2.4</div>
  <div id="upload" class="upload">2.5</div>
</div>

Answer №2

To achieve this layout using CSS, you can style the parent element with the following code:

    .files {
        display: flex;
        flex-direction: column-reverse;
    }

By applying these flex layout rules, the items will be displayed in a reversed column order.

For a more comprehensive solution, consider having the script prepend the images instead of appending them. If you have access to the script and are using jQuery, take a look at the following methods:

These methods function differently but both allow you to add elements before other elements. Depending on the script's behavior, you can choose either method to achieve the desired result.

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

Generate an HTML webpage with dynamic content using MySQL

How can I display data from a MySQL table on an HTML page? The content in my table includes various rows that I would like to show separately on the web page. This is the structure of my table: I envision the web page layout to showcase each row individ ...

The Transform feature doesn't seem to be functioning as expected within the Bootstrap

I was experimenting with creating a simple transform transition animation using css and js, so I wrote the following code: (test page) <!DOCTYPE html> <html> <head> <style type="text/css"> *{ ...

Issue with exporting data from Datatables

I followed the instructions, and the Datatables export function worked perfectly in localhost, but when I tried it in Google Apps script, it didn't work. For more information about the issue, you can visit this link: DataTables TableTools buttons not ...

Is it possible to adjust the dimensions of the number input spinner?

When working with a number input that includes a spinner, I have encountered challenges in changing the size of the spinner itself. Specifically, I am referring to the <input type='number'> element. Despite my efforts to adjust its size thr ...

Margin discrepancies when using em units

Currently, I am utilizing an em-based margin for items within a menu that are homogeneous in terms of markup, class, and id. It appears that the margins for each of these items should be rendered the same. However, some are displaying as 1px while others a ...

What is the best way to navigate through an HTML node tree, including all of its sub elements, when walking through

Do you know of a way to iterate through the entire hierarchy of HTML elements and check each one for attributes and more without using JavaScript? We currently have a JavaScript solution that iterates through each child element and all their descendants. ...

What is the best way to ensure that a piece of content remains stationary while the neighboring content shifts in position?

Hello there! I'm currently working on my very first HTML and CSS project, and one of the tasks involves aligning 4 icons side by side horizontally. The goal is to have the icons "grow" a bit when the mouse hovers over them. However, I've encounte ...

Enhanced button effect: image shaded on hover

Can someone help me figure out how to make the button and image darker when hovered over? I tried something but it didn't work... Here is a demonstration: http://jsfiddle.net/h2o8w5y6/ <!--- example code snippet --> <div class="col-lg-4 ...

Arranging Divs Inside a Container Div - Dealing with Overflow

I am in the process of developing a website that includes a unique structure that I have not encountered before. This layout involves joining multiple sections within a wrapper. As this is my first time attempting such a layout, I am encountering some chal ...

Developing a custom CSS for React using clamp and focus attributes

I'm currently working on creating an object to be used in inline style, but I'm having trouble figuring out how to correctly write `clamp` and `after`. const PhoneInputStyle = { fontSize: clamp("13px", "1.111vw", "16px"), /*this particular ...

Difficulty with Angular JS` ng-repeat functionality

I attempted to create a sample similar to the ones found on https://angularjs.org/ Here is the JS fiddle I put together. Could someone provide assistance? What seems to be the issue? The HTML code is as follows: <h2> To Do </h2> <div ng- ...

Fancybox 2 - CSS styles vanish when using Ajax request

I have a fancybox2 with static dummy content that has styling applied. It works fine, but now I need to load dynamic content via ajax. However, when I make the ajax call, the required content loads but loses all css styling, most likely due to changes in t ...

Utilize jQuery to extract URL parameters and incorporate them into a script

I have a unique link: http://mywebsite.org/product/page?var_one=result1&var_two=result2&var_three=result3&var_four=result4 The values can vary, meaning the parameters are not fixed. My example includes 4 parameters, but there could be more ...

Discovering uncategorized elements using javascript

Let's say I have a piece of HTML with some content that is not wrapped in any tags, like this: <html> <body> <p>text in a tag</p> other text outside any tag </body> </html> Is there a way to access the untagged el ...

Arrange the Elements of Select Options

I am currently facing an issue with organizing the elements in my select list. I have two interconnected lists, one containing a list of "models" which I have successfully sorted using the following function: var sel = $('#model'); var opts_lis ...

Arrange Raphael Objects in Their Relative Positions

I've been experimenting with Raphael.js recently and I've encountered an issue related to the positioning of each Raphael object. My goal is to create multiple 'canvases' without having them overlap within a predefined div on the page. ...

How can I use JQuery to select an element with a style attribute in Internet Explorer?

When using JQuery, I have set my target to be the following: <li style="margin-left: 15px;">blah<li> I am using this code to achieve that: $(".block-category-navigation li[style='margin-left: 15px;']").addClass('sub-menu' ...

Angular has the capability to rewrite URLs in CSS, providing a unique way

An issue arises in Angular when using a base set and html5mode with SVGs. This causes things like filter: url(#url) to be rewritten as filter: url(/base/#url). https://github.com/angular/angular.js/issues/8934 Disabling html5 mode and removing the base d ...

Achieving Height Diversity in Floating HTML Divs

I have a unique set of dynamically generated divs inside a container div with different heights. <div id="container"> <div id='d1'>Varying content...</div> <div id='d2'>Varying content...</div> < ...

Increased height of iPad screen in landscape mode with no body content displayed (iOS7)

The issue: An unusual problem occurs when the specified URL is loaded on an iOS7 iPad in landscape mode; a vertical scrollbar appears. There is absolutely no content within the body, and it seems to be adjusting the body/html margin/padding. It should be ...