Items expand and move seamlessly between two columns

My website features a Bootstrap accordion that I have organized into a two-column layout using the following CSS:


#accordion {
  column-count: 2;
}

.card {
  margin: 0px 20px 20px 20px;
  break-inside: avoid-column;
  -webkit-column-break-inside: avoid;
}
a{
  display:block;
}

The issue arises when expanding items in the second column. Some items in the first column jump back, especially on pages with a larger number of items, making it difficult for users to interact with them effectively.

I opted for the two-column layout due to the dynamic nature of the content generated by PHP, which can vary in the number of accordions and items.

Is there a solution to prevent these jumping behavior when expanding items across columns?

Answer №1

Consider utilizing the appropriate bootstrap functionality, like the class="row" and column classes (for example, "col-sm-6")... as shown below:

#accordion {
  column-count: 2;
}

.card {
  margin: 0px 20px 20px 20px;
  break-inside: avoid-column;
  -webkit-column-break-inside: avoid;
}
a{
  display:block;
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>

<div id="accordion" class="row">
  <div class="col-sm-6">
  
    <div class="card">
      <div class="card-header" id="heading1">
        <button class="btn" data-toggle="collapse" data-target="#collapse1" aria-expanded="false" aria-controls="collapse1">Heading1</button>
      </div>
      <div id="collapse1" class="collapse" aria-labelledby="heading1" data-parent="#accordion">
        <div class="card-body">
          <a href="#">item1</a>
          <a href="#">item2</a>
          <a href="#">item3</a>
          <a href="#">item4</a>
        </div>
      </div>
    </div>
    // more card elements with similar structure ...
  </div>
  <div class="col-sm-6">
    // more card elements with similar structure ...
  </div>
</div>

Answer №2

Follow these steps to achieve the desired layout: Start by creating a new div with the class "row" and add the "col-md-6" class to all the cards as shown below.

<div class="row">
   <div class col-md-6>
      <div class="card ">
        <div class="card-header" id="heading1">
             <button class="btn" data-toggle="collapse" data-target="#collapse1" aria-expanded="false" aria-controls="collapse1">Heading1</button>
        </div>
      <div id="collapse1" class="collapse" aria-labelledby="heading1" data-parent="#accordion">
        <div class="card-body">
         <a href="#">item1</a>
         <a href="#">item2</a>
         <a href="#">item3</a>
         <a href="#">item4</a>
        </div>
      </div>
   </div>
   <div class col-md-6>
     <div class="card">
       <div class="card-header" id="heading2">
           <button class="btn" data-toggle="collapse" data-target="#collapse2" aria-expanded="false" aria-controls="collapse2">Heading2</button>
       </div>
       <div id="collapse2" class="collapse" aria-labelledby="heading2" data-parent="#accordion">
          <div class="card-body">
              <a href="#">item1</a>
              <a href="#">item2</a>
              <a href="#">item3</a>
              <a href="#">item4</a>
          </div>
        </div>
      </div>
    </div>
</div>

Make sure to repeat this code for every pair of buttons.

Answer №3

I resolved the issue by making changes to PHP code generation instead of following the suggestion to add each item to a new row. Here's what I did:

$i =0;
$sql = "SELECT * FROM headings";
$stmt = DB::run($sql);

$count = $stmt->rowCount();
$half_count = ceil($count/2);

while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
    $i++;

    if($i==1 || $i==$half_count+1){
        $headings .= '<div class="col-lg-6 col-md-6 col-sm-12 col-xs-12 mt-5 mb-5">';
        $headings .= '<div id="accordion" class="index_accordion">';
    }

    // original code continuation

    if($i==$half_count || $i==$count){
        $headings .= '</div>';
        $headings .= '</div>';
    }
}

Now, the issue is that it functions as two separate accordions, allowing items to be expanded on both sides.

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

Using an array of objects to set a background image in a Bootstrap carousel using jQuery: a step-by-step guide

I have an array of items, each containing a background property with a URL to an image. Here is my array: https://i.sstatic.net/jfrV0.png Here is the HTML structure: <div id="myCarousel" class="carousel slide" data-ride="carousel"> <ol ...

IE is causing issues with the transition

How come the transition doesn't seem to be working in this IE example? https://codepen.io/littlesnippets/pen/rxKBWq I've searched for solutions and tried adding recommended meta tags, doctype, etc., but nothing seems to work... /* This code ...

Using HTML and jQuery to create a table with buttons to add or remove rows is a common practice. However, there

I have created a simple table with an "Add Row" button. Each new row has a "Remove" button. However, I am facing an issue in binding the 'click' event for the class 'remove'. Here is the code snippet: <script src="https://ajax.googl ...

When a click event triggers the list to close, an incorrect element

Here is the code snippet I am currently working with: http://jsfiddle.net/xFxD3/1/ However, I have encountered an issue where clicking on the "li" elements causes the list to close. The intended behavior is for the list to only close when one of the "span ...

Issue with NodeJS: The HTTP GET request is returning the code instead of the expected JSON object

My NodeJS code is designed to fetch a JSON object from a website. Here is the snippet: var http = require('http'); var url = { host: 'www.sample-website.com', headers: { "Accept": "application/json", 'Content-Type&apos ...

When using the ngFor directive, the select tag with ngModel does not correctly select options based on the specified

Issue with select dropdown not pre-selecting values in ngFor based on ngModel. Take a look at the relevant component and html code: testArr = [ { id : '1', value: 'one' }, { id : '2', ...

The element type 'ReactElement<any>' does not match a JSX element constructor function. 'undefined' cannot be assigned to type 'Element | null'

After attempting the suggested fix of deleting node_modules/ and yarn.lock, then reinstalling everything, I still cannot resolve the issue. I am currently developing a basic router that renders children based on a certain prop: import React, { Fragment } ...

Transform geojson data into an HTML table

As someone new to JavaScript, I am trying to figure out how to populate a HTML table with geojson entries like "ename" etc. Here is my current code: <table id="jsoncontent"></table> <script type="text/javascript"> window.onload = fu ...

Customizing div elements in various RMarkdown documents with a CSS file

I want to apply styling to divs in various RMarkdown files using a CSS file. However, I am encountering syntax issues when trying to use the external CSS file to style the div. Embedding the style tag within the rmarkdown body works as shown below: --- ti ...

jQuery refusing to delete class

var profession = "Chiropractor"; var elementsToToggle = "."+profession+"Toggle"; console.log(elementsToToggle); search(elementsToToggle).hide().show(); returns: .ChiropractorToggle TypeError: Object #<error> has no method 'hide' Even th ...

What is the best way to trigger a sound to play once when the document is loaded?

I was looking for an easy and straightforward method to play a sound as soon as the document is loaded. After browsing through various online resources, I couldn't find a clear explanation. Can someone please assist me on how to create this in a brow ...

Arranging Data in a Table using Tabs and JavaScript

I am seeking assistance with a table I created that has multiple tabs containing different data. Each tab displays different information within the table rows, including a column for the number of votes. I am looking to automatically sort the rows based on ...

The React JSON Unhandled Rejection problem requires immediate attention

While working on a form in React 16, I reached out to a tutor for some guidance. However, when trying to mock the componentDidMount, I encountered an error that has left me puzzled. The app still runs fine, but I am curious as to why this error is occurrin ...

access the ckeditor within the frame

I have a CKEditor in a frame and am experiencing an issue. When I try to console log from the browser, it won't work until I inspect near the frame and perform the console log again. [This is the CKEditor] https://i.stack.imgur.com/fWGzj.png The q ...

The child directive has the ability to interact with every parent directive

I am attempting to create a slider using angularJS. Within this slider, I have implemented a child directive with event listeners. Whenever the main event (mousedown) is triggered, it invokes a function from the parent directive through a controller and up ...

The Semantic-UI Dropdown does not appear when utilizing the sidebar feature

Is it normal for a dropdown in the pusher not to show when using a sidebar? I tried setting overflow-visible but it didn't work. Any suggestions on how to resolve this? Check out this JSFiddle example: https://jsfiddle.net/3djmjhn5/1/ Code: $(&ap ...

Completing the regex properly

When using my editor, I am able to paste a video URL which is then converted by regex into an embed code. The URL in the WYSIWYG-editor looks like this: Once converted, the output HTML appears as: <p>http://emedia.is.ed.ac.uk:8080/JW/wsconfig.xml& ...

Defeat the all-powerful !important tag and enable dialog customization for positioning

I am currently developing a browser extension and I am encountering an issue with using JQueryUI to display a modal dialog. The problem arises when certain websites, for example espn.com, also utilize JQueryUI but do not properly scope their own elements. ...

Problems with Searching in Bootstrap Tables

I'm experiencing a basic bootstrap error. I attempted to create a searchable table using this example: Unfortunately, the search function is not working when applied to my table. The table appears fully populated, but entering search terms like "CRY" ...

You need to click the form submit image twice in order to make it work

Struggling to get a standard opt-in form to work with an image submit button. The script works fine with a regular submit button, but the image button requires two clicks initially and then one click after unless the page is refreshed. I believe there mig ...