Is there a way to maintain the original order of DIVs when they expand on their own?

I have a grid of items that expand to show a table below when clicked. While this functionality works, it causes the DIVs to reorder as shown in my example below.

I want them to maintain their positions within their respective "columns".

Here is an illustration for clarity:

Below is the HTML code I'm working with:

<div 
class="item-component" 
ng-controller="CollapseCtrl"
ng-repeat="component in components.components | filter : components.filterByFilter | filter : searchText"
>

<div class="component-wrapper" ng-click="isCollapsed = !isCollapsed">
    Item - click to expand
</div>

<div class="codes-wrapper" collapse="isCollapsed">    
    <table class="table table-striped table-condensed">
        Expanded content here
    </table>    
</div>
</div>

Additionally, here is the .item-component class:

.item-component {
  width: 33.33333333333333%;
  float: left;
  padding-left: 15px;
}

How can I achieve the "expected result" from my illustration?

Answer №1

Instead of using float:left, try using display:inline-block for your .item-component

See Live Demo

.item-component {
  width: 33.33333333333333%;
  display: inline-block;
  padding-left: 15px;
}

Alternatively, you can refer to BootStrap and maintain the float:left by using the :before element. Remember to wrap each row:

.col{
    float:left;
    width: 32.33%;
    min-height: 50px;
    background: #ccc;
    padding: 20px;
    box-sizing: border-box;
}
.row{
    display:block;
}
/* This will solve the issue */
.row:before{
    content: " ";
    display: table;
    box-sizing: border-box;
    clear: both;
}

Live Example Here


Update: If you wish to eliminate the gap, consider a different HTML markup where each column is printed with its respective rows.

This is how the HTML markup should look like:

<div class="col">
    <div class="row" id="demo">1</div>
    <div class="row">4</div>
    <div class="row">7</div>
</div>

<div class="col">
    <div class="row">2</div>
    <div class="row">5</div>
    <div class="row">8</div>
</div>

<div class="col">
    <div class="row">3</div>
    <div class="row">6</div>
    <div class="row">9</div>
</div>

And here's the necessary CSS:

.col{
    float:left;
    width: 32.33%;
}
.row{
   display:block;
   padding: 20px;
   box-sizing: border-box;
   background: #ccc;
   min-height: 50px;
}
#demo{
    height: 150px;   
    background: red;
}

Check Out the Live Demo

Answer №2

You have the opportunity to accomplish it using the following approach.

HTML:

<div class="container">
    <div class="col">1</div>
    <div class="col">2</div>
    <div class="col">3</div>
    <br class="clear" />
    <div class="col">4</div>
    <div class="col">5</div>
    <div class="col">6</div>
    <br class="clear" />
    <div class="col">7</div>
    <div class="col">8</div>
    <div class="col">9</div>
<div>

CSS:

.col {
    float: left;
    width: 100px;
    min-height: 100px;
    background: #ccc;
    padding: 20px;
    margin: 10px;
    cursor: pointer;
}

.col:hover {
    background: yellow;
}

JS:

$('.col').click(function() {
    if ($(this).is('.clicked')) {
        $(this).removeClass('clicked');
    } else {
        $(this).addClass('clicked')
    }
});

Live demonstration: http://jsfiddle.net/S7r3D/1/

ETA: The issue with this methodology is that it displaces the entire row downwards. Finding a more elegant solution might be challenging... You could attempt to overflow the other divisions, but suitability depends on your requirements. Would such an approach be satisfactory?

ETA2: Surprisingly, I believe I have perfected it! Observe here: http://jsfiddle.net/S7r3D/3/ The pivotal modification involved reordering the divisions and placing them in columns instead.

HTML:

<div class="container">
    <div class="fleft">
        <div class="col">1</div>
        <div class="col">4</div>
        <div class="col">7</div>
    </div>
    <div class="fleft">
        <div class="col">2</div>
        <div class="col">5</div>
        <div class="col">8</div>
    </div>
    <div class="fleft">
        <div class="col">3</div>
        <div class="col">6</div>
        <div class="col">9</div>
    </div>
<div>    

CSS:

.col {
    clear: both;
    width: 100px;
    min-height: 100px;
    background: #ccc;
    padding: 20px;
    margin: 10px;
    cursor: pointer;
}

.col:hover {
    background: yellow;
}

.col.clicked {
    height: 300px;
    background-color: red;
}

.fleft
{    
    float: left;
}

JS: /* identical to above */

Answer №3

Divide the content into three separate container divs, assigning {1, 4, 7} to div1, {2, 5, 8} to div2, and {3, 6, 9} to div3 for easier management of their placement.

Failing to do so may result in challenges when trying to control their positioning effectively.

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

When I send data using axios, I receive the response in the config object instead of the data

Currently, my web app is being developed using NextJS NodeJS and Express. I have set up two servers running on localhost: one on port 3000 for Next and the other on port 9000 for Express. In the app, there is a form with two input fields where users can e ...

Event triggered by clicking on jsTree nodes

Is there a way to automatically redirect to mypage.aspx whenever a jsTree node is clicked? I want to pass the ID associated with the node to the page so it can render accordingly. Below is the code snippet I currently have for the jsTree setup. <script ...

What sets Angular and Meteor apart from each other?

Can you explain the contrast between Angular and Meteor? While it is true that both adhere to the model-view-viewmodel architecture, what sets these two frameworks apart from each other? ...

Is it necessary to have aria-checked attribute on a physical checkbox?

I've come across many instances of customized checkboxes that utilize the 'aria-checked' attribute. However, I am curious if it is necessary to include this attribute when using input type=checkbox? Will the checkbox still be accessible to s ...

Why does the "error loading page" message appear on the server when there is a PHP function inside jQuery Mobile?

I am facing an issue with a function I created in my jQuery Mobile code. It works perfectly fine on my local machine, but when I host it on the server, it does not work properly. Can someone please assist me? Here is a glimpse of the code: <!DOCTYPE ht ...

The posenet TensorFlow model results in Node.js do not contain any position detail for coordinates "x" and "y"

I executed the posenet model on node v8.11.0. Below is the code snippet that I utilized. Unfortunately, the output results are not displaying the position of each pose; instead, it simply shows [Object]. It seems like there might be a canvas-related issue. ...

Refreshing the entire page upon modifying a single state

I am currently in the process of constructing a page that includes numerous Charts, and I am utilizing a Material UI menu to switch between graphs. Each time I click on a new MenuItem, it alters my state and presents a new array of components. The pr ...

What is the best way to display an overlay internal link on top of the current one after it has been clicked

I am looking to create a button that, when clicked, will slide in a different page link and modify the URL to cover 80% of the website, while keeping the previous page in the background. Upon closing, the URL will revert back to the original one. I have c ...

Is it possible to use the jQuery .ajax() method to parse a javascript object that contains XML data?

Currently, I am facing a challenge parsing a javascript object that contains xml data within the .ajax() call using jquery. Can anyone provide guidance on how to accomplish this task? Any help would be greatly appreciated. Thank you! ...

Is there a way to specifically target the parent input div using event delegation with jQuery?

I'm having trouble inserting values into the input fields for Item Name and Description. See the HTML code below. <div class="input_fields_wrap"> <div class="input_fields_subwrap"><!-- this div is repeatable(dynamic form)--> ...

How about: "Are you utilizing the recoil selector for sorting through component

After diving into numerous examples, documentation, and videos about using selectors with recoil, I still can't seem to wrap my head around it. Maybe it's just one of those off days because it shouldn't be this difficult. Here's where ...

Filtering JSON data with JavaScript

Looking to group data in AngularJS using UnderscoreJS. Here is the JSON data: data = [ { "Building*": "Building A", "Wing*": "Wing C", "Floor*": "Floor 3", "Room Name*": "Room 3", "Room Type*": ...

Perpetual spinning of image grid

Are there any jQuery libraries available for continuous image rotation within an array? I have a collection of 30 partner logos that I would like to rotate continuously using either a jQuery library or plain CSS code. These images and information will be l ...

Selenium is transitioning to precise locations

Using the python library to control mouse movements, I have encountered an issue while trying to specify a pattern or random motions for the mouse. Initially, I attempted to determine the size of the //html element and use that information to establish bo ...

The AngularJS object becomes null when it is sent to a RESTful web service

Having some trouble with my Angular application. I am trying to update a RESTful web service by sending an int and an object. Surprisingly, the int is coming through correctly, but the object is always showing up as NULL at the web service. Strangely enoug ...

Utilizing AJAX and JavaScript to parse JSON data that includes an array

I'm encountering an issue while trying to parse a JSON message received through an AJAX response on my webpage. Strangely, it keeps throwing the following error: "SyntaxError: JSON.parse: expected ',' or ']' after array eleme ...

behavior of the back button within an AngularJS program

Currently facing an issue with a single-page application built using AngularJS. The app is composed of three main views - the login view, main view, and logout view. myapp/#/login myapp/#/main myapp/#/logout This is my route provider setup: function Ro ...

Allow the mobile web app to prompt the user's default mobile browser to open a link rather than displaying it within the app itself

After developing a mobile web app for Android and iOS, I successfully installed the apk and ipa files on my test phones. Unfortunately, I encountered an issue when attempting to open an external link that is supposed to open in the user's default bro ...

Utilize PHP to Extract ID3 Tags from MP3 Files and Embed Them into HTML

While conducting some research, I stumbled upon a helpful resource: ID3 Functions. This guide is all about PHP and how to utilize ID3 functions in web development. I'm interested in learning more about how to incorporate this into a website so that: ...

Updating the property value of an element in Angular 9

I'm facing an issue where I can't change the attribute value of an element successfully. Is there a best practice in Angular that can help solve this problem? <div name="hai" (click)=test($event) #ckechName> In the TypeScript ...