Creating a display of divs that flow from left to right in each row, and then stacking the rows from bottom to top using CSS and jQuery

My current dilemma involves a collection of divs that must be displayed in a specific order, but with a rather intricate layout. Essentially, this is the structure of my HTML:

<div>
    <div class="box">1 (first in list)</div>
    <div class="box">2</div>
    <div class="box">3</div>
    <div class="box">4</div>
    <div class="box">5</div>
    <div class="box">6</div>
    <div class="box">7</div>
    <div class="box">8</div>
    <div class="box">9 (last in list)</div>
</div>

The desired layout for these divs is to arrange them in rows of 4 from left to right, stacked from bottom to top within each row, like so:

9
5 6 7 8
1 2 3 4

I have included a fiddle showcasing the desired result: http://jsfiddle.net/mpcjs/ - However, I am seeking a solution that does not involve reordering the divs in the HTML. Additionally, please note that the number of divs may vary.

Is it feasible to achieve this? I am open to CSS3/jQuery solutions!

Answer №1

You can achieve this through the use of the jQuery Plugin called jQuery Masonry.

To make this change (starting at line 287)

var position = {
  top: minimumY + this.offset.y
};

do the following:

var position = (this.options.fromBottom) ? {
  bottom: minimumY + this.offset.y
} : {
  top: minimumY + this.offset.y
};

Check out the demo on jsFiddle: http://jsfiddle.net/fnexE/

Answer №2

If you're looking to enhance your CSS skills, consider diving into the world of flexbox. To see it in action, check out this demo on JsFiddle. Please note that I've only tested it in Chrome so far.

.flex{
width:400px;
display: -webkit-box;
display: -moz-box;
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
-webkit-box-direction: normal;
-moz-box-direction: normal;
-webkit-box-orient: horizontal;
-moz-box-orient: horizontal;
-webkit-flex-direction: row;
-ms-flex-direction: row;
flex-direction: row;
-webkit-flex-wrap: wrap-reverse;
-ms-flex-wrap: wrap-reverse;
flex-wrap: wrap-reverse;
-webkit-box-pack: start;
-moz-box-pack: start;
-webkit-justify-content: flex-start;
-ms-flex-pack: start;
justify-content: flex-start;
-webkit-align-content: stretch;
-ms-flex-line-pack: stretch;
align-content: stretch;}

Answer №4

I'm not quite sure how to stack the DIVs from the bottom, but this method will reverse their order:

HTML:

<div>
    <div>
        <div class='box'>1</div>
        <div class='box'>2</div>
        <div class='box'>3</div>
        <div class='box'>4</div>
        <div class='box'>5</div>
        <div class='box'>6</div>
        <div class='box'>7</div>
        <div class='box'>8</div>
        <div class='box'>9</div>
    </div>
</div>

CSS:

.box{
    float:left;
    border:1px solid #000;
    width:22%;
    margin:1%;
}

jQUERY:

$('div > .box').each(function() {
    $(this).prependTo(this.parentNode);
});

Answer №5

If you're looking for a simple CSS solution, here's how it can be done:

Start with the basic HTML structure:

<div class="container">HOVER ME!
<div class="inner">one</div>
<div class="inner">two</div>
<div class="inner">three</div>
<div class="inner">four</div>
<div class="inner">five</div>
<div class="inner">six</div>
<div class="inner">>seven</div>
</div>

Next, float the inner elements to the right:

.inner {
    float: right;
    width: 100px;
    height: 100px;
    background-color: lightsalmon;
    margin: 5px;
    font-size: 20px;
}

That's all! But wait, there's more - you'll need to add a hover effect:

.container:hover,
.container:hover div {
    -webkit-transform: rotate(180deg);
    -webkit-transition: all 3s;
}

Just a little touch to make it fun, but the effect is quite nice, don't you think?

Give it a try!

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

Corrupting the structure of XML data

Utilizing a jQuery object to append elements and update the values of an XML document, initializing it with an XML string containing nodes like <tgroup>, <table>, <row>, <tbody>, etc. as shown below. var str = "<txml> <tab ...

Scrolling Container Following Down the Page, Halts at Its Own Bottom (Similar to Twitter)

I am facing an issue and need some help. I am working on creating a three-column page layout where the middle section is for posts, the right section is for links and references (a bit long), and the left section is fixed. My question is: How can I preven ...

Adding a <tr> tag to an HTML table using JQuery and AJAX in the context of Django framework step by step

I am currently navigating the world of Javascript, Jquery, and Ajax requests and I'm encountering a challenge with how my scripts are executing. My homepage contains a lengthy list of items (over 1200) that need to be displayed. Previously, I loaded ...

How to make a routerLink clickable within an anchor tag in Angular 6

I am facing a peculiar issue with a group of hyperlinks. html <div class="col-12 navlinks"> <div class="text-center"> <div class="justify-content-center"> <a class="col-auto d-inline-block whitelink" rou ...

Tips for positioning two fields side by side on a webpage with CSS

I currently have two datepickers aligned vertically and I'm looking to display them in a horizontal layout with some spacing between them. What changes do I need to make in order to present these two calendar pickers side by side on the same row? Cou ...

A mysterious JavaScript snippet that automatically scrolls down when the webpage is loaded

Recently, I encountered an issue with a mysterious JavaScript script that automatically scrolls down when entering the page (I suspect it's JavaScript). I've tried to investigate using Firebug, examining the code, deleting scripts, and even remov ...

Tips for fetching data from a database using AJAX when the values of two drop-down lists are involved

I have successfully implemented an Example where I retrieve data using a single drop-down list from a database. Now, I want to extend this functionality to work with two drop-down lists, where the values retrieved from the database are dependent on the sel ...

Combining Python Bottle with Polymer Paper Elements

My website currently has a variety of forms where users input information, which is then used to calculate and display new content using Javascript. I rely on Python Bottle for user registration, form auto-filling from the backend and database management. ...

The issue of "400 Bad Request Error" in Wordpress's admin-ajax.php file is

I am attempting to send data from a form using AJAX. Initially, I have a button labeled "add file". When this button is clicked, the form is displayed via AJAX and it functions correctly. However, upon submitting the form, I encounter a bad request error ...

What is the best way to combine and merge JSON objects that consist of multiple sub-objects?

I am working with a JSON response that contains multiple objects consisting of two main objects - datacenter and environment: "deployments": [ { "datacenter": { "title": "euw1", ...

How can I remove the unsightly scrollbar caused by overflow: auto?

I've encountered a problem while working on my website. The inner div #content wasn't expanding to the full size of the page, causing the text to overflow messily over the background image and making it difficult to read. I initially used overflo ...

AJAX call not being executed by the script

I have been trying to send an ajax request to a specific file, but for some reason the file does not complete the operation. I can confirm that the request reaches the file because it echoes 'request received' and then stops abruptly. I am puzzle ...

Encountering the "TypeError: document.getElementById(...) is null" error message while utilizing react.js in conjunction with chart.js

I am encountering an issue while using react and chart.js together to create a bar chart. The problem lies in the fact that chart.js requires the use of canvas tags, and we need to locate this tag and insert the bar chart within it using the traditional do ...

"Discover a simple method to calculate the combined value of checkboxes and seamlessly transfer it to the next page

I have utilized jQuery to perform calculations on Page1.html, where I retrieve values from input boxes using the get function and set those values in other input boxes using the set function. I am having trouble getting the total value of checkboxes when t ...

What is the best way to increase the value of a variable using jQuery?

As I work on adding dates to a slider, I find myself needing to increment the values with each click. Initially, I start with the year - 2. $('#adddates').click(function() { var year = 2; $("#slider").dateRangeSlider({ bounds: { ...

The Spring Boot application is having trouble retrieving static files

I started a fresh springboot project and organized the directory structure as follows: view image description here The yml configuration is outlined below: server: port: 8783 servlet: context-path: /spring-demo spring: scan : com.example appli ...

CSS animations - transitioning opacity in and out

Looking to add some CSS animations to a menu but running into some issues. I've got the code below for the menu to fade in on hover, but can't quite figure out how to make it fade out smoothly as well. I've tried a few approaches, but all I ...

Unable to insert a JSON object into an Array

This might appear to be a duplicate, but it's not. None of the solutions I've tried have worked. Within my angular module, I have a list: this.checkedInterviews = [] Followed by a function that does the following: var interviewModel = { ...

Function that contains a JavaScript reference and observation

I'm experiencing issues with the code below and I'm having trouble figuring out what's causing the problem. function some(){ for (var i=0;i<....;i++) { var oneObject; ...some logic where this object is set oneObject.watch(prop ...

Which library do you typically employ for converting .mov files to mp4 format within a React application using Typescript?

As a web programming student, I have encountered a question during my project work. In our current project, users have the ability to upload images and videos. Interestingly, while videos can be uploaded successfully on Android devices, they seem to face ...