Considering divs in accordions as a singular unit

I am working with a grid of accordions presented like this:

<body>
    <div>
        <div class='accordion-left'>
            <h3>Left 1</h3>
            <div></div>
        </div>
        <div class='accordion-right'>
            <h3>Right 1</h3>
            <div></div>
        </div>
    </div>

    <div>
        <div class="accordion-left">
            <h3>Left 2</h3>
            <div></div>
        </div>
        <div class="accordion-right">
            <h3>Right 2</h3>
            <div></div>
        </div>
    </div>
</body>

In addition, there is a link to a jsfiddle showcasing the same.

When I click on "Left 1," based on how they are structured in HTML, I expected both "Left 2" and "Right 2" to expand together. However, that's not what happens as they end up misaligned. A similar issue arises when clicking "Right 1," resulting in only "Right 2" expanding. Ideally, if sibling divs have varying heights, I would want all matching bottom divs to drop down according to the tallest one.

Is there a way to treat these accordion div groups as a single entity?

Answer №1

If you're looking to ensure equal height for your accordion items, consider implementing the "clearfix hack". You can find more information about this technique here.

Here's how you can apply it to your accordions parent element:

.accordion-parent:before, .accordion-parent:after {
    content:"";
    display:table;
}
.accordion-parent:after {
    clear:both;
}

Even if your accordions are on the same line, they should maintain the same height without any issues (demonstrated in my demo).

For a visual representation of the concept, check out the updated version of the demo here.

Answer №2

The reason for the issue is related to the use of the float property in your code. To resolve this, try using inline-block instead for your accordions. You can also adjust the width slightly so that the left and right elements appear on the same line. Additionally, if you want them to align vertically at the top, you can apply vertical-align: top.

div {
    vertical-align: top;
}

.accordion-left {
    display: inline-block;
    width: 48%;
    overflow: hidden;
}
.accordion-right {
    display: inline-block;
    width: 48%;
    overflow: hidden;
}

http://jsfiddle.net/2Un4u/5/

Answer №3

I made some adjustments to the code by introducing a container for both sections and organizing them as left-left and right-right instead of left-right, left-right. This way, the container sets the width and ensures that the accordion will always occupy 100% of the container.

Check out the modified code on JSFiddle

  <div class="container"> <!-- new class -->
    <div class='accordion-left'>
        <h3>Left 1</h3>
        <div></div>
    </div>
    <div class='accordion-left'>
        <h3>left 2</h3>
        <div></div>
    </div>
</div>

<div class="container"> <-- new class
    <div class="accordion-right">
        <h3>right 1</h3>
        <div></div>
    </div>
    <div class="accordion-right">
        <h3>Right 2</h3>
        <div></div>
    </div>
</div>

.container{
    width: 50%;  /* define width */
    display:inline-block;
    float:left;
}
.accordion-left {
    width: 100%;
    overflow: hidden;
    display:inline-block;
}
.accordion-right {
    width: 100%;
    overflow: hidden;
    display:inline-block;
}

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 best method for creating table column text within a Bootstrap Modal through JSON data?

I am currently working on creating a table by using key value pairs from a JSON object. The keys will represent column-1 and the values will correspond to column-2. The desired output can be viewed at this link. I am wondering if there is a standard method ...

Is it possible for Google to crawl and index a website that has minified HTML and CSS?

After creating a Bootstrap website, I noticed that the HTML file is nearly 400kb in size, making it slow to load on mobile devices. To address this issue, I am considering using the tool to minify the code. Will Google be able to properly index a we ...

Utilizing a function within a span element

Can anyone help me figure out what I'm doing wrong while trying to toggle between a span and an input text field using the on function? If you want to take a look, I've created a fiddle for it here User Interface <div> <span >My va ...

Excessive Blank Space Issue on Mobile Devices with MDBootstrap

I have been working on creating a replica of Watch2Gether, and it looks great on medium and large screens. However, when viewed on a phone, everything seems perfect until you scroll to the left and notice a large white space on the right side. I've ex ...

Utilizing Arrays for Angular Data Binding with AJAX

I am currently experimenting with loading Ajax data into an array and then binding the array using Angular. Here is my code (I have some experience with KO, so I'm keeping it simple for now): Update: I managed to get it working. I believe the issue w ...

Currently, I am developing a customized stylesheet specifically designed for Internet Explorer versions 10 and 11

Is it possible to utilize this straightforward script for identifying IE versions 10 and 11? if($.browser.version == 11.0 || $.browser.version == 10.0) { $("body").addClass("ie"); } ...

Largest possible <main> container with footer positioned at the bottom

I have organized my webpage into three sections: the <header>, the <main>, and the <footer>. The <header> is fixed at the top with a height of 109px, including a 6px border, which results in the <main> having a margin of 109p ...

Fixing malfunctioning bootstrap dropdown menus in a few simple steps

For a while, I have been using the bootstrap dropdown code as a template and it was working perfectly fine. However, after ensuring everything is up to date, all my dropdowns have suddenly stopped working. Even when I tried pasting the bootstrap code dire ...

The $http patch request is failing to transmit data to the server

Recently, I started utilizing AngularJS to create a CRUD functionality using Laravel and AngularJS. I have implemented a function that serves the purpose of both updating and saving data. Here is my function code: $scope.save = function (modalstate, id) ...

Is there a way to adjust the dimensions of the "col-sm-9" div class in terms of width and height?

Struggling to adjust the size of my , but for some reason, the height command in CSS isn't taking effect. How can I set a custom size for it? This is important for my image slider, as the col-sm-10 is just too large on my website. Looking forward to y ...

How to use Jquery to dynamically alter cell backgrounds in a table based on their values?

I successfully imported the table from MySQL to HTML, as shown below: https://i.sstatic.net/kzJtF.png My script is designed to highlight the two lowest and two highest values in each column: $(document).ready(function(){ var $table = $("#tbTodos"); ...

How to download a dynamically generated PHP file to your local machine

I am trying to find a solution where the search results can be downloaded by the user and saved on their computer. Currently, the file is automatically stored on the server without giving the user an option to choose where to save it. In the search form, ...

What is the technique for placing a div element directly within the href attribute in a shape

I am wondering if it is feasible to include the following divs <div id="cal1"> [dopbsp id="1" lang=it]</div> <div id="cal2"> [dopbsp id="1" lang=it]</div> <div id="cal3"> [dopbsp id="1" lang=it]</div> directly wit ...

Ways to incorporate the setTimeOut function

<body> <script> $(window).scroll(function() { $('#csgo').each(function(){ var imagePos = $(this).offset().top; var topOfWindow = $(window).scroll ...

What is the best way to organize columns on the front end?

A table needs to be displayed on the front end with the following code snippet: <logic:present name="searchStudent"> <table class=" tblSearchResult" border="1" cellspacing="0" cellpadding="0"&g ...

Tabbed Content: Interactive content revealed upon selecting a tab (Bootstrap)

Hey there, I'm having trouble displaying content in tabs on my website. I've successfully fetched data from a MySQL table and loaded it into the tabs. However, the issue arises when the page finishes loading - I have to click on a tab to display ...

Using jQuery and PHP to send a dynamic form through AJAX

I'm currently working on a pet registration form where users can add new pets. When a user clicks the "add pet" button, I use jQuery to clone the pet section of the form and give each cloned section an id like #pet-2, #pet-3, and so on. Although my ...

Setting the attribute dynamically for a select box with multiple choices

In my form, I have multiple choice select boxes styled using bootstrap select. Since the app is developed in Express, I am having trouble retrieving the selected values due to bootstrap select creating a div and a list for user interaction. To tackle this ...

"Securing digital signatures by storing them as base64 encoded data in the

What is the most efficient way to store a base 64 encoded signature in a MySQL database? I've discovered that some of my signatures are larger than the character limit for varchar. I am currently utilizing jSignature to collect the string. Is this th ...

The image refuses to align to the left side below a floated paragraph

Hey there, I'm working on some CSS and I've run into an issue. I have two paragraphs and I want to place a picture below the paragraph on the left side. The left paragraph is longer than the one on the right, which seems to be causing the picture ...