How can I switch between columns in a dual-column layout?

In my latest project, I utilized CSS Flexbox to create a sophisticated two-column layout that allows toggling each column independently. While this functionality is exactly what I need, I can't help but feel that my current method is somewhat cumbersome. To achieve the two-column layout, I have to manually set the height of the container, which feels like an impractical solution. One approach could be to dynamically calculate the height based on the number of columns, but I find this less than ideal as well. When I experimented with using rows instead of columns, both row elements collapsed or expanded simultaneously, which was not the desired outcome. Please see the provided code snippet and design below. So, my question stands: Is there a more elegant and efficient solution to achieve this layout?

HTML

<div class="flex-grid">
   @foreach($parents as $parent)
      <div class="col">
         <div class="header-group group-toggle" id="{{$parent->id}}">
            <h4><a href="{{ url($parent->url()) }}">{{ $parent->name }}</a></h4>
                 <div class="chev-icon">
                    <i class="fas fa-chevron-right rotate"></i>
                  </div>
          </div>
        <div class="sub-group collapse in">
              @include('group.child', $parent)
        </div>
     </div>
   @endforeach
 </div>

CSS

.flex-grid {
   display: flex;
   flex-flow: column wrap;
   align-items: center;
   justify-content: flex-start;
   align-content: center;
   height: 1200px;

   .col {
     width: 50%;
   }
}

Jquery

$('.group-toggle').on('click', function () {
    if ( !$(this).next().hasClass('in')) {
        $(this).parent().children('.collapse.in').collapse('hide');
    }

    $(this).next().collapse('group-toggle'); 
}

Design

https://i.stack.imgur.com/Q8hiM.png

Answer №1

If my understanding is correct, this issue should be resolved quickly. The concern regarding the height of the columns can be directly fixed by adjusting the flexbox configuration.

Instead of utilizing column for flex direction and wrap for flex wrap...

flex-flow: column wrap; /* shorthand for flex-direction:column and flex-wrap: wrap */

...I suggest changing the direction back to the default row and disabling wrapping. This will result in a layout where each .col item has uniform height:

flex-direction: row;
flex-wrap: no-wrap;
height: 100%;

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

struggling to develop a sophisticated 'shopping cart organization' program

I am in the process of creating a database for video spots, where users can view and modify a list of spots. I am currently working on implementing a cart system that automatically stores checked spot IDs as cookies, allowing users to browse multiple pages ...

Initiate the countdown when the button is pushed

Recently ran into an issue where a button triggers a command to a Perl script, causing the page to continuously load for 60 seconds. To provide users with transparency on when the Perl script will be finished running, I implemented a JavaScript countdown t ...

Overuse of jQuery's preventDefault() and stopPropagation() functions

A recent discussion with a coworker revealed some contrasting coding practices, mainly concerning his extensive use of the two aforementioned methods in event handlers. Every event handler he creates follows this same pattern... $('span.whatever&apos ...

problem with html tags

I've been attempting to customize an input field using inline styles, but I'm running into issues. Here's what I tried: <div class="row vh-md-100"> <div class="col-md-7 my-md-auto text-center text-md-left"> ...

Discover the technique of accessing HTML/CSS toggle switch value in Golang

I am attempting to incorporate a toggle switch into my webpage. I followed this specific tutorial for guidance: w3schools.com Currently, I have set up my HTML file with a button and the toggle switch. Additionally, I configured my web server in Go to lis ...

Tips to store Google fonts in the assets directory

I've included this link in my styles.scss @import url('https://fonts.googleapis.com/css2?family=Open+Sans:wght@400;600;700&display=swap'); While it works locally, the API fails on production or is blocked. How can I host it within my p ...

Is there a way to prevent links from opening in an iframe and have them open in the main window instead?

Imagine you have an iframe on your webpage: <iframe style="border-radius:15px;" width="190" height="450" frameborder="0" scrolling="no" marginheight="5" marginwidth="10" src="../img/interactive_map/interactive_sverige.htm"> </iframe> ...

Is it necessary to have height:0 and width:0 when display:none is already applied?

Is it necessary to include height:0 and width:0 when applying display:none to a class? I've noticed these properties being used in certain instances, could they potentially resolve any issues with compatibility in older browsers? ...

I am unable to retrieve a list using Jquery's ajax function

Can someone please help me diagnose the issue with the code below and why it's not functioning properly? This code snippet is from a webmethod in an aspx.cs page. [webmethod] [ScriptMethod(ResponseFormat=ResponseFormat.Json)] public sta ...

Issue with asmx Web Service causing failure to return JSON data when using FineUploader for file uploads

I acknowledge that there are numerous questions similar to mine regarding this issue, but none of them have provided a solution for me. I understand that web services inherently convert my objects into json as part of the framework. I manually set the requ ...

Arranging elements on a webpage using Javascript

Creating a Dynamic Div Layout with JavaScript I have successfully implemented functionality wherein clicking on + opens a div and clicking on - closes it. However, I am now faced with the challenge of handling multiple divs at runtime. How can this be ach ...

Spontaneous Link with JQuery and Colorbox

Just to clarify, I have no programming experience so please explain everything in simple terms. I am currently using a .js script within SharePoint and it's working perfectly! <!DOCTYPE html> <html> <head> <meta charset= ...

Internet Explorer experiencing difficulty in loading ajax request

Hey there, I'm currently facing an issue with my script that loads data using ajax. The script splits the data, reverses it, and then displays it on the page. Everything seems to be working fine on all browsers except for IE - the dreaded Internet Exp ...

Tailwind - make sure the dropdown list is always on top of all other elements

Having an issue configuring the position of a dropdown list. The objective is to ensure it stays on top of all elements, however, when inside a relative positioned element, it ends up being obscured by it. Here's an example code snippet to illustrate ...

Leveraging the power of AngularJS controllers with jQuery's $.ajax functionality

Is there a way to utilize jQuery's $.ajax() function within an angularJS controller (instead of using angularJS built-in $http) in order to access $scope values from a view/template later? Below is an example of a somewhat minimalistic angularJS cont ...

Enabling drag and drop functionality for elements in Yii2

I am looking to create draggable elements in a Yii2 advanced application. I have tried using PHP and HTML examples similar to http://jqueryui.com/draggable, but so far it has not been successful. Can anyone provide assistance? Below is the code from the vi ...

I have successfully added an image to my CSS file, and it is appearing in Chrome's inspect feature. However, the image is not visible on the actual website

Here is the code snippet: .nav-link-apple { width: 1.6rem; background: 4.4rem; background: url(./Logo.jpg) center no-repeat; } Although the image I added can be seen in inspect on Chrome, it is not visible on the actual webpage. I have tried s ...

Instructions on creating a non-editable text area where only copying and pasting is allowed

I am looking to simply copy and paste content into a textarea without needing to make any edits within the field. <form action="save.php" method="post"> <h3>Text:</h3> <textarea name="text" rows="4" cols="50"></textarea ...

Utilizing the hcSticky plugin for creating a scrolling effect on webpage content

I'm attempting to utilize the JQuery plugin, hcSticky, in order to achieve a scrolling effect on my fixed content. However, I seem to be encountering some difficulty getting it to function properly. What could I possibly be doing incorrectly? JSFIDDL ...

Switch classes while navigating within a div

My website has a sticky sidebar with a list of cars and their corresponding categories in a table: <ul class = "cars"> <li class=""><a href="javascript:void(0)" class="model" data-id="1"> BMW </a></li> ...... ...